Introduced groups and added all related endpoints
All checks were successful
AWS Deploy on Push / build (push) Successful in 7m39s
All checks were successful
AWS Deploy on Push / build (push) Successful in 7m39s
This commit is contained in:
@@ -2,7 +2,7 @@ from fastapi import FastAPI
|
||||
from fastapi.concurrency import asynccontextmanager
|
||||
from fastapi.openapi.utils import get_openapi
|
||||
|
||||
from app.routers import auth, channels, playlist, priorities
|
||||
from app.routers import auth, channels, groups, playlist, priorities
|
||||
from app.utils.database import init_db
|
||||
|
||||
|
||||
@@ -68,3 +68,4 @@ app.include_router(auth.router)
|
||||
app.include_router(channels.router)
|
||||
app.include_router(playlist.router)
|
||||
app.include_router(priorities.router)
|
||||
app.include_router(groups.router)
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
from .db import Base, ChannelDB, ChannelURL
|
||||
from .db import Base, ChannelDB, ChannelURL, Group
|
||||
from .schemas import (
|
||||
ChannelCreate,
|
||||
ChannelResponse,
|
||||
ChannelUpdate,
|
||||
ChannelURLCreate,
|
||||
ChannelURLResponse,
|
||||
GroupCreate,
|
||||
GroupResponse,
|
||||
GroupUpdate,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
@@ -16,4 +19,8 @@ __all__ = [
|
||||
"ChannelURL",
|
||||
"ChannelURLCreate",
|
||||
"ChannelURLResponse",
|
||||
"Group",
|
||||
"GroupCreate",
|
||||
"GroupResponse",
|
||||
"GroupUpdate",
|
||||
]
|
||||
|
||||
@@ -1,18 +1,58 @@
|
||||
import os
|
||||
import uuid
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from sqlalchemy import (
|
||||
TEXT,
|
||||
Boolean,
|
||||
Column,
|
||||
DateTime,
|
||||
ForeignKey,
|
||||
Integer,
|
||||
String,
|
||||
TypeDecorator,
|
||||
UniqueConstraint,
|
||||
)
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
from sqlalchemy.orm import declarative_base, relationship
|
||||
|
||||
|
||||
# Custom UUID type for SQLite compatibility
|
||||
class SQLiteUUID(TypeDecorator):
|
||||
"""Enables UUID support for SQLite with proper comparison handling."""
|
||||
|
||||
impl = TEXT
|
||||
cache_ok = True
|
||||
|
||||
def process_bind_param(self, value, dialect):
|
||||
if value is None:
|
||||
return value
|
||||
if isinstance(value, uuid.UUID):
|
||||
return str(value)
|
||||
try:
|
||||
# Validate string format by attempting to create UUID
|
||||
uuid.UUID(value)
|
||||
return value
|
||||
except (ValueError, AttributeError):
|
||||
raise ValueError(f"Invalid UUID string format: {value}")
|
||||
|
||||
def process_result_value(self, value, dialect):
|
||||
if value is None:
|
||||
return value
|
||||
return uuid.UUID(value)
|
||||
|
||||
def compare_values(self, x, y):
|
||||
if x is None or y is None:
|
||||
return x == y
|
||||
return str(x) == str(y)
|
||||
|
||||
|
||||
# Determine which UUID type to use based on environment
|
||||
if os.getenv("MOCK_AUTH", "").lower() == "true":
|
||||
UUID_COLUMN_TYPE = SQLiteUUID()
|
||||
else:
|
||||
UUID_COLUMN_TYPE = UUID(as_uuid=True)
|
||||
|
||||
Base = declarative_base()
|
||||
|
||||
|
||||
@@ -25,20 +65,37 @@ class Priority(Base):
|
||||
description = Column(String, nullable=False)
|
||||
|
||||
|
||||
class Group(Base):
|
||||
"""SQLAlchemy model for channel groups"""
|
||||
|
||||
__tablename__ = "groups"
|
||||
|
||||
id = Column(UUID_COLUMN_TYPE, primary_key=True, default=uuid.uuid4)
|
||||
name = Column(String, nullable=False, unique=True)
|
||||
sort_order = Column(Integer, nullable=False, default=0)
|
||||
created_at = Column(DateTime, default=lambda: datetime.now(timezone.utc))
|
||||
updated_at = Column(
|
||||
DateTime,
|
||||
default=lambda: datetime.now(timezone.utc),
|
||||
onupdate=lambda: datetime.now(timezone.utc),
|
||||
)
|
||||
|
||||
# Relationship with Channel
|
||||
channels = relationship("ChannelDB", back_populates="group")
|
||||
|
||||
|
||||
class ChannelDB(Base):
|
||||
"""SQLAlchemy model for IPTV channels"""
|
||||
|
||||
__tablename__ = "channels"
|
||||
|
||||
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||
id = Column(UUID_COLUMN_TYPE, primary_key=True, default=uuid.uuid4)
|
||||
tvg_id = Column(String, nullable=False)
|
||||
name = Column(String, nullable=False)
|
||||
group_title = Column(String, nullable=False)
|
||||
group_id = Column(UUID_COLUMN_TYPE, ForeignKey("groups.id"), nullable=False)
|
||||
tvg_name = Column(String)
|
||||
|
||||
__table_args__ = (
|
||||
UniqueConstraint("group_title", "name", name="uix_group_title_name"),
|
||||
)
|
||||
__table_args__ = (UniqueConstraint("group_id", "name", name="uix_group_id_name"),)
|
||||
tvg_logo = Column(String)
|
||||
created_at = Column(DateTime, default=lambda: datetime.now(timezone.utc))
|
||||
updated_at = Column(
|
||||
@@ -47,10 +104,11 @@ class ChannelDB(Base):
|
||||
onupdate=lambda: datetime.now(timezone.utc),
|
||||
)
|
||||
|
||||
# Relationship with ChannelURL
|
||||
# Relationships
|
||||
urls = relationship(
|
||||
"ChannelURL", back_populates="channel", cascade="all, delete-orphan"
|
||||
)
|
||||
group = relationship("Group", back_populates="channels")
|
||||
|
||||
|
||||
class ChannelURL(Base):
|
||||
@@ -58,9 +116,9 @@ class ChannelURL(Base):
|
||||
|
||||
__tablename__ = "channels_urls"
|
||||
|
||||
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||
id = Column(UUID_COLUMN_TYPE, primary_key=True, default=uuid.uuid4)
|
||||
channel_id = Column(
|
||||
UUID(as_uuid=True),
|
||||
UUID_COLUMN_TYPE,
|
||||
ForeignKey("channels.id", ondelete="CASCADE"),
|
||||
nullable=False,
|
||||
)
|
||||
|
||||
@@ -53,12 +53,54 @@ class ChannelURLResponse(ChannelURLBase):
|
||||
pass
|
||||
|
||||
|
||||
# New Group Schemas
|
||||
class GroupCreate(BaseModel):
|
||||
"""Pydantic model for creating groups"""
|
||||
|
||||
name: str
|
||||
sort_order: int = Field(default=0, ge=0)
|
||||
|
||||
|
||||
class GroupUpdate(BaseModel):
|
||||
"""Pydantic model for updating groups"""
|
||||
|
||||
name: Optional[str] = None
|
||||
sort_order: Optional[int] = Field(None, ge=0)
|
||||
|
||||
|
||||
class GroupResponse(BaseModel):
|
||||
"""Pydantic model for group responses"""
|
||||
|
||||
id: UUID
|
||||
name: str
|
||||
sort_order: int
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
|
||||
class GroupSortUpdate(BaseModel):
|
||||
"""Pydantic model for updating a single group's sort order"""
|
||||
|
||||
sort_order: int = Field(ge=0)
|
||||
|
||||
|
||||
class GroupBulkSort(BaseModel):
|
||||
"""Pydantic model for bulk updating group sort orders"""
|
||||
|
||||
groups: list[dict] = Field(
|
||||
description="List of dicts with group_id and new sort_order",
|
||||
json_schema_extra={"example": [{"group_id": "uuid", "sort_order": 1}]},
|
||||
)
|
||||
|
||||
|
||||
class ChannelCreate(BaseModel):
|
||||
"""Pydantic model for creating channels"""
|
||||
|
||||
urls: list[ChannelURLCreate] # List of URL objects with priority
|
||||
name: str
|
||||
group_title: str
|
||||
group_id: UUID
|
||||
tvg_id: str
|
||||
tvg_logo: str
|
||||
tvg_name: str
|
||||
@@ -76,7 +118,7 @@ class ChannelUpdate(BaseModel):
|
||||
"""Pydantic model for updating channels (all fields optional)"""
|
||||
|
||||
name: Optional[str] = Field(None, min_length=1)
|
||||
group_title: Optional[str] = Field(None, min_length=1)
|
||||
group_id: Optional[UUID] = None
|
||||
tvg_id: Optional[str] = Field(None, min_length=1)
|
||||
tvg_logo: Optional[str] = None
|
||||
tvg_name: Optional[str] = Field(None, min_length=1)
|
||||
@@ -87,7 +129,7 @@ class ChannelResponse(BaseModel):
|
||||
|
||||
id: UUID
|
||||
name: str
|
||||
group_title: str
|
||||
group_id: UUID
|
||||
tvg_id: str
|
||||
tvg_logo: str
|
||||
tvg_name: str
|
||||
|
||||
@@ -13,6 +13,7 @@ from app.models import (
|
||||
ChannelURL,
|
||||
ChannelURLCreate,
|
||||
ChannelURLResponse,
|
||||
Group,
|
||||
)
|
||||
from app.models.auth import CognitoUser
|
||||
from app.models.schemas import ChannelURLUpdate
|
||||
@@ -29,12 +30,20 @@ def create_channel(
|
||||
user: CognitoUser = Depends(get_current_user),
|
||||
):
|
||||
"""Create a new channel"""
|
||||
# Check for duplicate channel (same group_title + name)
|
||||
# Check if group exists
|
||||
group = db.query(Group).filter(Group.id == channel.group_id).first()
|
||||
if not group:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Group not found",
|
||||
)
|
||||
|
||||
# Check for duplicate channel (same group_id + name)
|
||||
existing_channel = (
|
||||
db.query(ChannelDB)
|
||||
.filter(
|
||||
and_(
|
||||
ChannelDB.group_title == channel.group_title,
|
||||
ChannelDB.group_id == channel.group_id,
|
||||
ChannelDB.name == channel.name,
|
||||
)
|
||||
)
|
||||
@@ -44,7 +53,7 @@ def create_channel(
|
||||
if existing_channel:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail="Channel with same group_title and name already exists",
|
||||
detail="Channel with same group_id and name already exists",
|
||||
)
|
||||
|
||||
# Create channel without URLs first
|
||||
@@ -96,20 +105,27 @@ def update_channel(
|
||||
status_code=status.HTTP_404_NOT_FOUND, detail="Channel not found"
|
||||
)
|
||||
|
||||
# Only check for duplicates if name or group_title are being updated
|
||||
if channel.name is not None or channel.group_title is not None:
|
||||
# Only check for duplicates if name or group_id are being updated
|
||||
if channel.name is not None or channel.group_id is not None:
|
||||
name = channel.name if channel.name is not None else db_channel.name
|
||||
group_title = (
|
||||
channel.group_title
|
||||
if channel.group_title is not None
|
||||
else db_channel.group_title
|
||||
group_id = (
|
||||
channel.group_id if channel.group_id is not None else db_channel.group_id
|
||||
)
|
||||
|
||||
# Check if new group exists
|
||||
if channel.group_id is not None:
|
||||
group = db.query(Group).filter(Group.id == channel.group_id).first()
|
||||
if not group:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Group not found",
|
||||
)
|
||||
|
||||
existing_channel = (
|
||||
db.query(ChannelDB)
|
||||
.filter(
|
||||
and_(
|
||||
ChannelDB.group_title == group_title,
|
||||
ChannelDB.group_id == group_id,
|
||||
ChannelDB.name == name,
|
||||
ChannelDB.id != channel_id,
|
||||
)
|
||||
@@ -120,7 +136,7 @@ def update_channel(
|
||||
if existing_channel:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail="Channel with same group_title and name already exists",
|
||||
detail="Channel with same group_id and name already exists",
|
||||
)
|
||||
|
||||
# Update only provided fields
|
||||
@@ -163,9 +179,69 @@ def list_channels(
|
||||
return db.query(ChannelDB).offset(skip).limit(limit).all()
|
||||
|
||||
|
||||
# New endpoint to get channels by group
|
||||
@router.get("/groups/{group_id}/channels", response_model=list[ChannelResponse])
|
||||
def get_channels_by_group(
|
||||
group_id: UUID,
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""Get all channels for a specific group"""
|
||||
group = db.query(Group).filter(Group.id == group_id).first()
|
||||
if not group:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND, detail="Group not found"
|
||||
)
|
||||
return db.query(ChannelDB).filter(ChannelDB.group_id == group_id).all()
|
||||
|
||||
|
||||
# New endpoint to update a channel's group
|
||||
@router.put("/{channel_id}/group", response_model=ChannelResponse)
|
||||
@require_roles("admin")
|
||||
def update_channel_group(
|
||||
channel_id: UUID,
|
||||
group_id: UUID,
|
||||
db: Session = Depends(get_db),
|
||||
user: CognitoUser = Depends(get_current_user),
|
||||
):
|
||||
"""Update a channel's group"""
|
||||
channel = db.query(ChannelDB).filter(ChannelDB.id == channel_id).first()
|
||||
if not channel:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND, detail="Channel not found"
|
||||
)
|
||||
|
||||
group = db.query(Group).filter(Group.id == group_id).first()
|
||||
if not group:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND, detail="Group not found"
|
||||
)
|
||||
|
||||
# Check for duplicate channel name in new group
|
||||
existing_channel = (
|
||||
db.query(ChannelDB)
|
||||
.filter(
|
||||
and_(
|
||||
ChannelDB.group_id == group_id,
|
||||
ChannelDB.name == channel.name,
|
||||
ChannelDB.id != channel_id,
|
||||
)
|
||||
)
|
||||
.first()
|
||||
)
|
||||
|
||||
if existing_channel:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail="Channel with same name already exists in target group",
|
||||
)
|
||||
|
||||
channel.group_id = group_id
|
||||
db.commit()
|
||||
db.refresh(channel)
|
||||
return channel
|
||||
|
||||
|
||||
# URL Management Endpoints
|
||||
|
||||
|
||||
@router.post(
|
||||
"/{channel_id}/urls",
|
||||
response_model=ChannelURLResponse,
|
||||
|
||||
169
app/routers/groups.py
Normal file
169
app/routers/groups.py
Normal file
@@ -0,0 +1,169 @@
|
||||
from uuid import UUID
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.auth.dependencies import get_current_user, require_roles
|
||||
from app.models import Group
|
||||
from app.models.auth import CognitoUser
|
||||
from app.models.schemas import (
|
||||
GroupBulkSort,
|
||||
GroupCreate,
|
||||
GroupResponse,
|
||||
GroupSortUpdate,
|
||||
GroupUpdate,
|
||||
)
|
||||
from app.utils.database import get_db
|
||||
|
||||
router = APIRouter(prefix="/groups", tags=["groups"])
|
||||
|
||||
|
||||
@router.post("/", response_model=GroupResponse, status_code=status.HTTP_201_CREATED)
|
||||
@require_roles("admin")
|
||||
def create_group(
|
||||
group: GroupCreate,
|
||||
db: Session = Depends(get_db),
|
||||
user: CognitoUser = Depends(get_current_user),
|
||||
):
|
||||
"""Create a new channel group"""
|
||||
# Check for duplicate group name
|
||||
existing_group = db.query(Group).filter(Group.name == group.name).first()
|
||||
if existing_group:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail="Group with this name already exists",
|
||||
)
|
||||
|
||||
db_group = Group(**group.model_dump())
|
||||
db.add(db_group)
|
||||
db.commit()
|
||||
db.refresh(db_group)
|
||||
return db_group
|
||||
|
||||
|
||||
@router.get("/{group_id}", response_model=GroupResponse)
|
||||
def get_group(group_id: UUID, db: Session = Depends(get_db)):
|
||||
"""Get a group by id"""
|
||||
group = db.query(Group).filter(Group.id == group_id).first()
|
||||
if not group:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND, detail="Group not found"
|
||||
)
|
||||
return group
|
||||
|
||||
|
||||
@router.put("/{group_id}", response_model=GroupResponse)
|
||||
@require_roles("admin")
|
||||
def update_group(
|
||||
group_id: UUID,
|
||||
group: GroupUpdate,
|
||||
db: Session = Depends(get_db),
|
||||
user: CognitoUser = Depends(get_current_user),
|
||||
):
|
||||
"""Update a group's name or sort order"""
|
||||
db_group = db.query(Group).filter(Group.id == group_id).first()
|
||||
if not db_group:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND, detail="Group not found"
|
||||
)
|
||||
|
||||
# Check for duplicate name if name is being updated
|
||||
if group.name is not None and group.name != db_group.name:
|
||||
existing_group = db.query(Group).filter(Group.name == group.name).first()
|
||||
if existing_group:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail="Group with this name already exists",
|
||||
)
|
||||
|
||||
# Update only provided fields
|
||||
update_data = group.model_dump(exclude_unset=True)
|
||||
for key, value in update_data.items():
|
||||
setattr(db_group, key, value)
|
||||
|
||||
db.commit()
|
||||
db.refresh(db_group)
|
||||
return db_group
|
||||
|
||||
|
||||
@router.delete("/{group_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||
@require_roles("admin")
|
||||
def delete_group(
|
||||
group_id: UUID,
|
||||
db: Session = Depends(get_db),
|
||||
user: CognitoUser = Depends(get_current_user),
|
||||
):
|
||||
"""Delete a group (only if it has no channels)"""
|
||||
group = db.query(Group).filter(Group.id == group_id).first()
|
||||
if not group:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND, detail="Group not found"
|
||||
)
|
||||
|
||||
# Check if group has any channels
|
||||
if group.channels:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Cannot delete group with existing channels",
|
||||
)
|
||||
|
||||
db.delete(group)
|
||||
db.commit()
|
||||
return None
|
||||
|
||||
|
||||
@router.get("/", response_model=list[GroupResponse])
|
||||
def list_groups(db: Session = Depends(get_db)):
|
||||
"""List all groups sorted by sort_order"""
|
||||
return db.query(Group).order_by(Group.sort_order).all()
|
||||
|
||||
|
||||
@router.put("/{group_id}/sort", response_model=GroupResponse)
|
||||
@require_roles("admin")
|
||||
def update_group_sort_order(
|
||||
group_id: UUID,
|
||||
sort_update: GroupSortUpdate,
|
||||
db: Session = Depends(get_db),
|
||||
user: CognitoUser = Depends(get_current_user),
|
||||
):
|
||||
"""Update a single group's sort order"""
|
||||
db_group = db.query(Group).filter(Group.id == group_id).first()
|
||||
if not db_group:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND, detail="Group not found"
|
||||
)
|
||||
|
||||
db_group.sort_order = sort_update.sort_order
|
||||
db.commit()
|
||||
db.refresh(db_group)
|
||||
return db_group
|
||||
|
||||
|
||||
@router.post("/reorder", response_model=list[GroupResponse])
|
||||
@require_roles("admin")
|
||||
def bulk_update_sort_orders(
|
||||
bulk_sort: GroupBulkSort,
|
||||
db: Session = Depends(get_db),
|
||||
user: CognitoUser = Depends(get_current_user),
|
||||
):
|
||||
"""Bulk update group sort orders"""
|
||||
groups_to_update = []
|
||||
|
||||
for group_data in bulk_sort.groups:
|
||||
group_id = group_data["group_id"]
|
||||
sort_order = group_data["sort_order"]
|
||||
|
||||
group = db.query(Group).filter(Group.id == str(group_id)).first()
|
||||
if not group:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail=f"Group with id {group_id} not found",
|
||||
)
|
||||
|
||||
group.sort_order = sort_order
|
||||
groups_to_update.append(group)
|
||||
|
||||
db.commit()
|
||||
|
||||
# Return all groups in their new order
|
||||
return db.query(Group).order_by(Group.sort_order).all()
|
||||
Reference in New Issue
Block a user