Merge branch 'gilbert' of git://github.com/lapilofu/philo into gilbert
[philo.git] / contrib / gilbert / sites.py
index b3f0df2..8f666f9 100644 (file)
@@ -49,13 +49,19 @@ class GilbertAuthPlugin(GilbertPlugin):
                return True
        
        @gilbert_method
                return True
        
        @gilbert_method
-       def passwd(self, request, current_password, new_password, new_password_confirm):
-               user = request.user
-               if user.check_password(current_password) and (new_password == new_password_confirm):
-                       user.set_password(new_password)
-                       user.save()
-                       return True
-               return False
+       def get_passwd_form(self, request):
+               from django.contrib.auth.forms import PasswordChangeForm
+               return PasswordChangeForm(request.user).as_ext()
+       
+       @gilbert_method(form_handler=True)
+       def submit_passwd_form(self, request):
+               from django.contrib.auth.forms import PasswordChangeForm
+               form = PasswordChangeForm(request.user, data=request.POST)
+               if form.is_valid():
+                       form.save()
+                       return {'success': True}
+               else:
+                       return {'success': False, 'errors': form.errors}
        
        @gilbert_method
        def whoami(self, request):
        
        @gilbert_method
        def whoami(self, request):
@@ -149,6 +155,7 @@ class GilbertSite(object):
                                        model_methods.append({
                                                'name': method.name,
                                                'len': method.argc,
                                        model_methods.append({
                                                'name': method.name,
                                                'len': method.argc,
+                                               'formHandler': method.form_handler,
                                        })
                                if model_methods:
                                        model_actions[model_name] = model_methods
                                        })
                                if model_methods:
                                        model_actions[model_name] = model_methods
@@ -170,6 +177,7 @@ class GilbertSite(object):
                                plugin_methods.append({
                                        'name': method.name,
                                        'len': method.argc,
                                plugin_methods.append({
                                        'name': method.name,
                                        'len': method.argc,
+                                       'formHandler': method.form_handler,
                                })
                        if plugin_methods:
                                plugin_actions[plugin_name] = plugin_methods
                                })
                        if plugin_methods:
                                plugin_actions[plugin_name] = plugin_methods
@@ -185,21 +193,33 @@ class GilbertSite(object):
                        submitted_form = True
                
                if submitted_form:
                        submitted_form = True
                
                if submitted_form:
-                       post_dict = dict(request.POST)
                        ext_request = {
                        ext_request = {
-                               'action': post_dict.pop('extAction'),
-                               'method': post_dict.pop('extMethod'),
-                               'type': post_dict.pop('extType'),
-                               'tid': post_dict.pop('extTID'),
-                               'upload': post_dict.pop('extUpload', False),
+                               'action': request.POST.get('extAction'),
+                               'method': request.POST.get('extMethod'),
+                               'type': request.POST.get('extType'),
+                               'tid': request.POST.get('extTID'),
+                               'upload': request.POST.get('extUpload', False),
                                'data': None,
                                'data': None,
-                               'kwdata': post_dict
                        }
                        }
+                       response = self.handle_ext_request(request, ext_request, app_label)
                else:
                else:
-                       ext_request = json.loads(request.raw_post_data)
-                       ext_request['upload'] = False
-                       ext_request['kwdata'] = None
+                       ext_requests = json.loads(request.raw_post_data)
+                       if type(ext_requests) is dict:
+                               ext_requests['upload'] = False
+                               response = self.handle_ext_request(request, ext_requests, app_label)
+                       else:
+                               responses = []
+                               for ext_request in ext_requests:
+                                       ext_request['upload'] = False
+                                       responses.append(self.handle_ext_request(request, ext_request, app_label))
+                               response = responses
                
                
+               if submitted_form:
+                       if ext_request['upload'] is True:
+                               return HttpResponse(('<html><body><textarea>%s</textarea></body></html>' % json.dumps(response)))
+               return HttpResponse(json.dumps(response), content_type=('application/json; charset=%s' % settings.DEFAULT_CHARSET))
+       
+       def handle_ext_request(self, request, ext_request, app_label=None):
                try:
                        plugin = None
                        
                try:
                        plugin = None
                        
@@ -219,14 +239,10 @@ class GilbertSite(object):
                        if method is None or (method.restricted and not self.has_permission(request)):
                                raise NotImplementedError('The method named \'%s\' is not available' % method.name)
                        
                        if method is None or (method.restricted and not self.has_permission(request)):
                                raise NotImplementedError('The method named \'%s\' is not available' % method.name)
                        
-                       response = {'type': 'rpc', 'tid': ext_request['tid'], 'action': ext_request['action'], 'method': ext_request['method'], 'result': method(request, *(ext_request['data'] or []), **(ext_request['kwdata'] or {}))}
+                       return {'type': 'rpc', 'tid': ext_request['tid'], 'action': ext_request['action'], 'method': ext_request['method'], 'result': method(request, *(ext_request['data'] or []))}
                except:
                        exc_type, exc_value, exc_traceback = sys.exc_info()
                except:
                        exc_type, exc_value, exc_traceback = sys.exc_info()
-                       response = {'type': 'exception', 'tid': ext_request['tid'], 'message': ('%s: %s' % (exc_type, exc_value)), 'where': format_tb(exc_traceback)[0]}
-               
-               if submitted_form:
-                       return HttpResponse(('<html><body><textarea>%s</textarea></body></html>' % json.dumps(response)))
-               return HttpResponse(json.dumps(response), content_type=('application/json; charset=%s' % settings.DEFAULT_CHARSET))
+                       return {'type': 'exception', 'tid': ext_request['tid'], 'message': ('%s: %s' % (exc_type, exc_value)), 'where': format_tb(exc_traceback)[0]}
 
 
 site = GilbertSite()
\ No newline at end of file
 
 
 site = GilbertSite()
\ No newline at end of file