138 lines
3.9 KiB
Python
138 lines
3.9 KiB
Python
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()
|
|
|
|
|
|
class Priority(Base):
|
|
"""SQLAlchemy model for channel URL priorities"""
|
|
|
|
__tablename__ = "priorities"
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
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_COLUMN_TYPE, primary_key=True, default=uuid.uuid4)
|
|
tvg_id = Column(String, nullable=False)
|
|
name = Column(String, nullable=False)
|
|
group_id = Column(UUID_COLUMN_TYPE, ForeignKey("groups.id"), nullable=False)
|
|
tvg_name = Column(String)
|
|
|
|
__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(
|
|
DateTime,
|
|
default=lambda: datetime.now(timezone.utc),
|
|
onupdate=lambda: datetime.now(timezone.utc),
|
|
)
|
|
|
|
# Relationships
|
|
urls = relationship(
|
|
"ChannelURL", back_populates="channel", cascade="all, delete-orphan"
|
|
)
|
|
group = relationship("Group", back_populates="channels")
|
|
|
|
|
|
class ChannelURL(Base):
|
|
"""SQLAlchemy model for channel URLs"""
|
|
|
|
__tablename__ = "channels_urls"
|
|
|
|
id = Column(UUID_COLUMN_TYPE, primary_key=True, default=uuid.uuid4)
|
|
channel_id = Column(
|
|
UUID_COLUMN_TYPE,
|
|
ForeignKey("channels.id", ondelete="CASCADE"),
|
|
nullable=False,
|
|
)
|
|
url = Column(String, nullable=False)
|
|
in_use = Column(Boolean, default=False, nullable=False)
|
|
priority_id = Column(Integer, ForeignKey("priorities.id"), nullable=False)
|
|
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),
|
|
)
|
|
|
|
# Relationships
|
|
channel = relationship("ChannelDB", back_populates="urls")
|
|
priority = relationship("Priority")
|