dispatch: use generics better (?) in /message
use GenericAPIView rather than APIView in order to get some common idiom stuff done for us. we continue to support GET and POST in DispatchMessage, now it's just a bit cleaner. i think. anyway if we were doing this stuff more we could/should probably create mixins for them, but at the moment this is pretty sane i think
This commit is contained in:
parent
482870621b
commit
dac7999c0f
@ -15,3 +15,4 @@ class DispatcherSerializer(serializers.ModelSerializer):
|
|||||||
class DispatchMessageSerializer(serializers.Serializer):
|
class DispatchMessageSerializer(serializers.Serializer):
|
||||||
|
|
||||||
message = serializers.CharField()
|
message = serializers.CharField()
|
||||||
|
status = serializers.CharField(read_only=True)
|
||||||
|
@ -2,12 +2,14 @@
|
|||||||
|
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
import copy
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import xmlrpclib
|
import xmlrpclib
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.http import Http404
|
from django.http import Http404
|
||||||
|
from django.shortcuts import get_object_or_404
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework import generics, status
|
from rest_framework import generics, status
|
||||||
@ -35,38 +37,43 @@ class DispatcherDetail(generics.RetrieveAPIView):
|
|||||||
serializer_class = DispatcherSerializer
|
serializer_class = DispatcherSerializer
|
||||||
|
|
||||||
|
|
||||||
class DispatchMessage(APIView):
|
class DispatchMessage(generics.GenericAPIView):
|
||||||
|
|
||||||
"""Send a message to the given dispatcher."""
|
"""Send a message to the given dispatcher."""
|
||||||
|
|
||||||
queryset = Dispatcher.objects.none()
|
queryset = Dispatcher.objects.all()
|
||||||
|
serializer_class = DispatchMessageSerializer
|
||||||
|
|
||||||
def get_object(self, pk):
|
def get_object(self):
|
||||||
try:
|
queryset = self.get_queryset()
|
||||||
return Dispatcher.objects.get(pk=pk)
|
queryset = self.filter_queryset(queryset)
|
||||||
except Dispatcher.DoesNotExist:
|
return get_object_or_404(queryset)
|
||||||
raise Http404
|
|
||||||
|
|
||||||
def get(self, request, pk, format=None):
|
def get(self, request, *args, **kwargs):
|
||||||
dispatcher = self.get_object(pk)
|
dispatcher = self.get_object()
|
||||||
return Response({'message': ""})
|
data = {'status': "READY"}
|
||||||
|
message = self.serializer_class(data=data)
|
||||||
|
return Response(message.initial_data)
|
||||||
|
|
||||||
def post(self, request, pk, format=None):
|
def post(self, request, *args, **kwargs):
|
||||||
dispatcher = self.get_object(pk)
|
dispatcher = self.get_object()
|
||||||
serializer = DispatchMessageSerializer(data=request.data)
|
message = self.serializer_class(data=request.data)
|
||||||
if serializer.is_valid():
|
if message.is_valid():
|
||||||
if dispatcher.type == Dispatcher.PRIVMSG_TYPE:
|
if dispatcher.type == Dispatcher.PRIVMSG_TYPE:
|
||||||
bot_url = 'http://{0:s}:{1:d}/'.format(settings.IRCBOT_XMLRPC_HOST, settings.IRCBOT_XMLRPC_PORT)
|
bot_url = 'http://{0:s}:{1:d}/'.format(settings.IRCBOT_XMLRPC_HOST, settings.IRCBOT_XMLRPC_PORT)
|
||||||
bot = xmlrpclib.ServerProxy(bot_url)
|
bot = xmlrpclib.ServerProxy(bot_url)
|
||||||
log.debug("sending '%s' to channel %s", serializer.data['message'], dispatcher.destination)
|
log.debug("sending '%s' to channel %s", message.data['message'], dispatcher.destination)
|
||||||
bot.privmsg(dispatcher.destination, serializer.data['message'])
|
bot.privmsg(dispatcher.destination, message.data['message'])
|
||||||
return Response({'status': "OK"})
|
|
||||||
elif dispatcher.type == Dispatcher.FILE_TYPE:
|
elif dispatcher.type == Dispatcher.FILE_TYPE:
|
||||||
filename = os.path.abspath(dispatcher.destination)
|
filename = os.path.abspath(dispatcher.destination)
|
||||||
log.debug("sending '%s' to file %s", serializer.data['message'], filename)
|
log.debug("sending '%s' to file %s", message.data['message'], filename)
|
||||||
with open(filename, 'w') as f:
|
with open(filename, 'w') as f:
|
||||||
f.write(serializer.data['message'])
|
f.write(message.data['message'])
|
||||||
f.write('\n')
|
f.write('\n')
|
||||||
return Response({'status': "OK"})
|
|
||||||
|
|
||||||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
new_data = copy.deepcopy(message.data)
|
||||||
|
new_data['status'] = "OK"
|
||||||
|
new_message = self.serializer_class(data=new_data)
|
||||||
|
return Response(new_message.initial_data)
|
||||||
|
|
||||||
|
return Response(message.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
Loading…
Reference in New Issue
Block a user