Skip to content

Commit

Permalink
Added notification endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
casuru committed Oct 26, 2017
1 parent 02e2193 commit 05ec66b
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
14 changes: 13 additions & 1 deletion notify/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class Action(models.Model):
(REPORT, 'Report')
)

user = models.ForeignKey('authentication.Account')
user = models.ForeignKey('authentication.Account', related_name = 'sent')
recipient = models.ForeignKey('authentication.Account', related_name = 'recieved')
action = models.CharField(max_length = 1, choices = ACTIONS)
date_added = models.DateTimeField(auto_now_add = True)
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
Expand All @@ -31,3 +32,14 @@ def __unicode__(self):

return self.action


class Notification(models.Model):

user = models.ForeignKey('authentication.Account')
message = models.CharField(max_length = 225)
created_at = models.DateTimeField(auto_now_add = True)


def __unicode__(self):

return self.message
31 changes: 31 additions & 0 deletions notify/serializers.py
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']


21 changes: 19 additions & 2 deletions notify/views.py
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)

0 comments on commit 05ec66b

Please sign in to comment.