Newer
Older
@staticmethod
def get_end(obj) -> datetime:
return obj.end.astimezone(tz=ZoneInfo(settings.TIME_ZONE))
def update(self, instance, validated_data):
"""Update and return an existing Show instance, given the validated data."""
user = self.context.get("request").user
user_is_owner = user in instance.schedule.show.owners.all()
# Having the update_timeslot permission overrides the ownership
if not (
user.has_perm("program.update_timeslot")
or (user.has_perm("program.change_timeslot") and user_is_owner)
):
raise exceptions.PermissionDenied(
detail="You are not allowed to update this timeslot."
)
if "memo" in validated_data:
instance.memo = validated_data.get("memo")
if "repetition_of" in validated_data:
instance.repetition_of = validated_data.get("repetition_of")
if "playlist_id" in validated_data:
instance.playlist_id = validated_data.get("playlist_id")
instance.save()
return instance
class NoteLinkSerializer(serializers.ModelSerializer):
type_id = serializers.PrimaryKeyRelatedField(queryset=LinkType.objects.all(), source="type")
class Meta:
model = NoteLink
fields = ("type_id", "url")
tags_json_schema = {"type": "array", "items": {"type": "string"}}
class NoteSerializer(serializers.ModelSerializer):
contributor_ids = serializers.PrimaryKeyRelatedField(
many=True,
queryset=Profile.objects.all(),
required=False,
source="contributors",
help_text="`Profile` IDs that contributed to this episode.",
)
image_id = serializers.PrimaryKeyRelatedField(
queryset=Image.objects.all(),
required=False,
allow_null=True,
source="image",
help_text="`Image` ID.",
language_ids = serializers.PrimaryKeyRelatedField(
allow_null=True,
many=True,
queryset=Language.objects.all(),
required=False,
source="language",
help_text="Array of `Language` IDs.",
links = NoteLinkSerializer(many=True, required=False, help_text="Array of `Link` objects.")
playlist_id = serializers.IntegerField(required=False, help_text="Array of `Playlist` IDs.")
tags = JSONSchemaField(tags_json_schema, required=False, help_text="Tags of the Note.")
timeslot_id = serializers.PrimaryKeyRelatedField(
queryset=TimeSlot.objects.all(),
required=False,
source="timeslot",
help_text="`Timeslot` ID.",
topic_ids = serializers.PrimaryKeyRelatedField(
allow_null=True,
many=True,
queryset=Topic.objects.all(),
required=False,
source="topic",
help_text="Array of `Topic`IDs.",
class Meta:
model = Note
"created_at",
"created_by",
"updated_at",
"updated_by",
)
"contributor_ids",
"language_ids",
def update(self, instance, validated_data):
"""Update and return an existing Note instance, given the validated data."""
user = self.context.get("request").user
user_is_owner = user in instance.timeslot.schedule.show.owners.all()
# Having the update_note permission overrides the ownership
if not (
user.has_perm("program.update_note")
or (user.has_perm("program.change_note") and user_is_owner)
):
raise exceptions.PermissionDenied(detail="You are not allowed to update this note.")
if "cba_id" in validated_data:
instance.cba_id = validated_data.get("cba_id")
if "content" in validated_data:
instance.content = validated_data.get("content")
if "image" in validated_data:
instance.image = validated_data.get("image")
if "summary" in validated_data:
instance.summary = validated_data.get("summary")
if "timeslot" in validated_data:
instance.timeslot = validated_data.get("timeslot")
if "tags" in validated_data:
instance.tags = validated_data.get("tags")
if "title" in validated_data:
instance.title = validated_data.get("title")
# optional many-to-many
if "contributors" in validated_data:
instance.contributors.set(validated_data.get("contributors", []))
if "language" in validated_data:
instance.language.set(validated_data.get("language", []))
# Only update this field if the user has the update_note permission, ignore otherwise
if "topic" in validated_data and user.has_perm("program.update_note"):
instance.topic.set(validated_data.get("topic", []))
if "links" in validated_data:
instance = update_links(instance, validated_data.get("links"))
instance.updated_by = self.context.get("request").user.username
instance.save()

jackie / Andrea Ida Malkah Klaura
committed
return instance
class RadioSettingsSerializer(serializers.ModelSerializer):
cba = serializers.SerializerMethodField()
image_requirements = serializers.SerializerMethodField()
playout = serializers.SerializerMethodField()
program = serializers.SerializerMethodField()
station = serializers.SerializerMethodField()
class Meta:
fields = read_only_fields = (
"id",
"cba",
"image_requirements",
"playout",
"program",
def get_cba(self, obj) -> RadioCBASettings:
if self.context.get("request").user.is_authenticated:
return RadioCBASettings(
api_key=obj.cba_api_key,
domains=obj.cba_domains,
)
return RadioCBASettings(domains=obj.cba_domains)
@staticmethod
def get_image_requirements(obj) -> RadioImageRequirementsSettings:
def get_aspect_ratio(field) -> tuple[int, int] | tuple[float, float]:
"""return the tuple of ints or floats representing the aspect ratio of the image."""
values = field.split(":")
try:
return int(values[0]), int(values[1])
except ValueError:
return float(values[0]), float(values[1])
aspect_ratios = {
"note.image": get_aspect_ratio(obj.note_image_aspect_ratio),
"profile.image": get_aspect_ratio(obj.profile_image_aspect_ratio),
"show.image": get_aspect_ratio(obj.show_image_aspect_ratio),
"show.logo": get_aspect_ratio(obj.show_logo_aspect_ratio),
"aspect_ratio": aspect_ratios["note.image"],
"shape": obj.profile_image_shape,
"aspect_ratio": aspect_ratios["profile.image"],
"shape": obj.profile_image_shape,
}
},
"show.image": {
"frame": {
"aspect_ratio": aspect_ratios["show.image"],
"shape": obj.profile_image_shape,
}
},
"show.logo": {
"frame": {
"aspect_ratio": aspect_ratios["show.logo"],
"shape": obj.profile_image_shape,
}
},
}
def get_program(obj) -> RadioProgramSettings:
return RadioProgramSettings(
micro=MicroProgram(show_id=obj.micro_show.id if obj.micro_show else None),
fallback=ProgramFallback(
show_id=obj.fallback_show.id if obj.fallback_show else None,
default_pool="fallback" if obj.fallback_default_pool else "",
),
)
def get_playout(obj) -> RadioPlayoutSettings:
return RadioPlayoutSettings(
line_in_channels=obj.line_in_channels,
pools=obj.pools,
)
def get_station(obj) -> RadioStationSettings:
logo = (
Logo(
url=f"{settings.SITE_URL}{obj.station_logo.url}",
height=obj.station_logo.height,
width=obj.station_logo.width,
)
if obj.station_logo
else None
)
return RadioStationSettings(
name=obj.station_name,
logo=logo,
website=obj.station_website,
)
class BasicProgramEntrySerializer(serializers.Serializer):
start = serializers.DateTimeField()
end = serializers.DateTimeField()
timeslot_id = serializers.IntegerField(allow_null=True, source="timeslot.id")
playlist_id = serializers.IntegerField(allow_null=True)
show_id = serializers.IntegerField(source="show.id")
class PlayoutProgramEntrySerializer(BasicProgramEntrySerializer):
class PlayoutShowSerializer(serializers.ModelSerializer):
class Meta:
model = Show
fields = ["id", "name", "default_playlist_id"]
class PlayoutScheduleSerializer(serializers.ModelSerializer):
class Meta:
model = Schedule
fields = ["id", "default_playlist_id"]
class PlayoutEpisodeSerializer(serializers.ModelSerializer):
class Meta:
model = Note
fields = ["id", "title"]
timeslot = TimeSlotSerializer(allow_null=True)
show = PlayoutShowSerializer()
episode = PlayoutEpisodeSerializer(allow_null=True, source="timeslot.note")
schedule = PlayoutScheduleSerializer(allow_null=True, source="timeslot.schedule")
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
class CalendarSchemaSerializer(serializers.Serializer):
class Wrapper:
def __init__(self, program: list[ProgramEntry]):
self.program = program
@cached_property
def shows(self):
show_ids = set(entry.show.id for entry in self.program)
return Show.objects.distinct().filter(id__in=show_ids)
@cached_property
def timeslots(self):
timeslot_ids = set(entry.timeslot.id for entry in self.program if entry.timeslot)
return TimeSlot.objects.distinct().filter(id__in=timeslot_ids)
@cached_property
def episodes(self):
return Note.objects.distinct().filter(timeslot__in=self.timeslots)
@cached_property
def profiles(self):
return Profile.objects.distinct().filter(
Q(shows__in=self.shows) | Q(notes__in=self.episodes)
)
@property
def categories(self):
return Category.objects.distinct().filter(shows__in=self.shows)
@property
def funding_categories(self):
return FundingCategory.objects.distinct().filter(shows__in=self.shows)
@property
def types(self):
return Type.objects.distinct().filter(shows__in=self.shows)
@property
def topics(self):
return Topic.objects.distinct().filter(
Q(shows__in=self.shows) | Q(episodes__in=self.episodes)
)
@property
def languages(self):
return Language.objects.distinct().filter(
Q(shows__in=self.shows) | Q(episodes__in=self.episodes)
)
@property
def music_focuses(self):
return MusicFocus.objects.distinct().filter(shows__in=self.shows)
@cached_property
def images(self):
return Image.objects.distinct().filter(
Q(logo_shows__in=self.shows)
| Q(shows__in=self.shows)
| Q(profiles__in=self.profiles)
| Q(notes__in=self.episodes)
)
@property
def licenses(self):
return License.objects.distinct().filter(images__in=self.images)
@property
def link_types(self):
return LinkType.objects.all()

Ernesto Rico Schmidt
committed
class CalendarTimeslotSerializer(TimeSlotSerializer):
class Meta(TimeSlotSerializer.Meta):
fields = [f for f in TimeSlotSerializer.Meta.fields if f != "memo"]

Ernesto Rico Schmidt
committed
class CalendarEpisodeSerializer(NoteSerializer):
class Meta(NoteSerializer.Meta):
fields = [
field
for field in NoteSerializer.Meta.fields
if field not in ["created_at", "created_by", "updated_at", "updated_by"]
]

Ernesto Rico Schmidt
committed
class CalendarProfileSerializer(ProfileSerializer):
class Meta(ProfileSerializer.Meta):
fields = [
field
for field in ProfileSerializer.Meta.fields
if field
not in [
"created_at",
"created_by",
"owner_ids",
"updated_at",
"updated_by",
]
]

Ernesto Rico Schmidt
committed
class CalendarShowSerializer(ShowSerializer):
class Meta(ShowSerializer.Meta):
fields = [
field
for field in ShowSerializer.Meta.fields
if field
not in [
"created_at",
"created_by",
"internal_note",
"owner_ids",
"updated_at",
"updated_by",
]
]
shows = CalendarShowSerializer(many=True)
timeslots = CalendarTimeslotSerializer(many=True)

Ernesto Rico Schmidt
committed
profiles = CalendarProfileSerializer(many=True)
categories = CategorySerializer(many=True)
funding_categories = FundingCategorySerializer(many=True)
types = TypeSerializer(many=True)
images = ImageSerializer(many=True)
topics = TopicSerializer(many=True)
languages = LanguageSerializer(many=True)
music_focuses = MusicFocusSerializer(many=True)
program = BasicProgramEntrySerializer(many=True)

Ernesto Rico Schmidt
committed
episodes = CalendarEpisodeSerializer(many=True)
licenses = LicenseSerializer(many=True)
link_types = LinkTypeSerializer(many=True)
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
class ApplicationStatePurgeSerializer(serializers.Serializer):
@staticmethod
def _render_model_category_definitions():
yield "<dl>"
for category_name, models in application_state_manager.categorized_models.items():
model_names = ", ".join(sorted(model._meta.label for model in models))
yield f"<dt>{category_name}</dt>"
yield f"<dd>{model_names}</dd>"
yield "</dl>"
models = serializers.MultipleChoiceField(
choices=application_state_manager.model_choices, default=set()
)
model_categories = serializers.MultipleChoiceField(
choices=application_state_manager.model_category_choices,
default=set(),
help_text=(
"Selects multiple models by their categorization. "
"Models included in the categories are: "
f"{''.join(_render_model_category_definitions())}"
),
)
invert_selection = serializers.BooleanField(
default=False,
help_text=(
"Inverts the model selection that is selected through other filters. "
"Selects all models if set to true and no other filters have been set."
),
)