feat(main): main

This commit is contained in:
2026-03-09 08:26:45 -04:00
parent f37382d2b8
commit f14454b4c8
12 changed files with 598 additions and 62 deletions

View File

@@ -39,11 +39,13 @@ class ChannelSourceRuleSchema(Schema):
source_name: str
rule_mode: str
weight: float
schedule_block_label: Optional[str] = None
class ChannelSourceAssignSchema(Schema):
source_id: int
rule_mode: str = 'allow' # allow | prefer | avoid | block
weight: float = 1.0
schedule_block_label: Optional[str] = None
class AiringSchema(Schema):
id: int
@@ -58,7 +60,21 @@ class AiringSchema(Schema):
def from_airing(airing) -> 'AiringSchema':
media_path = None
if airing.media_item:
raw_path = airing.media_item.cached_file_path or airing.media_item.file_path
item = airing.media_item
# 1. Determine if this item is from a YouTube source
is_youtube = False
if item.media_source and item.media_source.source_type in ['youtube', 'youtube_channel', 'youtube_playlist']:
is_youtube = True
# 2. Strict signaling: If YouTube, we MUST have it downloaded
if is_youtube:
raw_path = item.cached_file_path
# If cached_file_path is None, raw_path is None, and media_path remains None
else:
# Fallback for generic local files/links
raw_path = item.cached_file_path or item.file_path
if raw_path:
if raw_path.startswith("http://") or raw_path.startswith("https://"):
media_path = raw_path
@@ -133,6 +149,7 @@ def list_channel_sources(request, channel_id: int):
source_name=r.media_source.name,
rule_mode=r.rule_mode,
weight=float(r.weight),
schedule_block_label=r.schedule_block_label,
)
for r in rules
]
@@ -146,6 +163,7 @@ def assign_source_to_channel(request, channel_id: int, payload: ChannelSourceAss
media_source=source,
rule_mode=payload.rule_mode,
weight=payload.weight,
schedule_block_label=payload.schedule_block_label,
)
return 201, ChannelSourceRuleSchema(
id=rule.id,
@@ -153,6 +171,7 @@ def assign_source_to_channel(request, channel_id: int, payload: ChannelSourceAss
source_name=source.name,
rule_mode=rule.rule_mode,
weight=float(rule.weight),
schedule_block_label=rule.schedule_block_label,
)
@router.delete("/{channel_id}/sources/{rule_id}", response={204: None})

View File

@@ -1,5 +1,6 @@
from ninja import Router, Schema
from typing import List, Optional
from datetime import date, time
from core.models import ScheduleTemplate, Channel, ScheduleBlock
from django.shortcuts import get_object_or_404
from datetime import date
@@ -27,6 +28,28 @@ class ScheduleTemplateCreateSchema(Schema):
is_active: bool = True
channel_id: int
class ScheduleBlockSchema(Schema):
id: int
schedule_template_id: int
name: str
block_type: str
start_local_time: time
end_local_time: time
day_of_week_mask: int
spills_past_midnight: bool
target_content_rating: Optional[int] = None
default_genre_id: Optional[int] = None
class ScheduleBlockCreateSchema(Schema):
schedule_template_id: int
name: str
block_type: str
start_local_time: time
end_local_time: time
day_of_week_mask: int
spills_past_midnight: bool = False
target_content_rating: Optional[int] = None
@router.get("/template/", response=List[ScheduleTemplateSchema])
def list_schedule_templates(request):
return ScheduleTemplate.objects.all()
@@ -92,3 +115,29 @@ def generate_schedule_today(request, channel_id: int):
generator = ScheduleGenerator(channel=channel)
airings_created = generator.generate_for_date(date.today())
return {"status": "success", "airings_created": airings_created}
@router.get("/template/{template_id}/blocks", response=List[ScheduleBlockSchema])
def list_schedule_blocks(request, template_id: int):
template = get_object_or_404(ScheduleTemplate, id=template_id)
return template.scheduleblock_set.all().order_by('start_local_time')
@router.post("/block/", response={201: ScheduleBlockSchema})
def create_schedule_block(request, payload: ScheduleBlockCreateSchema):
template = get_object_or_404(ScheduleTemplate, id=payload.schedule_template_id)
block = ScheduleBlock.objects.create(
schedule_template=template,
name=payload.name,
block_type=payload.block_type,
start_local_time=payload.start_local_time,
end_local_time=payload.end_local_time,
day_of_week_mask=payload.day_of_week_mask,
spills_past_midnight=payload.spills_past_midnight,
target_content_rating=payload.target_content_rating,
)
return 201, block
@router.delete("/block/{block_id}", response={204: None})
def delete_schedule_block(request, block_id: int):
block = get_object_or_404(ScheduleBlock, id=block_id)
block.delete()
return 204, None