Commit 2c203cca authored by Luko van der Maas's avatar Luko van der Maas

added an api for setting the categories and listing them in the devices path

parent 36790af2
from django.db.models import QuerySet
from rest_framework import permissions
class IsOwner(permissions.BasePermission):
def has_object_permission(self, request, view, obj):
if isinstance(obj, QuerySet):
return True
# must be the owner to view the object
return obj.user == request.user
......@@ -2,14 +2,28 @@ from __future__ import absolute_import
from rest_framework.serializers import ModelSerializer
from pushnotifications.models import Device
from pushnotifications.models import Device, Category
class DeviceSerializer(ModelSerializer):
class Meta:
model = Device
fields = ('pk', 'registration_id', 'active', 'date_created', 'type')
fields = (
'pk',
'registration_id',
'active',
'date_created',
'type',
'receive_category'
)
read_only_fields = ('date_created',)
extra_kwargs = {'active': {'default': True}}
class CategorySerializer(ModelSerializer):
class Meta:
model = Category
fields = ('key', 'name')
from rest_framework import permissions
from rest_framework.decorators import list_route
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from pushnotifications.api.permissions import IsOwner
from pushnotifications.api.serializers import DeviceSerializer
from pushnotifications.models import Device
from pushnotifications.api.serializers import DeviceSerializer, \
CategorySerializer
from pushnotifications.models import Device, Category
class DeviceViewSet(ModelViewSet):
......@@ -27,3 +30,9 @@ class DeviceViewSet(ModelViewSet):
def perform_update(self, serializer):
serializer.save(user=self.request.user)
@list_route()
def categories(self, request):
categories = Category.objects.all()
serializer = CategorySerializer(categories, many=True)
return Response(serializer.data)
......@@ -70,6 +70,7 @@ class Message(models.Model):
)
category = models.ForeignKey(
Category,
on_delete=models.CASCADE,
verbose_name=_('category'),
default="general"
)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment