From 60f3ddb2fb6dde3a04866b1bc3df7ae6e53ea918 Mon Sep 17 00:00:00 2001 From: Ed Bartosh Date: Mon, 28 Sep 2015 21:45:20 -0700 Subject: bitbake: toaster: implement decorator for REST responses 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. (Bitbake rev: bb0696f343aca44207581f15ff2b4f0045f7530c) Signed-off-by: Ed Bartosh Signed-off-by: brian avery Signed-off-by: Richard Purdie --- bitbake/lib/toaster/toastergui/views.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'bitbake/lib/toaster/toastergui/views.py') diff --git a/bitbake/lib/toaster/toastergui/views.py b/bitbake/lib/toaster/toastergui/views.py index 2e3b822797..95df60e472 100755 --- a/bitbake/lib/toaster/toastergui/views.py +++ b/bitbake/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() -- cgit v1.2.3-54-g00ecf