2017-08-03 12:14:53 +02:00
|
|
|
from django.db import transaction
|
|
|
|
from django.utils.decorators import method_decorator
|
|
|
|
|
2017-07-18 17:48:59 +02:00
|
|
|
from rest_framework import serializers
|
2017-08-03 12:14:53 +02:00
|
|
|
|
2017-07-20 15:08:19 +02:00
|
|
|
from event.models import Event, ActivityTag, Place, ActivityTemplate
|
2017-07-18 17:48:59 +02:00
|
|
|
|
2017-08-03 12:14:53 +02:00
|
|
|
from .fields import EventHyperlinkedIdentityField
|
|
|
|
|
2017-07-18 17:48:59 +02:00
|
|
|
|
2017-07-25 18:47:50 +02:00
|
|
|
# Event Serializer
|
2017-08-03 12:14:53 +02:00
|
|
|
class EventSerializer(serializers.ModelSerializer):
|
|
|
|
# TODO: Change this to a nested serializer ~(url, full_name) of User
|
2017-07-25 19:00:12 +02:00
|
|
|
created_by = serializers.ReadOnlyField(source='created_by.get_full_name')
|
2017-07-25 18:47:50 +02:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Event
|
2017-08-03 12:14:53 +02:00
|
|
|
fields = (
|
|
|
|
'url', 'id', 'title', 'slug', 'created_by', 'created_at',
|
|
|
|
'description', 'beginning_date', 'ending_date',
|
|
|
|
)
|
2017-07-22 00:56:30 +02:00
|
|
|
|
|
|
|
|
2017-07-22 02:09:11 +02:00
|
|
|
# Serializers
|
2017-07-20 15:08:19 +02:00
|
|
|
# TODO rajouter des permissions
|
2017-08-03 12:14:53 +02:00
|
|
|
class PlaceSerializer(serializers.ModelSerializer):
|
|
|
|
serializer_url_field = EventHyperlinkedIdentityField
|
2017-07-27 15:01:05 +02:00
|
|
|
|
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-08-03 12:14:53 +02:00
|
|
|
class ActivityTagSerializer(serializers.ModelSerializer):
|
|
|
|
serializer_url_field = EventHyperlinkedIdentityField
|
|
|
|
|
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):
|
2017-07-20 15:08:19 +02:00
|
|
|
tags = ActivityTagSerializer(many=True)
|
|
|
|
|
2017-08-03 12:14:53 +02:00
|
|
|
serializer_url_field = EventHyperlinkedIdentityField
|
|
|
|
|
2017-07-20 15:08:19 +02:00
|
|
|
class Meta:
|
|
|
|
model = ActivityTemplate
|
2017-08-03 12:14:53 +02:00
|
|
|
fields = (
|
|
|
|
'url', 'id', 'title', 'event', 'is_public', 'has_perm', 'min_perm',
|
|
|
|
'max_perm', 'description', 'remarks', 'tags',
|
|
|
|
)
|
2017-07-20 15:08:19 +02:00
|
|
|
|
2017-08-03 12:14:53 +02:00
|
|
|
def process_tags(self, instance, tags_data):
|
2017-07-26 15:41:20 +02:00
|
|
|
# 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
|
2017-08-03 12:14:53 +02:00
|
|
|
tags = []
|
2017-07-27 11:50:47 +02:00
|
|
|
for tag_data in tags_data:
|
2017-08-03 12:14:53 +02:00
|
|
|
tag, _ = ActivityTag.objects.get_or_create(**tag_data, defaults={
|
|
|
|
'event': instance.event,
|
|
|
|
})
|
|
|
|
tags.append(tag)
|
|
|
|
instance.tags.add(*tags)
|
2017-07-20 15:08:19 +02:00
|
|
|
|
|
|
|
def create(self, validated_data):
|
2017-07-26 15:41:20 +02:00
|
|
|
"""
|
|
|
|
@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éé
|
|
|
|
"""
|
2017-07-20 15:08:19 +02:00
|
|
|
tags_data = validated_data.pop('tags')
|
2017-08-03 12:14:53 +02:00
|
|
|
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)
|
2017-07-20 15:08:19 +02:00
|
|
|
return activity_template
|
2017-07-25 14:02:57 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ActivitySerializer(serializers.ModelSerializer):
|
|
|
|
pass
|