activity template serializer added

This commit is contained in:
Qwann 2017-07-22 03:12:50 +02:00
parent c9b25baec1
commit d7ee270fbf
3 changed files with 18 additions and 9 deletions

View file

@ -4,7 +4,7 @@ from event.models import Event, ActivityTag, Place, ActivityTemplate
# Classes utilitaires # Classes utilitaires
class EventSpecificSerializer(serializers.HyperlinkedModelSerializer): class EventSpecificSerializer(serializers.ModelSerializer):
""" """
Provide `update` and `create` methods for nested view with an Event Provide `update` and `create` methods for nested view with an Event
For example for Models which exetends EventSpecificMixin For example for Models which exetends EventSpecificMixin
@ -48,7 +48,6 @@ class PlaceSerializer(EventSpecificSerializer):
fields = ('url', 'id', 'name', 'description', 'event') fields = ('url', 'id', 'name', 'description', 'event')
# TODO rajouter des permissions # TODO rajouter des permissions
class ActivityTagSerializer(EventSpecificSerializer): class ActivityTagSerializer(EventSpecificSerializer):
event = EventSerializer(allow_null=True, read_only=True) event = EventSerializer(allow_null=True, read_only=True)
@ -59,29 +58,34 @@ class ActivityTagSerializer(EventSpecificSerializer):
# TODO rajouter des permissions # TODO rajouter des permissions
class ActivityTemplateSerializer(serializers.HyperlinkedModelSerializer): class ActivityTemplateSerializer(serializers.ModelSerializer):
event = serializers.ReadOnlyField(source='event.title') event = EventSerializer(read_only=True)
tags = ActivityTagSerializer(many=True) tags = ActivityTagSerializer(many=True)
class Meta: class Meta:
model = ActivityTemplate model = ActivityTemplate
fields = ('url', 'id', 'title', 'event', 'is_public', 'has_perm', fields = ('id', 'title', 'event', 'is_public', 'has_perm',
'min_perm', 'max_perm', 'description', 'remarks', 'tags',) 'min_perm', 'max_perm', 'description', 'remarks', 'tags',)
def update(self, instance, validated_data): def update(self, instance, validated_data):
tags_data = validated_data.pop('tags') tags_data = validated_data.pop('tags')
validated_data.pop('event_pk')
event = instance.event
[setattr(instance, key, value) [setattr(instance, key, value)
for key, value in validated_data.items()] for key, value in validated_data.items()]
instance.save() instance.save()
tags = [ActivityTag.objects.get_or_create(**tag_data)[0] tags = [ActivityTag.objects.get_or_create(event=event, **tag_data)[0]
for tag_data in tags_data] for tag_data in tags_data]
instance.tags = tags instance.tags = tags
return instance return instance
def create(self, validated_data): def create(self, validated_data):
tags_data = validated_data.pop('tags') tags_data = validated_data.pop('tags')
activity_template = ActivityTemplate.objects.create(**validated_data) event_pk = validated_data.pop('event_pk')
tags = [ActivityTag.objects.get_or_create(**tag_data)[0] 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]
for tag_data in tags_data] for tag_data in tags_data]
activity_template.tags = tags activity_template.tags = tags
return activity_template return activity_template

View file

@ -69,3 +69,7 @@ class ActivityTemplateViewSet(ModelViewSet):
""" """
queryset = ActivityTemplate.objects.all() queryset = ActivityTemplate.objects.all()
serializer_class = ActivityTemplateSerializer serializer_class = ActivityTemplateSerializer
def perform_create(self, serializer):
event_pk = self.kwargs.get('event_pk')
serializer.save(event_pk=event_pk)

View file

@ -8,12 +8,13 @@ router = SimpleRouter()
router.register(r'event', EventViewSet) router.register(r'event', EventViewSet)
router.register(r'place', PlaceViewSet) router.register(r'place', PlaceViewSet)
router.register(r'tag', ActivityTagViewSet) router.register(r'tag', ActivityTagViewSet)
router.register(r'activitytemplate', ActivityTemplateViewSet)
# Register nested router and register someviewsets vith it # Register nested router and register someviewsets vith it
event_router = NestedSimpleRouter(router, r'event', lookup='event') event_router = NestedSimpleRouter(router, r'event', lookup='event')
event_router.register(r'place', PlaceViewSet, base_name='event-names') event_router.register(r'place', PlaceViewSet, base_name='event-names')
event_router.register(r'tag', ActivityTagViewSet, base_name='event-names') event_router.register(r'tag', ActivityTagViewSet, base_name='event-names')
event_router.register(r'activitytemplate', ActivityTemplateViewSet,
base_name='event-names')
# The API URLs are now determined automatically by the router. # The API URLs are now determined automatically by the router.