80 lines
2.2 KiB
Python
80 lines
2.2 KiB
Python
import uuid
|
|
from datetime import datetime, timezone
|
|
|
|
from sqlalchemy import (
|
|
Boolean,
|
|
Column,
|
|
DateTime,
|
|
ForeignKey,
|
|
Integer,
|
|
String,
|
|
UniqueConstraint,
|
|
)
|
|
from sqlalchemy.dialects.postgresql import UUID
|
|
from sqlalchemy.orm import declarative_base, relationship
|
|
|
|
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 ChannelDB(Base):
|
|
"""SQLAlchemy model for IPTV channels"""
|
|
|
|
__tablename__ = "channels"
|
|
|
|
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
|
tvg_id = Column(String, nullable=False)
|
|
name = Column(String, nullable=False)
|
|
group_title = Column(String, nullable=False)
|
|
tvg_name = Column(String)
|
|
|
|
__table_args__ = (
|
|
UniqueConstraint("group_title", "name", name="uix_group_title_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),
|
|
)
|
|
|
|
# Relationship with ChannelURL
|
|
urls = relationship(
|
|
"ChannelURL", back_populates="channel", cascade="all, delete-orphan"
|
|
)
|
|
|
|
|
|
class ChannelURL(Base):
|
|
"""SQLAlchemy model for channel URLs"""
|
|
|
|
__tablename__ = "channels_urls"
|
|
|
|
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
|
channel_id = Column(
|
|
UUID(as_uuid=True),
|
|
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")
|