diff --git a/pyweb/mumble/mmobjects.py b/pyweb/mumble/mmobjects.py index 9caf33e..0427ebe 100644 --- a/pyweb/mumble/mmobjects.py +++ b/pyweb/mumble/mmobjects.py @@ -149,7 +149,8 @@ class mmChannel( object ): chandata['subchans'] = [ sc.asDict() for sc in self.subchans ]; return chandata; - def asXml( self, parentnode ): + def asMvXml( self, parentnode ): + """ Return an XML tree for this channel suitable for MumbleViewer-ng. """ from xml.etree.cElementTree import SubElement me = SubElement( parentnode, "item" , id=self.id, rel='channel' ) content = SubElement( me, "content" ) @@ -157,9 +158,23 @@ class mmChannel( object ): name.text = self.name for sc in self.subchans: - sc.asXml(me) + sc.asMvXml(me) for pl in self.players: - pl.asXml(me) + pl.asMvXml(me) + + def asMvJson( self ): + """ Return a Dict for this channel suitable for MumbleViewer-ng. """ + return { + "attributes": { + "href": self.connecturl, + "id": self.id, + "rel": "channel", + }, + 'data': self.name, + 'children': [ sc.asMvJson() for sc in self.subchans ] + \ + [ pl.asMvJson() for pl in self.players ], + 'state': { False: "closed", True: "open" }[self.top_or_not_empty], + } @@ -247,12 +262,23 @@ class mmPlayer( object ): return pldata; - def asXml( self, parentnode ): + def asMvXml( self, parentnode ): + """ Return an XML node for this player suitable for MumbleViewer-ng. """ from xml.etree.cElementTree import SubElement me = SubElement( parentnode, "item" , id=self.id, rel='user' ) content = SubElement( me, "content" ) name = SubElement( content , "name" ) name.text = self.name + + def asMvJson( self ): + """ Return a Dict for this player suitable for MumbleViewer-ng. """ + return { + "attributes": { + "id": self.id, + "rel": "user", + }, + 'data': self.name, + } diff --git a/pyweb/mumble/models.py b/pyweb/mumble/models.py index 58b62f7..234b726 100644 --- a/pyweb/mumble/models.py +++ b/pyweb/mumble/models.py @@ -492,12 +492,17 @@ class Mumble( models.Model ): 'root': self.rootchan.asDict() }; - def asXml( self ): + def asMvXml( self ): + """ Return an XML tree for this server suitable for MumbleViewer-ng. """ from xml.etree.cElementTree import Element root = Element("root") - self.rootchan.asXml(root) + self.rootchan.asMvXml(root) return root + def asMvJson( self ): + """ Return a Dict for this server suitable for MumbleViewer-ng. """ + return self.rootchan.asMvJson() + # "server" field protection def __setattr__( self, name, value ): if name == 'server': diff --git a/pyweb/mumble/urls.py b/pyweb/mumble/urls.py index a94a9fd..36fbb11 100644 --- a/pyweb/mumble/urls.py +++ b/pyweb/mumble/urls.py @@ -26,6 +26,7 @@ urlpatterns = patterns( ( r'murmur/tree/(?P\d+)', 'mmng_tree' ), ( r'mumbleviewer/(?P\d+).xml', 'mumbleviewer_tree_xml' ), + ( r'mumbleviewer/(?P\d+).json', 'mumbleviewer_tree_json'), ( r'mobile/(?P\d+)', 'mobile_show' ), ( r'mobile/?$', 'mobile_mumbles' ), diff --git a/pyweb/mumble/views.py b/pyweb/mumble/views.py index 26bba96..cfef7b8 100644 --- a/pyweb/mumble/views.py +++ b/pyweb/mumble/views.py @@ -392,10 +392,24 @@ def mmng_tree( request, server ): def mumbleviewer_tree_xml( request, server ): + """ Get the XML tree from the server and serialize it to the client. """ from xml.etree.cElementTree import tostring as xml_to_string srv = get_object_or_404( Mumble, id=int(server) ); return HttpResponse( - xml_to_string( srv.asXml(), encoding='utf-8' ), + xml_to_string( srv.asMvXml(), encoding='utf-8' ), mimetype='text/xml' ); +def mumbleviewer_tree_json( request, server ): + """ Get the Dict from the server and serialize it as JSON to the client. """ + srv = get_object_or_404( Mumble, id=int(server) ); + + if "jsonp_callback" in request.GET: + prefix = request.GET["jsonp_callback"] + else: + prefix = "" + + return HttpResponse( + prefix + "(" + simplejson.dumps( srv.asMvJson() ) + ")", + mimetype='text/javascript' + );