comparison gpp/phantombrigade/views.py @ 544:c092efc9ce71

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