diff --git a/dr_botzo/dispatch/migrations/0003_auto_20150619_1637.py b/dr_botzo/dispatch/migrations/0003_auto_20150619_1637.py new file mode 100644 index 0000000..f3b0ab4 --- /dev/null +++ b/dr_botzo/dispatch/migrations/0003_auto_20150619_1637.py @@ -0,0 +1,23 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('dispatch', '0002_auto_20150619_1124'), + ] + + operations = [ + migrations.AlterModelOptions( + name='dispatcher', + options={'permissions': (('send_message', 'Can send messages to dispatchers'),)}, + ), + migrations.AlterField( + model_name='dispatcheraction', + name='dispatcher', + field=models.ForeignKey(related_name='actions', to='dispatch.Dispatcher'), + ), + ] diff --git a/dr_botzo/dispatch/models.py b/dr_botzo/dispatch/models.py index cc26e47..23d286a 100644 --- a/dr_botzo/dispatch/models.py +++ b/dr_botzo/dispatch/models.py @@ -14,6 +14,11 @@ class Dispatcher(models.Model): key = models.CharField(max_length=16, unique=True) + class Meta: + permissions = ( + ('send_message', "Can send messages to dispatchers"), + ) + def __unicode__(self): """String representation.""" diff --git a/dr_botzo/dispatch/views.py b/dr_botzo/dispatch/views.py index 17ea73a..3a15ac3 100644 --- a/dr_botzo/dispatch/views.py +++ b/dr_botzo/dispatch/views.py @@ -9,8 +9,9 @@ import xmlrpclib from django.conf import settings from django.shortcuts import get_object_or_404 -from rest_framework.response import Response from rest_framework import generics, status +from rest_framework.permissions import IsAuthenticated +from rest_framework.response import Response from dispatch.models import Dispatcher, DispatcherAction from dispatch.serializers import DispatchMessageSerializer, DispatcherSerializer, DispatcherActionSerializer @@ -19,6 +20,15 @@ from dispatch.serializers import DispatchMessageSerializer, DispatcherSerializer log = logging.getLogger('dispatch.views') +class HasSendMessagePermission(IsAuthenticated): + + def has_permission(self, request, view): + if request.user.has_perm('dispatch.send_message'): + return True + + return False + + class DispatcherList(generics.ListAPIView): """List all dispatchers.""" @@ -39,6 +49,8 @@ class DispatchMessage(generics.GenericAPIView): """Send a message to the given dispatcher.""" + permission_classes = (HasSendMessagePermission,) + queryset = Dispatcher.objects.all() serializer_class = DispatchMessageSerializer