2015-06-18 23:57:43 -05:00
|
|
|
"""Handle dispatcher API requests."""
|
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2015-06-19 10:51:11 -05:00
|
|
|
import copy
|
2015-06-18 23:57:43 -05:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import xmlrpclib
|
|
|
|
|
|
|
|
from django.conf import settings
|
2015-06-19 10:51:11 -05:00
|
|
|
from django.shortcuts import get_object_or_404
|
2015-06-18 23:57:43 -05:00
|
|
|
from rest_framework.response import Response
|
|
|
|
from rest_framework import generics, status
|
|
|
|
|
2015-06-19 11:29:00 -05:00
|
|
|
from dispatch.models import Dispatcher, DispatcherAction
|
|
|
|
from dispatch.serializers import DispatchMessageSerializer, DispatcherSerializer, DispatcherActionSerializer
|
2015-06-18 23:57:43 -05:00
|
|
|
|
|
|
|
|
|
|
|
log = logging.getLogger('dispatch.views')
|
|
|
|
|
|
|
|
|
|
|
|
class DispatcherList(generics.ListAPIView):
|
|
|
|
|
|
|
|
"""List all dispatchers."""
|
|
|
|
|
|
|
|
queryset = Dispatcher.objects.all()
|
|
|
|
serializer_class = DispatcherSerializer
|
|
|
|
|
|
|
|
|
|
|
|
class DispatcherDetail(generics.RetrieveAPIView):
|
|
|
|
|
|
|
|
"""Detail the given dispatcher."""
|
|
|
|
|
|
|
|
queryset = Dispatcher.objects.all()
|
|
|
|
serializer_class = DispatcherSerializer
|
|
|
|
|
|
|
|
|
2015-06-19 10:51:11 -05:00
|
|
|
class DispatchMessage(generics.GenericAPIView):
|
2015-06-18 23:57:43 -05:00
|
|
|
|
|
|
|
"""Send a message to the given dispatcher."""
|
|
|
|
|
2015-06-19 10:51:11 -05:00
|
|
|
queryset = Dispatcher.objects.all()
|
|
|
|
serializer_class = DispatchMessageSerializer
|
|
|
|
|
|
|
|
def get_object(self):
|
|
|
|
queryset = self.get_queryset()
|
|
|
|
queryset = self.filter_queryset(queryset)
|
|
|
|
return get_object_or_404(queryset)
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
dispatcher = self.get_object()
|
2015-06-19 15:10:21 -05:00
|
|
|
data = {'message': "", 'status': "READY"}
|
2015-06-19 10:51:11 -05:00
|
|
|
message = self.serializer_class(data=data)
|
|
|
|
return Response(message.initial_data)
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
dispatcher = self.get_object()
|
|
|
|
message = self.serializer_class(data=request.data)
|
|
|
|
if message.is_valid():
|
2015-06-19 15:08:57 -05:00
|
|
|
for action in dispatcher.actions.all():
|
2015-06-19 11:29:00 -05:00
|
|
|
if action.type == DispatcherAction.PRIVMSG_TYPE:
|
|
|
|
bot_url = 'http://{0:s}:{1:d}/'.format(settings.IRCBOT_XMLRPC_HOST, settings.IRCBOT_XMLRPC_PORT)
|
|
|
|
bot = xmlrpclib.ServerProxy(bot_url)
|
|
|
|
log.debug("sending '%s' to channel %s", message.data['message'], action.destination)
|
|
|
|
bot.privmsg(action.destination, message.data['message'])
|
|
|
|
elif action.type == DispatcherAction.FILE_TYPE:
|
|
|
|
filename = os.path.abspath(action.destination)
|
|
|
|
log.debug("sending '%s' to file %s", message.data['message'], filename)
|
|
|
|
with open(filename, 'w') as f:
|
|
|
|
f.write(message.data['message'])
|
|
|
|
f.write('\n')
|
2015-06-18 23:57:43 -05:00
|
|
|
|
2015-06-19 10:51:11 -05:00
|
|
|
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)
|
2015-06-19 11:29:00 -05:00
|
|
|
|
|
|
|
|
|
|
|
class DispatcherActionList(generics.ListAPIView):
|
|
|
|
|
|
|
|
"""List all dispatchers."""
|
|
|
|
|
|
|
|
queryset = DispatcherAction.objects.all()
|
|
|
|
serializer_class = DispatcherActionSerializer
|
|
|
|
|
|
|
|
|
|
|
|
class DispatcherActionDetail(generics.RetrieveAPIView):
|
|
|
|
|
|
|
|
"""Detail the given dispatcher."""
|
|
|
|
|
|
|
|
queryset = DispatcherAction.objects.all()
|
|
|
|
serializer_class = DispatcherActionSerializer
|