poulpe/api/event/serializers.py

98 lines
3.4 KiB
Python
Raw Normal View History

from django.shortcuts import get_object_or_404
2017-07-18 17:48:59 +02:00
from rest_framework import serializers
2017-07-20 15:08:19 +02:00
from event.models import Event, ActivityTag, Place, ActivityTemplate
2017-07-18 17:48:59 +02:00
# Event Serializer
class EventSerializer(serializers.HyperlinkedModelSerializer):
2017-07-25 19:00:12 +02:00
created_by = serializers.ReadOnlyField(source='created_by.get_full_name')
creation_date = serializers.ReadOnlyField()
class Meta:
model = Event
fields = ('url', 'id', 'title', 'slug', 'created_by', 'creation_date',
'description', 'beginning_date', 'ending_date')
# Classes utilitaires
2017-07-22 03:12:50 +02:00
class EventSpecificSerializer(serializers.ModelSerializer):
"""
Provide `update` and `create` methods for nested view with an Event
For example for Models which extends EventSpecificMixin
the event id has to be provided in the `save` method
Works fine with view.EventSpecificViewSet
Also provides :
event = eventserializer(allow_null=true, read_only=true)
"""
event = EventSerializer(allow_null=True, read_only=True)
def update(self, instance, validated_data):
"""
Note : does NOT change the event value of the instance
"""
validated_data.pop('event_pk')
[setattr(instance, key, value)
for key, value in validated_data.items()]
instance.save()
return instance
def create(self, validated_data):
ModelClass = self.Meta.model
event_pk = validated_data.pop('event_pk', None)
2017-07-22 02:09:11 +02:00
event = event_pk and get_object_or_404(Event, id=event_pk) or None
instance = ModelClass.objects.create(event=event, **validated_data)
return instance
2017-07-22 02:09:11 +02:00
# Serializers
2017-07-20 15:08:19 +02:00
# TODO rajouter des permissions
class PlaceSerializer(EventSpecificSerializer):
2017-07-20 15:08:19 +02:00
class Meta:
model = Place
2017-07-21 17:03:33 +02:00
fields = ('url', 'id', 'name', 'description', 'event')
2017-07-20 15:08:19 +02:00
# TODO rajouter des permissions
2017-07-22 02:09:31 +02:00
class ActivityTagSerializer(EventSpecificSerializer):
2017-07-20 15:08:19 +02:00
class Meta:
model = ActivityTag
2017-07-22 02:09:31 +02:00
fields = ('url', 'id', 'name', 'is_public', 'color', 'event')
2017-07-20 15:08:19 +02:00
# TODO rajouter des permissions
2017-07-22 03:12:50 +02:00
class ActivityTemplateSerializer(serializers.ModelSerializer):
event = EventSerializer(read_only=True)
2017-07-20 15:08:19 +02:00
tags = ActivityTagSerializer(many=True)
class Meta:
model = ActivityTemplate
2017-07-22 03:12:50 +02:00
fields = ('id', 'title', 'event', 'is_public', 'has_perm',
2017-07-20 15:08:19 +02:00
'min_perm', 'max_perm', 'description', 'remarks', 'tags',)
def update(self, instance, validated_data):
tags_data = validated_data.pop('tags')
2017-07-22 03:12:50 +02:00
validated_data.pop('event_pk')
event = instance.event
2017-07-21 17:03:33 +02:00
[setattr(instance, key, value)
for key, value in validated_data.items()]
2017-07-20 15:08:19 +02:00
instance.save()
2017-07-22 03:12:50 +02:00
tags = [ActivityTag.objects.get_or_create(event=event, **tag_data)[0]
2017-07-20 15:08:19 +02:00
for tag_data in tags_data]
2017-07-26 13:11:58 +02:00
instance.tags.set(tags)
2017-07-21 17:03:33 +02:00
return instance
2017-07-20 15:08:19 +02:00
def create(self, validated_data):
tags_data = validated_data.pop('tags')
2017-07-22 03:12:50 +02:00
event_pk = validated_data.pop('event_pk')
event = event_pk and get_object_or_404(Event, id=event_pk) or None
activity_template = ActivityTemplate.objects.create(event=event,
**validated_data)
tags = [ActivityTag.objects.get_or_create(event=event, **tag_data)[0]
2017-07-20 15:08:19 +02:00
for tag_data in tags_data]
activity_template.tags = tags
return activity_template
2017-07-25 14:02:57 +02:00
class ActivitySerializer(serializers.ModelSerializer):
pass