poulpe/api/event/serializers.py
Aurélien Delobelle fc4930a49e Clean serializers and viewsets
Event-based urls
- Add viewset mixin 'EventUrlViewSetMixin' to get the event from the
'event_pk' url kwarg of a view.
- Add url serializer fields for object which can be accessed with a
root-level and/or event-specific url ('EventHyperlinked*Field).

Update viewsets and serializers to use these + clean inheritance
viewsets.
2017-08-13 18:05:24 +02:00

92 lines
3.1 KiB
Python

from django.db import transaction
from django.utils.decorators import method_decorator
from rest_framework import serializers
from event.models import Event, ActivityTag, Place, ActivityTemplate
from .fields import EventHyperlinkedIdentityField
# Event Serializer
class EventSerializer(serializers.ModelSerializer):
# TODO: Change this to a nested serializer ~(url, full_name) of User
created_by = serializers.ReadOnlyField(source='created_by.get_full_name')
class Meta:
model = Event
fields = (
'url', 'id', 'title', 'slug', 'created_by', 'created_at',
'description', 'beginning_date', 'ending_date',
)
# Serializers
# TODO rajouter des permissions
class PlaceSerializer(serializers.ModelSerializer):
serializer_url_field = EventHyperlinkedIdentityField
class Meta:
model = Place
fields = ('url', 'id', 'name', 'description', 'event')
# TODO rajouter des permissions
class ActivityTagSerializer(serializers.ModelSerializer):
serializer_url_field = EventHyperlinkedIdentityField
class Meta:
model = ActivityTag
fields = ('url', 'id', 'name', 'is_public', 'color', 'event')
# TODO rajouter des permissions
class ActivityTemplateSerializer(serializers.ModelSerializer):
tags = ActivityTagSerializer(many=True)
serializer_url_field = EventHyperlinkedIdentityField
class Meta:
model = ActivityTemplate
fields = (
'url', 'id', 'title', 'event', 'is_public', 'has_perm', 'min_perm',
'max_perm', 'description', 'remarks', 'tags',
)
def process_tags(self, instance, tags_data):
# TODO: en fonction de si backbone envoie un `id` ou non lorsque le tag
# n'existe pas encore il faudra faire un premier passage sur `tags` i
# pour s'assurer que le get ne foire pas le get si, par exemple, le tag
# été modifié entre temps dans la base de donnée (mais pas sur la
# classe backbone
tags = []
for tag_data in tags_data:
tag, _ = ActivityTag.objects.get_or_create(**tag_data, defaults={
'event': instance.event,
})
tags.append(tag)
instance.tags.add(*tags)
def create(self, validated_data):
"""
@tags comportement attendu : si l'id existe déjà on ne change pas
les autres champs et si l'id n'existe pas on le créé
"""
tags_data = validated_data.pop('tags')
activity_template = super().create(validated_data)
self.process_tags(activity_template, tags_data)
return activity_template
def update(self, instance, validated_data):
"""
@tags comportement attendu : si l'id existe déjà on ne change pas
les autres champs et si l'id n'existe pas on le créé
"""
tags_data = validated_data.pop('tags')
activity_template = super().update(instance, validated_data)
self.process_tags(activity_template, tags_data)
return activity_template
class ActivitySerializer(serializers.ModelSerializer):
pass