activity template serializer added
This commit is contained in:
parent
c9b25baec1
commit
d7ee270fbf
3 changed files with 18 additions and 9 deletions
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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.
|
||||
|
|
Loading…
Reference in a new issue