dispatch: have option to include key in output

This commit is contained in:
Brian S. Stephan 2015-06-20 11:22:56 -05:00
parent 98bb608291
commit f542a862b8
3 changed files with 39 additions and 5 deletions

View File

@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('dispatch', '0003_auto_20150619_1637'),
]
operations = [
migrations.AddField(
model_name='dispatcheraction',
name='include_key',
field=models.BooleanField(default=False),
),
]

View File

@ -42,6 +42,7 @@ class DispatcherAction(models.Model):
dispatcher = models.ForeignKey('Dispatcher', related_name='actions')
type = models.CharField(max_length=16, choices=TYPE_CHOICES)
destination = models.CharField(max_length=200)
include_key = models.BooleanField(default=False)
def __unicode__(self):
"""String representation."""

View File

@ -70,14 +70,28 @@ class DispatchMessage(generics.GenericAPIView):
message = self.serializer_class(data=request.data)
if message.is_valid():
for action in dispatcher.actions.all():
# manipulate the message if desired
if action.include_key:
text = "[{0:s}] {1:s}".format(action.dispatcher.key, message.data['message'])
else:
text = message.data['message']
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'])
# connect over XML-RPC and send
try:
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", text, action.destination)
bot.privmsg(action.destination, message.data['message'])
except Exception as e:
new_data = copy.deepcopy(message.data)
new_data['status'] = "FAILED - {0:s}".format(e)
new_message = self.serializer_class(data=new_data)
return Response(new_message.initial_data)
elif action.type == DispatcherAction.FILE_TYPE:
# write to file
filename = os.path.abspath(action.destination)
log.debug("sending '%s' to file %s", message.data['message'], filename)
log.debug("sending '%s' to file %s", text, filename)
with open(filename, 'w') as f:
f.write(message.data['message'])
f.write(b'\n')