Newer
Older
#
# steering, Programme/schedule management for AURA
#
# Copyright (C) 2011-2017, 2020, Ernesto Rico Schmidt
# Copyright (C) 2017-2019, Ingo Leindecker
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from profile.models import Profile
from profile.serializers import ProfileSerializer
from typing import List
from django.contrib.auth.models import User
from django.core.exceptions import ObjectDoesNotExist
from django.utils import timezone
from program.models import (
Category,
FundingCategory,
Host,
SOLUTION_CHOICES = {
"theirs": "Discard projected timeslot. Keep existing timeslot(s).",
"ours": "Create projected timeslot. Delete existing timeslot(s).",
"theirs-start": (
"Keep existing timeslot. Create projected timeslot with start time of existing end."
),
"ours-start": (
"Create projected timeslot. Change end of existing timeslot to projected start time."
),
"theirs-end": (
"Keep existing timeslot. Create projected timeslot with end of existing start time."
),
"ours-end": (
"Create projected timeslot. Change start of existing timeslot to projected end time."
),
"theirs-both": (
"Keep existing timeslot. "
"Create two projected timeslots with end of existing start and start of existing end."
),
"ours-both": (
"Create projected timeslot. Split existing timeslot into two: \n\n"
"* set existing end time to projected start,\n"
"* create another timeslot with start = projected end and end = existing end."
),
}
class ErrorSerializer(serializers.Serializer):
message = serializers.CharField()
code = serializers.CharField(allow_null=True)
class UserSerializer(serializers.ModelSerializer):
# Add profile fields to JSON
profile = ProfileSerializer(required=False)
class Meta:
model = User
fields = (
"id",
"username",
"first_name",
"last_name",
"email",
"is_staff",
"is_active",
"is_superuser",
"password",
"profile",
)
def create(self, validated_data):
"""
Create and return a new User instance, given the validated data.
"""
profile_data = (
validated_data.pop("profile") if "profile" in validated_data else None
)
user = super(UserSerializer, self).create(validated_data)
user.date_joined = timezone.now()
user.set_password(validated_data["password"])
if profile_data:
profile = Profile(
user=user,
cba_username=profile_data.get("cba_username").strip(),
cba_user_token=profile_data.get("cba_user_token").strip(),
)
profile.save()
def update(self, instance, validated_data):
"""
Update and return an existing User instance, given the validated data.
"""
instance.first_name = validated_data.get("first_name", instance.first_name)
instance.last_name = validated_data.get("last_name", instance.last_name)
instance.email = validated_data.get("email", instance.email)
instance.is_active = validated_data.get("is_active", instance.is_active)
instance.is_staff = validated_data.get("is_staff", instance.is_staff)
instance.is_superuser = validated_data.get(
"is_superuser", instance.is_superuser
)
profile_data = (
validated_data.pop("profile") if "profile" in validated_data else None
)
if profile_data:
# TODO: How to hook into this from ProfileSerializer without having to call it here?
try:
profile = Profile.objects.get(user=instance.id)
except ObjectDoesNotExist:
profile = Profile.objects.create(user=instance, **profile_data)
profile.cba_username = profile_data.get("cba_username")
profile.cba_user_token = profile_data.get("cba_user_token")
profile.save()
instance.save()
return instance
class CategorySerializer(serializers.ModelSerializer):

Ernesto Rico Schmidt
committed
# TODO: remove this when the dashboard is updated
category = serializers.CharField(source="name")

Ernesto Rico Schmidt
committed
class Meta:
model = Category

Ernesto Rico Schmidt
committed
# TODO: replace `category` with `name` when the dashboard is updated
fields = ("category", "abbrev", "slug", "is_active", "description")
class LinkSerializer(serializers.ModelSerializer):
class Meta:
model = Link
class HostSerializer(serializers.ModelSerializer):
links = LinkSerializer(many=True, required=False)
thumbnails = serializers.SerializerMethodField()
def get_thumbnails(host) -> List[str]:
"""Returns thumbnails"""
thumbnails = []
if host.image.name and THUMBNAIL_SIZES:
for size in THUMBNAIL_SIZES:
thumbnails.append(host.image.crop[size].name)
return thumbnails
class Meta:
model = Host
def create(self, validated_data):
links_data = validated_data.pop("links", [])
host = Host.objects.create(**validated_data)
for link_data in links_data:
Link.objects.create(host=host, **link_data)
host.save()
return host
def update(self, instance, validated_data):
"""
Update and return an existing Host instance, given the validated data.
"""
instance.name = validated_data.get("name", instance.name)
instance.is_active = validated_data.get("is_active", instance.is_active)
instance.email = validated_data.get("email", instance.email)
instance.website = validated_data.get("website", instance.website)
instance.biography = validated_data.get("biography", instance.biography)
instance.image = validated_data.get("image", instance.image)
instance.ppoi = validated_data.get("ppoi", instance.ppoi)
if instance.links.count() > 0:
for link in instance.links.all():
link.delete(keep_parents=True)
if links := validated_data.get("links"):
for link_data in links:
Link.objects.create(host=instance, **link_data)
instance.save()
return instance
class LanguageSerializer(serializers.ModelSerializer):
class Meta:
model = Language
class TopicSerializer(serializers.ModelSerializer):

Ernesto Rico Schmidt
committed
# TODO: remove this when the dashboard is updated
topic = serializers.CharField(source="name")

Ernesto Rico Schmidt
committed
class Meta:
model = Topic

Ernesto Rico Schmidt
committed
# TODO: replace `topic` with `name` when the dashboard is updated
fields = ("topic", "abbrev", "slug", "is_active")
class MusicFocusSerializer(serializers.ModelSerializer):

Ernesto Rico Schmidt
committed
# TODO: remove this when the dashboard is updated
focus = serializers.CharField(source="name")

Ernesto Rico Schmidt
committed
class Meta:
model = MusicFocus

Ernesto Rico Schmidt
committed
# TODO: replace `focus` with `name` when the dashboard is updated
fields = ("focus", "abbrev", "slug", "is_active")

Ingo Leindecker
committed
class TypeSerializer(serializers.ModelSerializer):

Ernesto Rico Schmidt
committed
# TODO: remove this when the dashboard is updated
type = serializers.CharField(source="name")

Ernesto Rico Schmidt
committed

Ingo Leindecker
committed
class Meta:
model = Type

Ernesto Rico Schmidt
committed
# TODO: replace `type` with `name` when the dashboard is updated

Ingo Leindecker
committed
class FundingCategorySerializer(serializers.ModelSerializer):

Ernesto Rico Schmidt
committed
# TODO: remove this when the dashboard is updated
fundingcategory = serializers.CharField(source="name")

Ernesto Rico Schmidt
committed

Ingo Leindecker
committed
class Meta:

Ernesto Rico Schmidt
committed
# TODO: replace `fundingcategory` with `name` when the dashboard is updated
fields = ("fundingcategory", "abbrev", "slug", "is_active")

Ingo Leindecker
committed
class ShowSerializer(serializers.HyperlinkedModelSerializer):
owners = serializers.PrimaryKeyRelatedField(queryset=User.objects.all(), many=True)
category = serializers.PrimaryKeyRelatedField(
queryset=Category.objects.all(), many=True
)
hosts = serializers.PrimaryKeyRelatedField(queryset=Host.objects.all(), many=True)
language = serializers.PrimaryKeyRelatedField(
queryset=Language.objects.all(), many=True
)
topic = serializers.PrimaryKeyRelatedField(queryset=Topic.objects.all(), many=True)
# TODO: replace `musicfocs` with `music_focus` and remove the source when the dashboard is
# updated
musicfocus = serializers.PrimaryKeyRelatedField(
queryset=MusicFocus.objects.all(), source="music_focus", many=True
)
type = serializers.PrimaryKeyRelatedField(queryset=Type.objects.all())
# TODO: replace `fundingcategory` with `funding_category` and remove the source when the
# dashboard is updated
fundingcategory = serializers.PrimaryKeyRelatedField(
queryset=FundingCategory.objects.all(), source="funding_category"
)
predecessor = serializers.PrimaryKeyRelatedField(
queryset=Show.objects.all(), required=False, allow_null=True
)
thumbnails = serializers.SerializerMethodField()
def get_thumbnails(show) -> List[str]:
"""Returns thumbnails"""
thumbnails = []
if show.image.name and THUMBNAIL_SIZES:
for size in THUMBNAIL_SIZES:
thumbnails.append(show.image.crop[size].name)
return thumbnails
class Meta:
model = Show
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
fields = (
"id",
"name",
"slug",
"image",
"ppoi",
"logo",
"short_description",
"description",
"email",
"website",
"type",
"fundingcategory",
"predecessor",
"cba_series_id",
"default_playlist_id",
"category",
"hosts",
"owners",
"language",
"topic",
"musicfocus",
"thumbnails",
"is_active",
"is_public",
)
def create(self, validated_data):
"""
Create and return a new Show instance, given the validated data.
"""
owners = validated_data.pop("owners")
category = validated_data.pop("category")
hosts = validated_data.pop("hosts")
language = validated_data.pop("language")
topic = validated_data.pop("topic")
music_focus = validated_data.pop("music_focus")
show = Show.objects.create(**validated_data)
# Save many-to-many relationships

jackie / Andrea Ida Malkah Klaura
committed
show.owners.set(owners)
show.category.set(category)
show.hosts.set(hosts)
show.language.set(language)
show.topic.set(topic)
show.music_focus.set(music_focus)
show.save()
return show
def update(self, instance, validated_data):
"""
Update and return an existing Show instance, given the validated data.
"""
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
instance.name = validated_data.get("name", instance.name)
instance.slug = validated_data.get("slug", instance.slug)
instance.image = validated_data.get("image", instance.image)
instance.ppoi = validated_data.get("ppoi", instance.ppoi)
instance.logo = validated_data.get("logo", instance.logo)
instance.short_description = validated_data.get(
"short_description", instance.short_description
)
instance.description = validated_data.get("description", instance.description)
instance.email = validated_data.get("email", instance.email)
instance.website = validated_data.get("website", instance.website)
instance.cba_series_id = validated_data.get(
"cba_series_id", instance.cba_series_id
)
instance.default_playlist_id = validated_data.get(
"default_playlist_id", instance.default_playlist_id
)
instance.type = validated_data.get("type", instance.type)
instance.funding_category = validated_data.get(
"funding_category", instance.funding_category
)
instance.predecessor = validated_data.get("predecessor", instance.predecessor)
instance.is_active = validated_data.get("is_active", instance.is_active)
instance.is_public = validated_data.get("is_public", instance.is_public)
instance.owners.set(validated_data.get("owners", instance.owners))
instance.category.set(validated_data.get("category", instance.category))
instance.hosts.set(validated_data.get("hosts", instance.hosts))
instance.language.set(validated_data.get("language", instance.language))
instance.topic.set(validated_data.get("topic", instance.topic))
instance.music_focus.set(
validated_data.get("music_focus", instance.music_focus)
)
instance.save()
return instance
class ScheduleSerializer(serializers.ModelSerializer):
rrule = serializers.PrimaryKeyRelatedField(
queryset=RRule.objects.all(),
help_text=Schedule.rrule.field.help_text,
)
show = serializers.PrimaryKeyRelatedField(
queryset=Show.objects.all(),
help_text=Schedule.show.field.help_text,
)
# TODO: remove this when the dashboard is updated
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
byweekday = serializers.IntegerField(
source="by_weekday",
help_text=Schedule.by_weekday.field.help_text,
)
dstart = serializers.DateField(
source="first_date",
help_text=Schedule.first_date.field.help_text,
)
tstart = serializers.TimeField(
source="start_time",
help_text=Schedule.start_time.field.help_text,
)
tend = serializers.TimeField(
source="end_time",
help_text=Schedule.end_time.field.help_text,
)
until = serializers.DateField(
source="last_date",
help_text=Schedule.last_date.field.help_text,
)
dryrun = serializers.BooleanField(
write_only=True,
required=False,
help_text=(
"Whether to simulate the database changes. If true, no database changes will occur. "
"Instead a list of objects that would be created, updated and deleted if dryrun was "
"false will be returned."
),
)
def create(self, validated_data):
"""Create and return a new Schedule instance, given the validated data."""
rrule = validated_data.pop("rrule")
show = validated_data.pop("show")
schedule = Schedule.objects.create(**validated_data)
schedule.rrule = rrule
schedule.show = show
schedule.save()
return schedule
def update(self, instance, validated_data):
"""Update and return an existing Schedule instance, given the validated data."""
instance.by_weekday = validated_data.get("byweekday", instance.by_weekday)
instance.first_date = validated_data.get("dstart", instance.first_date)
instance.start_time = validated_data.get("tstart", instance.start_time)
instance.end_time = validated_data.get("tend", instance.end_time)
instance.last_date = validated_data.get("until", instance.last_date)
instance.is_repetition = validated_data.get(
"is_repetition", instance.is_repetition
)
instance.default_playlist_id = validated_data.get(
"default_playlist_id", instance.default_playlist_id
)
instance.rrule = validated_data.get("rrule", instance.rrule)
instance.show = validated_data.get("show", instance.show)
instance.add_days_no = validated_data.get("add_days_no", instance.add_days_no)
instance.add_business_days_only = validated_data.get(
"add_business_days_only", instance.add_business_days_only
)
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
class CollisionSerializer(serializers.Serializer):
id = serializers.IntegerField()
start = serializers.DateTimeField()
end = serializers.DateTimeField()
playlist_id = serializers.IntegerField(allow_null=True)
show = serializers.IntegerField()
show_name = serializers.CharField()
is_repetition = serializers.BooleanField()
schedule = serializers.IntegerField()
memo = serializers.CharField()
note_id = serializers.IntegerField(allow_null=True)
class ProjectedTimeSlotSerializer(serializers.Serializer):
hash = serializers.CharField()
start = serializers.DateTimeField()
end = serializers.DateTimeField()
collisions = CollisionSerializer(many=True)
error = serializers.CharField(allow_null=True)
solution_choices = serializers.ListField(
child=serializers.ChoiceField(SOLUTION_CHOICES)
)
class DryRunTimeSlotSerializer(serializers.Serializer):
id = serializers.PrimaryKeyRelatedField(
queryset=TimeSlot.objects.all(), allow_null=True
)
schedule = serializers.PrimaryKeyRelatedField(
queryset=Schedule.objects.all(), allow_null=True
)
playlist_id = serializers.IntegerField(allow_null=True)
start = serializers.DateField()
end = serializers.DateField()
is_repetition = serializers.BooleanField()
memo = serializers.CharField()
class ScheduleCreateUpdateRequestSerializer(serializers.Serializer):
schedule = ScheduleSerializer()
solutions = serializers.DictField(child=serializers.ChoiceField(SOLUTION_CHOICES))
notes = serializers.DictField(child=serializers.IntegerField(), required=False)
playlists = serializers.DictField(child=serializers.IntegerField(), required=False)
# TODO: There shouldn’t be a separate ScheduleSerializer for use in responses.
# Instead the default serializer should be used. Unfortunately, the
# code that generates the data creates custom dicts with this particular format.
class ScheduleInResponseSerializer(serializers.Serializer):
# "Schedule schema type" is the rendered name of the ScheduleSerializer.
"""
For documentation on the individual fields see the
Schedule schema type.
"""
add_business_days_only = serializers.BooleanField()
add_days_no = serializers.IntegerField(allow_null=True)
by_weekday = serializers.IntegerField()
default_playlist_id = serializers.IntegerField(allow_null=True)
end_time = serializers.TimeField()
first_date = serializers.DateField()
id = serializers.PrimaryKeyRelatedField(queryset=Schedule.objects.all())
is_repetition = serializers.BooleanField()
last_date = serializers.DateField()
rrule = serializers.PrimaryKeyRelatedField(queryset=RRule.objects.all())
show = serializers.PrimaryKeyRelatedField(queryset=Note.objects.all())
start_time = serializers.TimeField()
class ScheduleConflictResponseSerializer(serializers.Serializer):
projected = ProjectedTimeSlotSerializer(many=True)
solutions = serializers.DictField(child=serializers.ChoiceField(SOLUTION_CHOICES))
notes = serializers.DictField(child=serializers.IntegerField())
playlists = serializers.DictField(child=serializers.IntegerField())
schedule = ScheduleInResponseSerializer()
class ScheduleDryRunResponseSerializer(serializers.Serializer):
created = DryRunTimeSlotSerializer(many=True)
updated = DryRunTimeSlotSerializer(many=True)
deleted = DryRunTimeSlotSerializer(many=True)
class TimeSlotSerializer(serializers.ModelSerializer):
show = serializers.PrimaryKeyRelatedField(queryset=Show.objects.all())
schedule = serializers.PrimaryKeyRelatedField(queryset=Schedule.objects.all())
class Meta:
model = TimeSlot
def create(self, validated_data):
"""Create and return a new TimeSlot instance, given the validated data."""
return TimeSlot.objects.create(**validated_data)
def update(self, instance, validated_data):
"""Update and return an existing Show instance, given the validated data."""
# Only save certain fields
instance.memo = validated_data.get("memo", instance.memo)
instance.is_repetition = validated_data.get(
"is_repetition", instance.is_repetition
)
instance.playlist_id = validated_data.get("playlist_id", instance.playlist_id)
instance.save()
return instance
class NoteSerializer(serializers.ModelSerializer):
show = serializers.PrimaryKeyRelatedField(queryset=Show.objects.all())
timeslot = serializers.PrimaryKeyRelatedField(queryset=TimeSlot.objects.all())
host = serializers.PrimaryKeyRelatedField(queryset=Host.objects.all())
thumbnails = serializers.SerializerMethodField()
def get_thumbnails(note) -> List[str]:
"""Returns thumbnails"""
thumbnails = []
if note.image.name and THUMBNAIL_SIZES:
for size in THUMBNAIL_SIZES:
thumbnails.append(note.image.crop[size].name)
return thumbnails
class Meta:
model = Note
def create(self, validated_data):
"""Create and return a new Note instance, given the validated data."""
# Save the creator
validated_data["user_id"] = self.context["user_id"]
# Try to retrieve audio URL from CBA
validated_data["audio_url"] = get_audio_url(validated_data.get("cba_id", None))
note = Note.objects.create(**validated_data)
# Assign note to timeslot
try:
timeslot = TimeSlot.objects.get(pk=note.timeslot_id)
timeslot.note_id = note.id
timeslot.save(update_fields=["note_id"])
except ObjectDoesNotExist:
pass
return note
def update(self, instance, validated_data):
"""Update and return an existing Note instance, given the validated data."""
instance.show = validated_data.get("show", instance.show)
instance.timeslot = validated_data.get("timeslot", instance.timeslot)
instance.title = validated_data.get("title", instance.title)
instance.slug = validated_data.get("slug", instance.slug)
instance.summary = validated_data.get("summary", instance.summary)
instance.content = validated_data.get("content", instance.content)
instance.image = validated_data.get("image", instance.image)
instance.ppoi = validated_data.get("ppoi", instance.ppoi)
instance.status = validated_data.get("status", instance.status)
instance.host = validated_data.get("host", instance.host)
instance.cba_id = validated_data.get("cba_id", instance.cba_id)
instance.audio_url = get_audio_url(instance.cba_id)
instance.save()
# Remove existing note connections from timeslots
timeslots = TimeSlot.objects.filter(note_id=instance.id)
for ts in timeslots:
ts.note_id = None
ts.save(update_fields=["note_id"])
# Assign note to timeslot
try:
timeslot = TimeSlot.objects.get(pk=instance.timeslot.id)
timeslot.note_id = instance.id
timeslot.save(update_fields=["note_id"])
except ObjectDoesNotExist:
pass

jackie / Andrea Ida Malkah Klaura
committed
return instance