Commits

João Pedro Francese committed 5e3d3e7

Created ResponseWrapperException in AjaxView, to allow context methods to present a specific HttpResponse.

  • Participants
  • Parent commits 72e3c4e

Comments (0)

Files changed (1)

File inoa/views/ajaxview.py

 
     Set a template_name attribute in your class with the template name for this view.
     If not set, non-ajax requests will return an HTTP error.
+    
+    If you want to return a specific HttpResponse in your httpmethod_context() method,
+    raise a ResponseWrapperException with your response wrapped in.
     """
+    
+    class ResponseWrapperException(Exception):
+        """Wraps a HttpResponse object."""
+        def __init__(self, response):
+            self.response = response
 
     def __init__(self, *args, **kwargs):
         super(AjaxView, self).__init__(*args, **kwargs)
         if not request.is_ajax() and not hasattr(self, 'template_name'):
             return HttpResponseBadRequest
 
-        ctx = context_method(request, *args, **kwargs)
+        try:
+            ctx = context_method(request, *args, **kwargs)
+        except self.ResponseWrapperException as ex:
+            return ex.response
+        
         if request.is_ajax():
             return JsonResponse(ctx)
         else: