Project for the UPV to develop an app like BlaBlaCar but only for UPV people.
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

api for notifications

+84
+2
upvcarshare/config/router.py
··· 6 6 7 7 from journeys.api.v1.resources import TransportResource, ResidenceResource, CampusResource, JourneyResource, \ 8 8 join_journey, leave_journey, recommended_journeys, cancel_journey 9 + from notifications.api.v1.resources import NotificationResource 9 10 from users.api.v1.resources import me 10 11 11 12 router = SimpleRouter() ··· 14 15 router.register(r'residences', viewset=ResidenceResource) 15 16 router.register(r'campus', viewset=CampusResource) 16 17 router.register(r'journeys', viewset=JourneyResource) 18 + router.register(r'notifications', viewset=NotificationResource) 17 19 18 20 urlpatterns = [ 19 21 url(r'^journeys/recommended/$', recommended_journeys, kwargs={'pk': None}, name='all-recommended-journeys'),
+2
upvcarshare/notifications/api/__init__.py
··· 1 + # -*- coding: utf-8 -*- 2 + from __future__ import unicode_literals, print_function, absolute_import
+2
upvcarshare/notifications/api/v1/__init__.py
··· 1 + # -*- coding: utf-8 -*- 2 + from __future__ import unicode_literals, print_function, absolute_import
+22
upvcarshare/notifications/api/v1/resources.py
··· 1 + # -*- coding: utf-8 -*- 2 + from __future__ import unicode_literals, print_function, absolute_import 3 + 4 + from rest_framework import viewsets 5 + from rest_framework.permissions import IsAuthenticated 6 + 7 + from notifications.api.v1.serializers import NotificationSerializer 8 + from notifications.models import Notification 9 + 10 + 11 + class NotificationResource(viewsets.ReadOnlyModelViewSet): 12 + 13 + queryset = Notification.objects.filter(read=False) 14 + serializer_class = NotificationSerializer 15 + permission_classes = [ 16 + IsAuthenticated, 17 + ] 18 + 19 + def get_queryset(self): 20 + queryset = super(NotificationResource, self).get_queryset() 21 + queryset = queryset.filter(user=self.request.user) 22 + return queryset
+52
upvcarshare/notifications/api/v1/serializers.py
··· 1 + # -*- coding: utf-8 -*- 2 + from __future__ import unicode_literals, print_function, absolute_import 3 + 4 + from rest_framework import serializers 5 + 6 + from journeys.api.v1.serializers import JourneySerializer 7 + from journeys.models import Journey 8 + from notifications.models import Notification 9 + from users.api.v1.serializers import UserSerializer 10 + from users.models import User 11 + 12 + 13 + class BaseObjectRelatedField(serializers.RelatedField): 14 + """A custom field to use for the `actor`, `target` or `action` generic relationship.""" 15 + 16 + def to_internal_value(self, data): 17 + pass 18 + 19 + def to_representation(self, value): 20 + """Serialize tagged objects to a simple textual representation.""" 21 + if isinstance(value, User): 22 + serializer = UserSerializer(value) 23 + elif isinstance(value, Journey): 24 + serializer = JourneySerializer(value) 25 + else: 26 + raise Exception('Unexpected type of tagged object') 27 + return serializer.data 28 + 29 + 30 + class ActorObjectRelatedField(BaseObjectRelatedField): 31 + pass 32 + 33 + 34 + class TargetObjectRelatedField(BaseObjectRelatedField): 35 + pass 36 + 37 + 38 + class ActionObjectRelatedField(BaseObjectRelatedField): 39 + pass 40 + 41 + 42 + class NotificationSerializer(serializers.ModelSerializer): 43 + """Serializes a notification.""" 44 + 45 + text = serializers.CharField() 46 + actor = ActionObjectRelatedField(read_only=True) 47 + target = TargetObjectRelatedField(read_only=True) 48 + action = ActionObjectRelatedField(read_only=True) 49 + 50 + class Meta: 51 + model = Notification 52 + fields = ["actor", "verb", "action", "target", "text", "read", "created"]
+2
upvcarshare/notifications/tests/test_api.py
··· 1 + # -*- coding: utf-8 -*- 2 + from __future__ import unicode_literals, print_function, absolute_import
+2
upvcarshare/notifications/tests/test_models.py
··· 1 + # -*- coding: utf-8 -*- 2 + from __future__ import unicode_literals, print_function, absolute_import