parent
015eacbe53
commit
0ec5f8033c
@ -0,0 +1 @@
|
|||||||
|
"""Dispatch is Django/ircbot glue code that allows REST clients to hit Django views and execute IRC bot stuff."""
|
@ -9,24 +9,23 @@ log = logging.getLogger('dispatch.models')
|
|||||||
|
|
||||||
|
|
||||||
class Dispatcher(models.Model):
|
class Dispatcher(models.Model):
|
||||||
|
|
||||||
"""Organize dispatchers by key."""
|
"""Organize dispatchers by key."""
|
||||||
|
|
||||||
key = models.CharField(max_length=16, unique=True)
|
key = models.CharField(max_length=16, unique=True)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
|
"""Meta options."""
|
||||||
|
|
||||||
permissions = (
|
permissions = (
|
||||||
('send_message', "Can send messages to dispatchers"),
|
('send_message', "Can send messages to dispatchers"),
|
||||||
)
|
)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
"""String representation."""
|
"""String representation."""
|
||||||
|
|
||||||
return "{0:s}".format(self.key)
|
return "{0:s}".format(self.key)
|
||||||
|
|
||||||
|
|
||||||
class DispatcherAction(models.Model):
|
class DispatcherAction(models.Model):
|
||||||
|
|
||||||
"""Handle requests to dispatchers and do something with them."""
|
"""Handle requests to dispatchers and do something with them."""
|
||||||
|
|
||||||
PRIVMSG_TYPE = 'privmsg'
|
PRIVMSG_TYPE = 'privmsg'
|
||||||
@ -44,5 +43,4 @@ class DispatcherAction(models.Model):
|
|||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
"""String representation."""
|
"""String representation."""
|
||||||
|
|
||||||
return "{0:s} -> {1:s} {2:s}".format(self.dispatcher.key, self.type, self.destination)
|
return "{0:s} -> {1:s} {2:s}".format(self.dispatcher.key, self.type, self.destination)
|
||||||
|
@ -6,22 +6,29 @@ from dispatch.models import Dispatcher, DispatcherAction
|
|||||||
|
|
||||||
|
|
||||||
class DispatcherActionSerializer(serializers.ModelSerializer):
|
class DispatcherActionSerializer(serializers.ModelSerializer):
|
||||||
|
"""Serializer for the individual actions associated to a preconfigured dispatcher."""
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
|
"""Meta options."""
|
||||||
|
|
||||||
model = DispatcherAction
|
model = DispatcherAction
|
||||||
fields = ('id', 'dispatcher', 'type', 'destination')
|
fields = ('id', 'dispatcher', 'type', 'destination')
|
||||||
|
|
||||||
|
|
||||||
class DispatcherSerializer(serializers.ModelSerializer):
|
class DispatcherSerializer(serializers.ModelSerializer):
|
||||||
|
"""Serializer for a dispatcher, a set of actions for a key."""
|
||||||
|
|
||||||
actions = DispatcherActionSerializer(many=True, read_only=True)
|
actions = DispatcherActionSerializer(many=True, read_only=True)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
|
"""Meta options."""
|
||||||
|
|
||||||
model = Dispatcher
|
model = Dispatcher
|
||||||
fields = ('id', 'key', 'actions')
|
fields = ('id', 'key', 'actions')
|
||||||
|
|
||||||
|
|
||||||
class DispatchMessageSerializer(serializers.Serializer):
|
class DispatchMessageSerializer(serializers.Serializer):
|
||||||
|
"""Serializer for dispatch messaging."""
|
||||||
|
|
||||||
message = serializers.CharField()
|
message = serializers.CharField()
|
||||||
status = serializers.CharField(read_only=True)
|
status = serializers.CharField(read_only=True)
|
||||||
|
@ -18,8 +18,10 @@ log = logging.getLogger('dispatch.views')
|
|||||||
|
|
||||||
|
|
||||||
class HasSendMessagePermission(IsAuthenticated):
|
class HasSendMessagePermission(IsAuthenticated):
|
||||||
|
"""Class to check if the authenticated user can send messages via dispatch."""
|
||||||
|
|
||||||
def has_permission(self, request, view):
|
def has_permission(self, request, view):
|
||||||
|
"""Check user permission for dispatch.send_message."""
|
||||||
if request.user.has_perm('dispatch.send_message'):
|
if request.user.has_perm('dispatch.send_message'):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@ -27,7 +29,6 @@ class HasSendMessagePermission(IsAuthenticated):
|
|||||||
|
|
||||||
|
|
||||||
class DispatcherList(generics.ListAPIView):
|
class DispatcherList(generics.ListAPIView):
|
||||||
|
|
||||||
"""List all dispatchers."""
|
"""List all dispatchers."""
|
||||||
|
|
||||||
queryset = Dispatcher.objects.all()
|
queryset = Dispatcher.objects.all()
|
||||||
@ -35,7 +36,6 @@ class DispatcherList(generics.ListAPIView):
|
|||||||
|
|
||||||
|
|
||||||
class DispatcherDetail(generics.RetrieveAPIView):
|
class DispatcherDetail(generics.RetrieveAPIView):
|
||||||
|
|
||||||
"""Detail the given dispatcher."""
|
"""Detail the given dispatcher."""
|
||||||
|
|
||||||
queryset = Dispatcher.objects.all()
|
queryset = Dispatcher.objects.all()
|
||||||
@ -43,12 +43,12 @@ class DispatcherDetail(generics.RetrieveAPIView):
|
|||||||
|
|
||||||
|
|
||||||
class DispatcherDetailByKey(DispatcherDetail):
|
class DispatcherDetailByKey(DispatcherDetail):
|
||||||
|
"""Detail a specific dispatcher."""
|
||||||
|
|
||||||
lookup_field = 'key'
|
lookup_field = 'key'
|
||||||
|
|
||||||
|
|
||||||
class DispatchMessage(generics.GenericAPIView):
|
class DispatchMessage(generics.GenericAPIView):
|
||||||
|
|
||||||
"""Send a message to the given dispatcher."""
|
"""Send a message to the given dispatcher."""
|
||||||
|
|
||||||
permission_classes = (HasSendMessagePermission,)
|
permission_classes = (HasSendMessagePermission,)
|
||||||
@ -57,12 +57,13 @@ class DispatchMessage(generics.GenericAPIView):
|
|||||||
serializer_class = DispatchMessageSerializer
|
serializer_class = DispatchMessageSerializer
|
||||||
|
|
||||||
def get(self, request, *args, **kwargs):
|
def get(self, request, *args, **kwargs):
|
||||||
dispatcher = self.get_object()
|
"""Return a default message, since this needs POST."""
|
||||||
data = {'message': "", 'status': "READY"}
|
data = {'message': "", 'status': "READY"}
|
||||||
message = self.serializer_class(data=data)
|
message = self.serializer_class(data=data)
|
||||||
return Response(message.initial_data)
|
return Response(message.initial_data)
|
||||||
|
|
||||||
def post(self, request, *args, **kwargs):
|
def post(self, request, *args, **kwargs):
|
||||||
|
"""Accept and dispatch a provided message."""
|
||||||
dispatcher = self.get_object()
|
dispatcher = self.get_object()
|
||||||
message = self.serializer_class(data=request.data)
|
message = self.serializer_class(data=request.data)
|
||||||
if message.is_valid():
|
if message.is_valid():
|
||||||
@ -101,12 +102,12 @@ class DispatchMessage(generics.GenericAPIView):
|
|||||||
|
|
||||||
|
|
||||||
class DispatchMessageByKey(DispatchMessage):
|
class DispatchMessageByKey(DispatchMessage):
|
||||||
|
"""Dispatch a message for a specific key."""
|
||||||
|
|
||||||
lookup_field = 'key'
|
lookup_field = 'key'
|
||||||
|
|
||||||
|
|
||||||
class DispatcherActionList(generics.ListAPIView):
|
class DispatcherActionList(generics.ListAPIView):
|
||||||
|
|
||||||
"""List all dispatchers."""
|
"""List all dispatchers."""
|
||||||
|
|
||||||
queryset = DispatcherAction.objects.all()
|
queryset = DispatcherAction.objects.all()
|
||||||
@ -114,7 +115,6 @@ class DispatcherActionList(generics.ListAPIView):
|
|||||||
|
|
||||||
|
|
||||||
class DispatcherActionDetail(generics.RetrieveAPIView):
|
class DispatcherActionDetail(generics.RetrieveAPIView):
|
||||||
|
|
||||||
"""Detail the given dispatcher."""
|
"""Detail the given dispatcher."""
|
||||||
|
|
||||||
queryset = DispatcherAction.objects.all()
|
queryset = DispatcherAction.objects.all()
|
||||||
|
Loading…
Reference in New Issue
Block a user