From 0ec5f8033cd795bd92b5ebbd30d6e86961104c0a Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Sun, 12 Feb 2017 11:58:20 -0600 Subject: [PATCH] dispatch: code quality cleanups bss/dr.botzo#17 --- dispatch/__init__.py | 1 + dispatch/models.py | 6 ++---- dispatch/serializers.py | 7 +++++++ dispatch/views.py | 12 ++++++------ 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/dispatch/__init__.py b/dispatch/__init__.py index e69de29..e8fe2e6 100644 --- a/dispatch/__init__.py +++ b/dispatch/__init__.py @@ -0,0 +1 @@ +"""Dispatch is Django/ircbot glue code that allows REST clients to hit Django views and execute IRC bot stuff.""" diff --git a/dispatch/models.py b/dispatch/models.py index eddb2a3..fb3ab48 100644 --- a/dispatch/models.py +++ b/dispatch/models.py @@ -9,24 +9,23 @@ log = logging.getLogger('dispatch.models') class Dispatcher(models.Model): - """Organize dispatchers by key.""" key = models.CharField(max_length=16, unique=True) class Meta: + """Meta options.""" + permissions = ( ('send_message', "Can send messages to dispatchers"), ) def __str__(self): """String representation.""" - return "{0:s}".format(self.key) class DispatcherAction(models.Model): - """Handle requests to dispatchers and do something with them.""" PRIVMSG_TYPE = 'privmsg' @@ -44,5 +43,4 @@ class DispatcherAction(models.Model): def __str__(self): """String representation.""" - return "{0:s} -> {1:s} {2:s}".format(self.dispatcher.key, self.type, self.destination) diff --git a/dispatch/serializers.py b/dispatch/serializers.py index 500aa9d..ca38c16 100644 --- a/dispatch/serializers.py +++ b/dispatch/serializers.py @@ -6,22 +6,29 @@ from dispatch.models import Dispatcher, DispatcherAction class DispatcherActionSerializer(serializers.ModelSerializer): + """Serializer for the individual actions associated to a preconfigured dispatcher.""" class Meta: + """Meta options.""" + model = DispatcherAction fields = ('id', 'dispatcher', 'type', 'destination') class DispatcherSerializer(serializers.ModelSerializer): + """Serializer for a dispatcher, a set of actions for a key.""" actions = DispatcherActionSerializer(many=True, read_only=True) class Meta: + """Meta options.""" + model = Dispatcher fields = ('id', 'key', 'actions') class DispatchMessageSerializer(serializers.Serializer): + """Serializer for dispatch messaging.""" message = serializers.CharField() status = serializers.CharField(read_only=True) diff --git a/dispatch/views.py b/dispatch/views.py index 7d16961..21f534f 100644 --- a/dispatch/views.py +++ b/dispatch/views.py @@ -18,8 +18,10 @@ log = logging.getLogger('dispatch.views') class HasSendMessagePermission(IsAuthenticated): + """Class to check if the authenticated user can send messages via dispatch.""" def has_permission(self, request, view): + """Check user permission for dispatch.send_message.""" if request.user.has_perm('dispatch.send_message'): return True @@ -27,7 +29,6 @@ class HasSendMessagePermission(IsAuthenticated): class DispatcherList(generics.ListAPIView): - """List all dispatchers.""" queryset = Dispatcher.objects.all() @@ -35,7 +36,6 @@ class DispatcherList(generics.ListAPIView): class DispatcherDetail(generics.RetrieveAPIView): - """Detail the given dispatcher.""" queryset = Dispatcher.objects.all() @@ -43,12 +43,12 @@ class DispatcherDetail(generics.RetrieveAPIView): class DispatcherDetailByKey(DispatcherDetail): + """Detail a specific dispatcher.""" lookup_field = 'key' class DispatchMessage(generics.GenericAPIView): - """Send a message to the given dispatcher.""" permission_classes = (HasSendMessagePermission,) @@ -57,12 +57,13 @@ class DispatchMessage(generics.GenericAPIView): serializer_class = DispatchMessageSerializer def get(self, request, *args, **kwargs): - dispatcher = self.get_object() + """Return a default message, since this needs POST.""" data = {'message': "", 'status': "READY"} message = self.serializer_class(data=data) return Response(message.initial_data) def post(self, request, *args, **kwargs): + """Accept and dispatch a provided message.""" dispatcher = self.get_object() message = self.serializer_class(data=request.data) if message.is_valid(): @@ -101,12 +102,12 @@ class DispatchMessage(generics.GenericAPIView): class DispatchMessageByKey(DispatchMessage): + """Dispatch a message for a specific key.""" lookup_field = 'key' class DispatcherActionList(generics.ListAPIView): - """List all dispatchers.""" queryset = DispatcherAction.objects.all() @@ -114,7 +115,6 @@ class DispatcherActionList(generics.ListAPIView): class DispatcherActionDetail(generics.RetrieveAPIView): - """Detail the given dispatcher.""" queryset = DispatcherAction.objects.all()