From a16bd64701f035c32c32fb8f4e5db0c4482147ca Mon Sep 17 00:00:00 2001 From: Michael Ziegler Date: Fri, 2 Jul 2010 18:41:00 +0200 Subject: [PATCH] finish ext.direct provider integration and move stuff to DirectStores --- pyweb/extdirect.py | 24 +++++++++++++++--------- pyweb/mumble/urls.py | 5 ++++- pyweb/mumble/views.py | 40 ++++++---------------------------------- 3 files changed, 25 insertions(+), 44 deletions(-) diff --git a/pyweb/extdirect.py b/pyweb/extdirect.py index 66ed193..9ef83fc 100644 --- a/pyweb/extdirect.py +++ b/pyweb/extdirect.py @@ -40,6 +40,7 @@ class Provider( object ): response and exceptions - if any. Instantiation: + >>> EXT_JS_PROVIDER = Provider( [name="Ext.app.REMOTING_API", autoadd=True] ) If autoadd is True, the api.js will include a line like such: @@ -48,9 +49,9 @@ class Provider( object ): After instantiating the Provider, register functions to it like so: >>> @EXT_JS_PROVIDER.register_method("myclass") - def myview( request, possibly, some, other, arguments ): - " does something with all those args and returns something " - return 13.37 + ... def myview( request, possibly, some, other, arguments ): + ... " does something with all those args and returns something " + ... return 13.37 Note that those views **MUST NOT** return an HttpResponse but simply the plain result, as the Provider will build a response from whatever @@ -61,13 +62,14 @@ class Provider( object ): >>> from views import EXT_JS_PROVIDER # import our provider instance >>> urlpatterns = patterns( - # other patterns go here - ( r'api/', include(EXT_DIRECT_PROVIDER.urls) ), - ) + ... # other patterns go here + ... ( r'api/', include(EXT_DIRECT_PROVIDER.urls) ), + ... ) This way, the Provider will define the URLs "api/api.js" and "api/router". - If you then access the "api/api.js" URL, you will get a response such as: + If you then access the "api/api.js" URL, you will get a response such as:: + Ext.app.REMOTING_API = { # Ext.app.REMOTING_API is from Provider.name "url": "/mumble/api/router", "type": "remoting", @@ -83,7 +85,12 @@ class Provider( object ): self.classes = {} def register_method( self, cls_or_name ): - """ Return a function that takes a method as an argument and adds that to cls_or_name. """ + """ Return a function that takes a method as an argument and adds that + to cls_or_name. + + Note: This decorator does not replace the method by a new function, + it returns the original function as-is. + """ clsname = getname(cls_or_name) if clsname not in self.classes: self.classes[clsname] = {} @@ -221,4 +228,3 @@ class Provider( object ): (r'api.js$', self.get_api ), (r'router/?', self.request ), ) - diff --git a/pyweb/mumble/urls.py b/pyweb/mumble/urls.py index cd40e07..9c63988 100644 --- a/pyweb/mumble/urls.py +++ b/pyweb/mumble/urls.py @@ -15,13 +15,16 @@ * GNU General Public License for more details. """ -from django.conf.urls.defaults import patterns +from django.conf.urls.defaults import patterns, include +from views import EXT_DIRECT_PROVIDER urlpatterns = patterns( 'mumble.views', ( r'djangousers', 'djangousers' ), ( r'(?P\d+)/users', 'users' ), + ( r'api/', include(EXT_DIRECT_PROVIDER.urls) ), + ( r'(?P\d+)/(?P\d+)/texture.png', 'showTexture' ), ( r'murmur/tree/(?P\d+)', 'mmng_tree' ), diff --git a/pyweb/mumble/views.py b/pyweb/mumble/views.py index fea567e..2f562d9 100644 --- a/pyweb/mumble/views.py +++ b/pyweb/mumble/views.py @@ -34,7 +34,7 @@ from forms import MumbleUserLinkForm, MumbleTextureForm, MumbleKickForm from extdirect import Provider -EXT_DIRECT_PROVIDER = Provider( "/mumble/ext" ) +EXT_DIRECT_PROVIDER = Provider() @EXT_DIRECT_PROVIDER.register_method( "omgfu" ) def ohai( request ): @@ -257,6 +257,7 @@ def showTexture( request, server, userid ): @login_required +@EXT_DIRECT_PROVIDER.register_method( "Mumble" ) def users( request, server ): """ Create a list of MumbleUsers for a given server serialized as a JSON object. @@ -269,30 +270,7 @@ def users( request, server ): srv.readUsersFromMurmur() if not srv.isUserAdmin( request.user ): - return HttpResponse( - simplejson.dumps({ 'success': False, 'objects': [], 'errormsg': 'Access denied' }), - mimetype='text/javascript' - ) - - if request.method == 'POST': - data = simplejson.loads( request.POST['data'] ) - for record in data: - if record['id'] == -1: - if record['delete']: - continue - mu = MumbleUser( server=srv ) - else: - mu = MumbleUser.objects.get( id=record['id'] ) - if record['delete']: - mu.delete() - continue - - mu.name = record['name'] - mu.password = record['password'] - if record['owner']: - mu.owner = User.objects.get( id=int(record['owner']) ) - mu.save() - mu.aclAdmin = record['admin'] + raise Exception( 'Access denied' ) users = [] for mu in srv.mumbleuser_set.all(): @@ -308,13 +286,10 @@ def users( request, server ): 'admin': mu.aclAdmin, } ) - return HttpResponse( - simplejson.dumps( { 'success': True, 'objects': users } ), - mimetype='text/javascript' - ) - + return users @login_required +@EXT_DIRECT_PROVIDER.register_method( "Mumble" ) def djangousers( request ): """ Return a list of all Django users' names and IDs. """ @@ -325,10 +300,7 @@ def djangousers( request ): 'uname': unicode( du ), } ) - return HttpResponse( - simplejson.dumps( { 'success': True, 'objects': users } ), - mimetype='text/javascript' - ) + return users def mmng_tree( request, server ):