]> git.ipfire.org Git - thirdparty/openembedded/openembedded-core-contrib.git/commitdiff
toaster: implement decorator for REST responses
authorEd Bartosh <ed.bartosh@linux.intel.com>
Tue, 29 Sep 2015 04:45:20 +0000 (21:45 -0700)
committerRichard Purdie <richard.purdie@linuxfoundation.org>
Tue, 29 Sep 2015 12:44:48 +0000 (13:44 +0100)
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@linux.intel.com>
Signed-off-by: brian avery <avery.brian@gmail.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
lib/toaster/toastergui/views.py

index 2e3b822797e01a388150499b2121dbffe17cf7c8..95df60e4724b6c151ec25ad90abf1118cfaef2fb 100755 (executable)
@@ -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()