Browse Source

finish ext.direct provider integration and move stuff to DirectStores

Natenom/support-murmur-13-1446181288462
Michael Ziegler 15 years ago
parent
commit
a16bd64701
  1. 24
      pyweb/extdirect.py
  2. 5
      pyweb/mumble/urls.py
  3. 40
      pyweb/mumble/views.py

24
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 ),
)

5
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<server>\d+)/users', 'users' ),
( r'api/', include(EXT_DIRECT_PROVIDER.urls) ),
( r'(?P<server>\d+)/(?P<userid>\d+)/texture.png', 'showTexture' ),
( r'murmur/tree/(?P<server>\d+)', 'mmng_tree' ),

40
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 ):

Loading…
Cancel
Save