[bitbake-devel] [PATCH 10/22] toaster: implement decorator for REST responses

brian avery avery.brian at gmail.com
Tue Sep 29 04:45:20 UTC 2015


From: Ed Bartosh <ed.bartosh at linux.intel.com>

Implemented xhr_response decorator to decorate responses
from REST methods into Django HttpResponse objects.

This decorator should reduce amount of repeated code in
REST methods and make them more readable.

Signed-off-by: Ed Bartosh <ed.bartosh at linux.intel.com>
Signed-off-by: brian avery <avery.brian at gmail.com>
---
 lib/toaster/toastergui/views.py | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/lib/toaster/toastergui/views.py b/lib/toaster/toastergui/views.py
index 2e3b822..95df60e 100755
--- a/lib/toaster/toastergui/views.py
+++ b/lib/toaster/toastergui/views.py
@@ -45,6 +45,7 @@ from django.utils import formats
 from toastergui.templatetags.projecttags import json as jsonfilter
 import json
 from os.path import dirname
+from functools import wraps
 import itertools
 
 import magic
@@ -2314,6 +2315,18 @@ if True:
 
         return context
 
+    def xhr_response(fun):
+        """
+        Decorator for REST methods.
+        calls jsonfilter on the returned dictionary and returns result
+        as HttpResponse object of content_type application/json
+        """
+        @wraps(fun)
+        def wrapper(*args, **kwds):
+            return HttpResponse(jsonfilter(fun(*args, **kwds)),
+                                content_type="application/json")
+        return wrapper
+
     def jsunittests(request):
       """ Provides a page for the js unit tests """
       bbv = BitbakeVersion.objects.filter(branch="master").first()
-- 
1.9.1




More information about the bitbake-devel mailing list