changeset 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 fc564c209899
children cb298c8eb039
files gpp/phantombrigade/__init__.py gpp/phantombrigade/models.py gpp/phantombrigade/urls.py gpp/phantombrigade/views.py gpp/settings/base.py gpp/urls.py
diffstat 5 files changed, 121 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gpp/phantombrigade/models.py	Thu Jan 12 21:42:01 2012 -0600
@@ -0,0 +1,3 @@
+from django.db import models
+
+# Create your models here.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gpp/phantombrigade/urls.py	Thu Jan 12 21:42:01 2012 -0600
@@ -0,0 +1,10 @@
+"""
+Url patterns for the phantombrigade application.
+
+"""
+from django.conf.urls.defaults import patterns, url
+
+
+urlpatterns = patterns('phantombrigade.views',
+   url(r'^ts3/$', 'ts3_query', name='phantombrigade-ts3'),
+)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gpp/phantombrigade/views.py	Thu Jan 12 21:42:01 2012 -0600
@@ -0,0 +1,101 @@
+"""
+Views for the phantombrigade application.
+
+The phantombrigade application doesn't have anything to do with SG101. It is
+simply some useful web services that we provide to the gaming clan Phantom
+Brigade. Rather than create a whole new website we just use the infrastructure
+of SG101.
+
+Current we provide a TeamSpeak 3 status view for the PhantomBrigade.us website.
+
+"""
+from django.conf import settings
+from django.core.cache import cache
+from django.http import HttpResponse, HttpResponseServerError
+from django.utils import simplejson
+import ts3
+
+
+CACHE_KEY = 'phantombrigade-ts3-json'
+CACHE_TIMEOUT = 2 * 60
+
+
+def ts3_query(request):
+    """
+    Query the TeamSpeak3 server for status, and output a JSON representation.
+
+    The JSON we return is targeted towards the jQuery plugin Dynatree
+    http://code.google.com/p/dynatree/
+
+    """
+    # Do we have the result cached?
+    result = cache.get(CACHE_KEY)
+    if result:
+        return HttpResponse(result, content_type='application/json')
+
+    # Cache miss, go query the remote server
+
+    try:
+        svr = ts3.TS3Server(settings.PB_TS3_IP, settings.PB_TS3_PORT,
+                settings.PB_TS3_VID)
+    except ts3.ConnectionError:
+        return HttpResponseServerError()
+
+    response = svr.send_command('serverinfo')
+    if response.response['msg'] != 'ok':
+        return HttpResponseServerError()
+    svr_info = response.data[0]
+
+    response = svr.send_command('channellist')
+    if response.response['msg'] != 'ok':
+        return HttpResponseServerError()
+    channel_list = response.data
+
+    response = svr.send_command('clientlist')
+    if response.response['msg'] != 'ok':
+        return HttpResponseServerError()
+    client_list = response.data
+
+    # Start building the channel / client tree.
+    # We save tree nodes in a dictionary, keyed by their id so we can find them
+    # later in order to support arbitrary channel hierarchies.
+    channels = {}
+
+    # Build the root, or channel 0
+    channels[0] = {
+        'title': svr_info['virtualserver_name'],
+        'isFolder': True,
+        'expand': True,
+        'children': []
+    }
+
+    # Add the channels to our tree
+
+    for channel in channel_list:
+        node = {
+            'title': channel['channel_name'],
+            'isFolder': True,
+            'expand': True,
+            'children': []
+        }
+        parent = channels[int(channel['pid'])]
+        parent['children'].append(node)
+        channels[int(channel['cid'])] = node
+
+    # Add the clients to the tree
+
+    for client in client_list:
+        if client['client_type'] == '0':
+            node = {
+                'title': client['client_nickname'],
+                'icon': 'client.png'
+            }
+            channel = channels[int(client['cid'])]
+            channel['children'].append(node)
+
+    tree = [channels[0]]
+    json = simplejson.dumps(tree)
+
+    cache.set(CACHE_KEY, json, CACHE_TIMEOUT)
+
+    return HttpResponse(json, content_type='application/json')
--- a/gpp/settings/base.py	Thu Jan 12 20:15:28 2012 -0600
+++ b/gpp/settings/base.py	Thu Jan 12 21:42:01 2012 -0600
@@ -138,6 +138,7 @@
     'messages',
     'news',
     'oembed',
+    'phantombrigade',
     'podcast',
     'polls',
     'potd',
@@ -276,6 +277,11 @@
 GOOGLE_OAUTH_CONSUMER_KEY = 'surfguitar101.com'
 GOOGLE_OAUTH_PRIVATE_KEY_PATH = SECRETS['GOOGLE_KEY_PATH']
 
+# Phantom Brigade TeamSpeak3 settings
+PB_TS3_IP = '206.123.95.194'
+PB_TS3_PORT = 10011
+PB_TS3_VID = 6113
+
 #######################################################################
 # Asynchronous settings (queues, queued_search, redis, celery, etc)
 #######################################################################
--- a/gpp/urls.py	Thu Jan 12 20:15:28 2012 -0600
+++ b/gpp/urls.py	Thu Jan 12 21:42:01 2012 -0600
@@ -42,6 +42,7 @@
    (r'^messages/', include('messages.urls')),
    (r'^news/', include('news.urls')),
    (r'^oembed/', include('oembed.urls')),
+   (r'^pb/', include('phantombrigade.urls')),
    (r'^podcast/', include('podcast.urls')),
    (r'^polls/', include('polls.urls')),
    (r'^potd/', include('potd.urls')),