diff --git a/api/event/serializers.py b/api/event/serializers.py index 18bc74d..6195837 100644 --- a/api/event/serializers.py +++ b/api/event/serializers.py @@ -1,6 +1,5 @@ from rest_framework import serializers -from event.models import Event -from django.contrib.auth import get_user_model +from event.models import Event, ActivityTag, Place, ActivityTemplate class EventSerializer(serializers.HyperlinkedModelSerializer): @@ -9,4 +8,50 @@ class EventSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = Event fields = ('url', 'id', 'title', 'slug', 'created_by', 'creation_date', - 'description', 'begining_date', 'ending_date') + 'description', 'beginning_date', 'ending_date') + + +# TODO rajouter des permissions +class PlaceSerializer(serializers.HyperlinkedModelSerializer): + + class Meta: + model = Place + fields = ('url', 'id', 'name', 'description',) + + +# TODO rajouter des permissions +class ActivityTagSerializer(serializers.HyperlinkedModelSerializer): + + class Meta: + model = ActivityTag + fields = ('url', 'id', 'name', 'is_public', 'color',) + + +# TODO rajouter des permissions +class ActivityTemplateSerializer(serializers.HyperlinkedModelSerializer): + event = serializers.ReadOnlyField(source='event.title') + tags = ActivityTagSerializer(many=True) + + class Meta: + model = ActivityTemplate + fields = ('url', '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') + [setattr(instance, key, value) for key, value in validated_data.items()] + instance.save() + tags = [ActivityTag.objects.get_or_create(**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] + 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 0b05eb7..fc66a42 100644 --- a/api/event/views.py +++ b/api/event/views.py @@ -2,18 +2,51 @@ from django.contrib.auth import get_user_model from rest_framework.viewsets import ModelViewSet -from api.event.serializers import EventSerializer -from event.models import Event +from api.event.serializers import EventSerializer, PlaceSerializer,\ + ActivityTagSerializer, ActivityTemplateSerializer +from event.models import Event, Place, ActivityTag, ActivityTemplate User = get_user_model() class EventViewSet(ModelViewSet): """ - This viewset automatically provides `list` and `detail` actions. + This viewset automatically provides `list`, `create`, `retrieve`, + `update` and `destroy` actions. + """ queryset = Event.objects.all() serializer_class = EventSerializer def perform_create(self, serializer): serializer.save(created_by=self.request.user) + + +class PlaceViewSet(ModelViewSet): + """ + This viewset automatically provides `list`, `create`, `retrieve`, + `update` and `destroy` actions. + + """ + queryset = Place.objects.all() + serializer_class = PlaceSerializer + + +class ActivityTagViewSet(ModelViewSet): + """ + This viewset automatically provides `list`, `create`, `retrieve`, + `update` and `destroy` actions. + + """ + queryset = ActivityTag.objects.all() + serializer_class = ActivityTagSerializer + + +class ActivityTemplateViewSet(ModelViewSet): + """ + This viewset automatically provides `list`, `create`, `retrieve`, + `update` and `destroy` actions. + + """ + queryset = ActivityTemplate.objects.all() + serializer_class = ActivityTemplateSerializer diff --git a/api/urls.py b/api/urls.py index 660569c..337a87f 100644 --- a/api/urls.py +++ b/api/urls.py @@ -1,10 +1,14 @@ from django.conf.urls import url, include from rest_framework.routers import DefaultRouter -from api.event.views import EventViewSet +from api.event.views import EventViewSet, PlaceViewSet, ActivityTagViewSet,\ + ActivityTemplateViewSet # Create a router and register our viewsets with it. router = DefaultRouter() router.register(r'event', EventViewSet) +router.register(r'place', PlaceViewSet) +router.register(r'tag', ActivityTagViewSet) +router.register(r'activitytemplate', ActivityTemplateViewSet) # The API URLs are now determined automatically by the router. # Additionally, we include the login URLs for the browsable API.