annotate phantombrigade/views.py @ 640:560e4fe35bd3

Add a tag to choose slides for home slideshow.
author Brian Neal <bgneal@gmail.com>
date Sat, 09 Mar 2013 11:42:35 -0600
parents ee87ea74d46b
children 10f3acc18b2d
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@544 12 from django.conf import settings
bgneal@544 13 from django.core.cache import cache
bgneal@544 14 from django.http import HttpResponse, HttpResponseServerError
bgneal@544 15 from django.utils import simplejson
bgneal@544 16 import ts3
bgneal@544 17
bgneal@544 18
bgneal@544 19 CACHE_KEY = 'phantombrigade-ts3-json'
bgneal@544 20 CACHE_TIMEOUT = 2 * 60
bgneal@544 21
bgneal@544 22
bgneal@544 23 def ts3_query(request):
bgneal@544 24 """
bgneal@544 25 Query the TeamSpeak3 server for status, and output a JSON representation.
bgneal@544 26
bgneal@544 27 The JSON we return is targeted towards the jQuery plugin Dynatree
bgneal@544 28 http://code.google.com/p/dynatree/
bgneal@544 29
bgneal@544 30 """
bgneal@544 31 # Do we have the result cached?
bgneal@544 32 result = cache.get(CACHE_KEY)
bgneal@544 33 if result:
bgneal@544 34 return HttpResponse(result, content_type='application/json')
bgneal@544 35
bgneal@544 36 # Cache miss, go query the remote server
bgneal@544 37
bgneal@544 38 try:
bgneal@544 39 svr = ts3.TS3Server(settings.PB_TS3_IP, settings.PB_TS3_PORT,
bgneal@544 40 settings.PB_TS3_VID)
bgneal@544 41 except ts3.ConnectionError:
bgneal@544 42 return HttpResponseServerError()
bgneal@544 43
bgneal@544 44 response = svr.send_command('serverinfo')
bgneal@544 45 if response.response['msg'] != 'ok':
bgneal@544 46 return HttpResponseServerError()
bgneal@544 47 svr_info = response.data[0]
bgneal@544 48
bgneal@544 49 response = svr.send_command('channellist')
bgneal@544 50 if response.response['msg'] != 'ok':
bgneal@544 51 return HttpResponseServerError()
bgneal@544 52 channel_list = response.data
bgneal@544 53
bgneal@544 54 response = svr.send_command('clientlist')
bgneal@544 55 if response.response['msg'] != 'ok':
bgneal@544 56 return HttpResponseServerError()
bgneal@544 57 client_list = response.data
bgneal@544 58
bgneal@544 59 # Start building the channel / client tree.
bgneal@544 60 # We save tree nodes in a dictionary, keyed by their id so we can find them
bgneal@544 61 # later in order to support arbitrary channel hierarchies.
bgneal@544 62 channels = {}
bgneal@544 63
bgneal@544 64 # Build the root, or channel 0
bgneal@544 65 channels[0] = {
bgneal@544 66 'title': svr_info['virtualserver_name'],
bgneal@544 67 'isFolder': True,
bgneal@544 68 'expand': True,
bgneal@544 69 'children': []
bgneal@544 70 }
bgneal@544 71
bgneal@544 72 # Add the channels to our tree
bgneal@544 73
bgneal@544 74 for channel in channel_list:
bgneal@544 75 node = {
bgneal@544 76 'title': channel['channel_name'],
bgneal@544 77 'isFolder': True,
bgneal@544 78 'expand': True,
bgneal@544 79 'children': []
bgneal@544 80 }
bgneal@544 81 parent = channels[int(channel['pid'])]
bgneal@544 82 parent['children'].append(node)
bgneal@544 83 channels[int(channel['cid'])] = node
bgneal@544 84
bgneal@544 85 # Add the clients to the tree
bgneal@544 86
bgneal@544 87 for client in client_list:
bgneal@544 88 if client['client_type'] == '0':
bgneal@544 89 node = {
bgneal@544 90 'title': client['client_nickname'],
bgneal@544 91 'icon': 'client.png'
bgneal@544 92 }
bgneal@544 93 channel = channels[int(client['cid'])]
bgneal@544 94 channel['children'].append(node)
bgneal@544 95
bgneal@544 96 tree = [channels[0]]
bgneal@544 97 json = simplejson.dumps(tree)
bgneal@544 98
bgneal@544 99 cache.set(CACHE_KEY, json, CACHE_TIMEOUT)
bgneal@544 100
bgneal@544 101 return HttpResponse(json, content_type='application/json')