dispatch: code quality cleanups

bss/dr.botzo#17
This commit is contained in:
Brian S. Stephan 2017-02-12 11:58:20 -06:00
parent 015eacbe53
commit 0ec5f8033c
4 changed files with 16 additions and 10 deletions

View File

@ -0,0 +1 @@
"""Dispatch is Django/ircbot glue code that allows REST clients to hit Django views and execute IRC bot stuff."""

View File

@ -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)

View File

@ -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)

View File

@ -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()