def wrapper(request, *args, **kwargs): from django.conf import settings ajax = request.is_ajax() try: res = f(request, *args, **kwargs) except Exception, e: # we got an error. If in debug mode send a JSON response with # the error message back to javascript. if settings.DEBUG and ajax: res = jservererror(e, url = request.path) else: raise e
def post_response(self, djp): '''Handle the post view. This function checks the request.POST dictionary for a post_view_key specified in HTML_CLASSES (by default 'xhr') If it finds this key, the post view is an AJAX-JSON request and the key VALUE represents a function responsible for handling the response. These ajax enabled post view functions must start with the prefix ajax__ followed by the VALUE of the post_view_key. Example: so lets say we find in the POST dictionary { ... 'xhr': 'change_parameter', ... } Than there should be a function called 'ajax__change_parameter' which handle the response''' request = djp.request post = request.POST is_ajax = request.is_ajax() ajax_key = False mimetype = None http = djp.http if is_ajax: mimetype = 'application/javascript' params = dict(post.items()) prefix = params.get('_prefixed',None) ajax_key = params.get(djp.css.post_view_key, None) if ajax_key: if prefix and prefix in ajax_key: ajax_key = ajax_key[len(prefix)+1:] ajax_key = ajax_key.replace('-','_').lower() # If ajax_key is defined, it means this is a AJAX-JSON request if is_ajax: # # Handle the cancel request redirect. # Check for next in the parameters, # If not there redirect to self.defaultredirect if ajax_key == 'cancel': next = params.get('next',None) next = self.defaultredirect(djp.request, next = url, **djp.kwargs) res = jredirect(next) else: ajax_view_function = None if ajax_key: ajax_view = 'ajax__%s' % ajax_key ajax_view_function = getattr(self,str(ajax_view),None) # No post view function found. Let's try the default ajax post view if not ajax_view_function: ajax_view_function = self.default_post; try: res = ajax_view_function(djp) except Exception as e: # we got an error. If in debug mode send a JSON response with # the error message back to javascript. if djp.settings.DEBUG: exc_info = sys.exc_info() stack_trace = '\n'.join(traceback.format_exception(*exc_info)) logerror(self.logger, request, exc_info) res = jservererror(stack_trace, url = djp.url) else: raise e return http.HttpResponse(res.dumps(),mimetype) # # Otherwise it is the default POST response else: return self.default_post(djp)