annotate phantombrigade/views.py @ 733:18e42866b34d

For #56, default to anonymous radio button on donations form. Also improve the donations admin page with a date hierarchy and other fields on the list display.
author Brian Neal <bgneal@gmail.com>
date Thu, 24 Oct 2013 21:03:03 -0500
parents 89b240fe9297
children
rev   line source
bgneal@544 1 """
bgneal@544 2 Views for the phantombrigade application.
bgneal@544 3
bgneal@544 4 The phantombrigade application doesn't have anything to do with SG101. It is
bgneal@544 5 simply some useful web services that we provide to the gaming clan Phantom
bgneal@544 6 Brigade. Rather than create a whole new website we just use the infrastructure
bgneal@544 7 of SG101.
bgneal@544 8
bgneal@544 9 Current we provide a TeamSpeak 3 status view for the PhantomBrigade.us website.
bgneal@544 10
bgneal@544 11 """
bgneal@679 12 import json
bgneal@679 13
bgneal@544 14 from django.conf import settings
bgneal@544 15 from django.core.cache import cache
bgneal@544 16 from django.http import HttpResponse, HttpResponseServerError
bgneal@544 17 import ts3
bgneal@544 18
bgneal@544 19
bgneal@544 20 CACHE_KEY = 'phantombrigade-ts3-json'
bgneal@544 21 CACHE_TIMEOUT = 2 * 60
bgneal@544 22
bgneal@544 23
bgneal@544 24 def ts3_query(request):
bgneal@544 25 """
bgneal@544 26 Query the TeamSpeak3 server for status, and output a JSON representation.
bgneal@544 27
bgneal@544 28 The JSON we return is targeted towards the jQuery plugin Dynatree
bgneal@544 29 http://code.google.com/p/dynatree/
bgneal@544 30
bgneal@544 31 """
bgneal@544 32 # Do we have the result cached?
bgneal@544 33 result = cache.get(CACHE_KEY)
bgneal@544 34 if result:
bgneal@544 35 return HttpResponse(result, content_type='application/json')
bgneal@544 36
bgneal@544 37 # Cache miss, go query the remote server
bgneal@544 38
bgneal@544 39 try:
bgneal@544 40 svr = ts3.TS3Server(settings.PB_TS3_IP, settings.PB_TS3_PORT,
bgneal@544 41 settings.PB_TS3_VID)
bgneal@544 42 except ts3.ConnectionError:
bgneal@544 43 return HttpResponseServerError()
bgneal@544 44
bgneal@544 45 response = svr.send_command('serverinfo')
bgneal@648 46 if response.response.get('msg') != 'ok':
bgneal@544 47 return HttpResponseServerError()
bgneal@544 48 svr_info = response.data[0]
bgneal@544 49
bgneal@544 50 response = svr.send_command('channellist')
bgneal@648 51 if response.response.get('msg') != 'ok':
bgneal@544 52 return HttpResponseServerError()
bgneal@544 53 channel_list = response.data
bgneal@544 54
bgneal@544 55 response = svr.send_command('clientlist')
bgneal@648 56 if response.response.get('msg') != 'ok':
bgneal@544 57 return HttpResponseServerError()
bgneal@544 58 client_list = response.data
bgneal@544 59
bgneal@544 60 # Start building the channel / client tree.
bgneal@544 61 # We save tree nodes in a dictionary, keyed by their id so we can find them
bgneal@544 62 # later in order to support arbitrary channel hierarchies.
bgneal@544 63 channels = {}
bgneal@544 64
bgneal@544 65 # Build the root, or channel 0
bgneal@544 66 channels[0] = {
bgneal@544 67 'title': svr_info['virtualserver_name'],
bgneal@544 68 'isFolder': True,
bgneal@544 69 'expand': True,
bgneal@544 70 'children': []
bgneal@544 71 }
bgneal@544 72
bgneal@544 73 # Add the channels to our tree
bgneal@544 74
bgneal@544 75 for channel in channel_list:
bgneal@544 76 node = {
bgneal@544 77 'title': channel['channel_name'],
bgneal@544 78 'isFolder': True,
bgneal@544 79 'expand': True,
bgneal@544 80 'children': []
bgneal@544 81 }
bgneal@544 82 parent = channels[int(channel['pid'])]
bgneal@544 83 parent['children'].append(node)
bgneal@544 84 channels[int(channel['cid'])] = node
bgneal@544 85
bgneal@544 86 # Add the clients to the tree
bgneal@544 87
bgneal@544 88 for client in client_list:
bgneal@544 89 if client['client_type'] == '0':
bgneal@544 90 node = {
bgneal@544 91 'title': client['client_nickname'],
bgneal@544 92 'icon': 'client.png'
bgneal@544 93 }
bgneal@544 94 channel = channels[int(client['cid'])]
bgneal@544 95 channel['children'].append(node)
bgneal@544 96
bgneal@544 97 tree = [channels[0]]
bgneal@679 98 status = json.dumps(tree)
bgneal@544 99
bgneal@679 100 cache.set(CACHE_KEY, status, CACHE_TIMEOUT)
bgneal@544 101
bgneal@679 102 return HttpResponse(status, content_type='application/json')