bgneal@544: """ bgneal@544: Views for the phantombrigade application. bgneal@544: bgneal@544: The phantombrigade application doesn't have anything to do with SG101. It is bgneal@544: simply some useful web services that we provide to the gaming clan Phantom bgneal@544: Brigade. Rather than create a whole new website we just use the infrastructure bgneal@544: of SG101. bgneal@544: bgneal@544: Current we provide a TeamSpeak 3 status view for the PhantomBrigade.us website. bgneal@544: bgneal@544: """ bgneal@544: from django.conf import settings bgneal@544: from django.core.cache import cache bgneal@544: from django.http import HttpResponse, HttpResponseServerError bgneal@544: from django.utils import simplejson bgneal@544: import ts3 bgneal@544: bgneal@544: bgneal@544: CACHE_KEY = 'phantombrigade-ts3-json' bgneal@544: CACHE_TIMEOUT = 2 * 60 bgneal@544: bgneal@544: bgneal@544: def ts3_query(request): bgneal@544: """ bgneal@544: Query the TeamSpeak3 server for status, and output a JSON representation. bgneal@544: bgneal@544: The JSON we return is targeted towards the jQuery plugin Dynatree bgneal@544: http://code.google.com/p/dynatree/ bgneal@544: bgneal@544: """ bgneal@544: # Do we have the result cached? bgneal@544: result = cache.get(CACHE_KEY) bgneal@544: if result: bgneal@544: return HttpResponse(result, content_type='application/json') bgneal@544: bgneal@544: # Cache miss, go query the remote server bgneal@544: bgneal@544: try: bgneal@544: svr = ts3.TS3Server(settings.PB_TS3_IP, settings.PB_TS3_PORT, bgneal@544: settings.PB_TS3_VID) bgneal@544: except ts3.ConnectionError: bgneal@544: return HttpResponseServerError() bgneal@544: bgneal@544: response = svr.send_command('serverinfo') bgneal@544: if response.response['msg'] != 'ok': bgneal@544: return HttpResponseServerError() bgneal@544: svr_info = response.data[0] bgneal@544: bgneal@544: response = svr.send_command('channellist') bgneal@544: if response.response['msg'] != 'ok': bgneal@544: return HttpResponseServerError() bgneal@544: channel_list = response.data bgneal@544: bgneal@544: response = svr.send_command('clientlist') bgneal@544: if response.response['msg'] != 'ok': bgneal@544: return HttpResponseServerError() bgneal@544: client_list = response.data bgneal@544: bgneal@544: # Start building the channel / client tree. bgneal@544: # We save tree nodes in a dictionary, keyed by their id so we can find them bgneal@544: # later in order to support arbitrary channel hierarchies. bgneal@544: channels = {} bgneal@544: bgneal@544: # Build the root, or channel 0 bgneal@544: channels[0] = { bgneal@544: 'title': svr_info['virtualserver_name'], bgneal@544: 'isFolder': True, bgneal@544: 'expand': True, bgneal@544: 'children': [] bgneal@544: } bgneal@544: bgneal@544: # Add the channels to our tree bgneal@544: bgneal@544: for channel in channel_list: bgneal@544: node = { bgneal@544: 'title': channel['channel_name'], bgneal@544: 'isFolder': True, bgneal@544: 'expand': True, bgneal@544: 'children': [] bgneal@544: } bgneal@544: parent = channels[int(channel['pid'])] bgneal@544: parent['children'].append(node) bgneal@544: channels[int(channel['cid'])] = node bgneal@544: bgneal@544: # Add the clients to the tree bgneal@544: bgneal@544: for client in client_list: bgneal@544: if client['client_type'] == '0': bgneal@544: node = { bgneal@544: 'title': client['client_nickname'], bgneal@544: 'icon': 'client.png' bgneal@544: } bgneal@544: channel = channels[int(client['cid'])] bgneal@544: channel['children'].append(node) bgneal@544: bgneal@544: tree = [channels[0]] bgneal@544: json = simplejson.dumps(tree) bgneal@544: bgneal@544: cache.set(CACHE_KEY, json, CACHE_TIMEOUT) bgneal@544: bgneal@544: return HttpResponse(json, content_type='application/json')