-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
63 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from rest_framework import serializers | ||
from potholes.models import Pothole, Report | ||
from potholes.serializers import PotholeSerializer, ReportSerializer | ||
from authentication.serializers import AccountSerializer | ||
from authentication.models import Account | ||
from notify.models import Action, Notification | ||
|
||
class RelatedFieldSerializer(serializers.RelatedField): | ||
|
||
def to_representation(self, value): | ||
|
||
if isinstance(value, Pothole): | ||
serializer = PotholeSerializer(value) | ||
elif isinstance(value, Account): | ||
serializer = AccountSerializer(value) | ||
else: | ||
raise Exception('Unexpected object') | ||
|
||
return serializer.data | ||
|
||
class ActionSerializer(serializers.ModelSerializer): | ||
|
||
content_object = RelatedFieldSerializer(read_only = True) | ||
|
||
class Meta: | ||
|
||
model = Action | ||
fields = '__all__' | ||
read_only_fields = ['content_object'] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,20 @@ | ||
from django.shortcuts import render | ||
from rest_framework.views import APIView | ||
from rest_framework.response import Response | ||
from rest_framework import status, permissions | ||
from notify.models import Action | ||
from notify.serializers import ActionSerializer | ||
|
||
# Create your views here. | ||
class NotificationListView(APIView): | ||
|
||
def get_objects(self, username): | ||
|
||
notifications = Action.objects.filter(recipient__username = username) | ||
return notifications | ||
|
||
def get(self, request, username): | ||
|
||
notifications = self.get_objects(username) | ||
serializer = ActionSerializer(notifications, many = True) | ||
|
||
return Response(serializer.data, status = status.HTTP_200_OK) | ||
|