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:
@@ -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,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user