122 lines
4.6 KiB
Python
122 lines
4.6 KiB
Python
from django.shortcuts import get_object_or_404
|
|
from rest_framework import serializers
|
|
from event.models import Event, ActivityTag, Place, ActivityTemplate
|
|
|
|
|
|
# Event Serializer
|
|
class EventSerializer(serializers.HyperlinkedModelSerializer):
|
|
created_by = serializers.ReadOnlyField(source='created_by.get_full_name')
|
|
created_at = serializers.ReadOnlyField()
|
|
|
|
class Meta:
|
|
model = Event
|
|
fields = ('url', 'id', 'title', 'slug', 'created_by', 'created_at',
|
|
'description', 'beginning_date', 'ending_date')
|
|
|
|
|
|
# Classes utilitaires
|
|
class EventSpecificSerializerMixin():
|
|
"""
|
|
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)
|
|
event = event_pk and get_object_or_404(Event, id=event_pk) or None
|
|
instance = ModelClass.objects.create(event=event, **validated_data)
|
|
return instance
|
|
|
|
|
|
# Serializers
|
|
# TODO rajouter des permissions
|
|
class PlaceSerializer(EventSpecificSerializerMixin,
|
|
serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
model = Place
|
|
fields = ('url', 'id', 'name', 'description', 'event')
|
|
|
|
|
|
# TODO rajouter des permissions
|
|
class ActivityTagSerializer(EventSpecificSerializerMixin,
|
|
serializers.ModelSerializer):
|
|
class Meta:
|
|
model = ActivityTag
|
|
fields = ('url', 'id', 'name', 'is_public', 'color', 'event')
|
|
|
|
|
|
# TODO rajouter des permissions
|
|
class ActivityTemplateSerializer(serializers.ModelSerializer):
|
|
event = EventSerializer(read_only=True)
|
|
tags = ActivityTagSerializer(many=True)
|
|
|
|
class Meta:
|
|
model = ActivityTemplate
|
|
fields = ('id', 'title', 'event', 'is_public', 'has_perm',
|
|
'min_perm', 'max_perm', 'description', 'remarks', 'tags',)
|
|
|
|
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')
|
|
validated_data.pop('event_pk')
|
|
event = instance.event
|
|
[setattr(instance, key, value)
|
|
for key, value in validated_data.items()]
|
|
instance.save()
|
|
# 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
|
|
for tag_data in tags_data:
|
|
tag_data.pop('event', None)
|
|
tags = [ActivityTag.objects.get_or_create(event=event, **tag_data)[0]
|
|
for tag_data in tags_data]
|
|
instance.tags.set(tags)
|
|
return instance
|
|
|
|
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')
|
|
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)
|
|
# 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
|
|
for tag_data in tags_data:
|
|
tag_data.pop('event', None)
|
|
tags = [ActivityTag.objects.get_or_create(event=event, **tag_data)[0]
|
|
for tag_data in tags_data]
|
|
activity_template.tags = tags
|
|
return activity_template
|
|
|
|
|
|
class ActivitySerializer(serializers.ModelSerializer):
|
|
pass
|