diff --git a/api/event/serializers.py b/api/event/serializers.py index 741c01e..dce4c5b 100644 --- a/api/event/serializers.py +++ b/api/event/serializers.py @@ -4,7 +4,7 @@ from event.models import Event, ActivityTag, Place, ActivityTemplate # Classes utilitaires -class EventSpecificSerializer(serializers.HyperlinkedModelSerializer): +class EventSpecificSerializer(serializers.ModelSerializer): """ Provide `update` and `create` methods for nested view with an Event For example for Models which exetends EventSpecificMixin @@ -48,7 +48,6 @@ class PlaceSerializer(EventSpecificSerializer): fields = ('url', 'id', 'name', 'description', 'event') - # TODO rajouter des permissions class ActivityTagSerializer(EventSpecificSerializer): event = EventSerializer(allow_null=True, read_only=True) @@ -59,29 +58,34 @@ class ActivityTagSerializer(EventSpecificSerializer): # TODO rajouter des permissions -class ActivityTemplateSerializer(serializers.HyperlinkedModelSerializer): - event = serializers.ReadOnlyField(source='event.title') +class ActivityTemplateSerializer(serializers.ModelSerializer): + event = EventSerializer(read_only=True) tags = ActivityTagSerializer(many=True) class Meta: 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',) def update(self, instance, validated_data): 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() - 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] instance.tags = tags return instance def create(self, validated_data): tags_data = validated_data.pop('tags') - activity_template = ActivityTemplate.objects.create(**validated_data) - tags = [ActivityTag.objects.get_or_create(**tag_data)[0] + 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] for tag_data in tags_data] activity_template.tags = tags return activity_template diff --git a/api/event/views.py b/api/event/views.py index b4a7ff4..4844e39 100644 --- a/api/event/views.py +++ b/api/event/views.py @@ -69,3 +69,7 @@ class ActivityTemplateViewSet(ModelViewSet): """ queryset = ActivityTemplate.objects.all() serializer_class = ActivityTemplateSerializer + + def perform_create(self, serializer): + event_pk = self.kwargs.get('event_pk') + serializer.save(event_pk=event_pk) diff --git a/api/urls.py b/api/urls.py index 60ae603..5065d7e 100644 --- a/api/urls.py +++ b/api/urls.py @@ -8,12 +8,13 @@ router = SimpleRouter() router.register(r'event', EventViewSet) router.register(r'place', PlaceViewSet) router.register(r'tag', ActivityTagViewSet) -router.register(r'activitytemplate', ActivityTemplateViewSet) # Register nested router and register someviewsets vith it event_router = NestedSimpleRouter(router, r'event', lookup='event') event_router.register(r'place', PlaceViewSet, 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.