82d361e775
- Add ActivitySerializer + tests. - Add 'places' field to ActivityTemplateSerializer.
105 lines
3.4 KiB
Python
105 lines
3.4 KiB
Python
from rest_framework import serializers
|
|
|
|
from event.models import Activity, ActivityTag, ActivityTemplate, Event, Place
|
|
|
|
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')
|
|
|
|
|
|
class BaseActivitySerializer(serializers.ModelSerializer):
|
|
tags = ActivityTagSerializer(many=True)
|
|
|
|
serializer_url_field = EventHyperlinkedIdentityField
|
|
|
|
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
|
|
|
|
|
|
# TODO rajouter des permissions
|
|
class ActivityTemplateSerializer(
|
|
BaseActivitySerializer,
|
|
serializers.ModelSerializer,
|
|
):
|
|
|
|
class Meta:
|
|
model = ActivityTemplate
|
|
fields = (
|
|
'url', 'id', 'title', 'event', 'is_public', 'has_perm', 'min_perm',
|
|
'max_perm', 'description', 'remarks', 'tags', 'places',
|
|
)
|
|
|
|
|
|
class ActivitySerializer(
|
|
BaseActivitySerializer,
|
|
serializers.ModelSerializer,
|
|
):
|
|
|
|
class Meta:
|
|
model = Activity
|
|
fields = (
|
|
'url', 'id', 'title', 'event', 'is_public', 'has_perm', 'min_perm',
|
|
'max_perm', 'description', 'remarks', 'tags', 'places',
|
|
'parent', 'staff', 'beginning', 'end',
|
|
)
|