Return-Path: Received: by yocto-www.yoctoproject.org (Postfix, from userid 118) id D64E8E006F6; Fri, 25 Sep 2015 11:07:51 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on yocto-www.yoctoproject.org X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.1 X-Spam-HAM-Report: * -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low * trust * [209.85.212.172 listed in list.dnswl.org] * -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% * [score: 0.0000] Received: from mail-wi0-f172.google.com (mail-wi0-f172.google.com [209.85.212.172]) by yocto-www.yoctoproject.org (Postfix) with ESMTP id 51F23E006F6 for ; Fri, 25 Sep 2015 11:07:50 -0700 (PDT) Received: by wicge5 with SMTP id ge5so31467734wic.0 for ; Fri, 25 Sep 2015 11:07:50 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=wiucReghKgPJYKXgwNtYhO9L4zq1+MeTSzjax8q73Ds=; b=fJLlj86diGDhHN8tqCNWtx4/LpK+4GJjWIU6Uzuj/hCcOCarYmCmaEy2yHyFmfN6A8 qlQPnM/o8OtTZr17isYY6SauyC4+R0Op2S2ljtYo5eux+vtznDySk+JcD11iidQ1RG6P /dv1cg5zXMIPlj0fCch1S5O3niISlm6qT38uZrRu10qeqxVONf1/VWLjZFcQlz7jrtV4 FVPHf6IySwwWkb5OnNdVMzf4mgK++fTwz3Z4lwJjLn7/WN7DLlj0xPxUYsizI7J6K1SL JlhA89A+gaxqZXmPEkDH+Xj8eX8QSXqQBYJ5Noo3Iiy/BUaNw8Nyw8kv8SCLcD82jYOJ //fw== X-Gm-Message-State: ALoCoQmy3Ob11INIlM962IijCTiLgtIHYos7YMvQu7aZcT3litRs/HWHyEvKg+R564vMOIxDMDH0 X-Received: by 10.194.120.198 with SMTP id le6mr7712217wjb.133.1443204470206; Fri, 25 Sep 2015 11:07:50 -0700 (PDT) Received: from draco.isw.intel.com ([83.217.123.106]) by smtp.gmail.com with ESMTPSA id uq5sm4511789wjc.3.2015.09.25.11.07.49 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 25 Sep 2015 11:07:49 -0700 (PDT) From: Michael Wood To: toaster@yoctoproject.org Date: Fri, 25 Sep 2015 19:07:21 +0100 Message-Id: <1443204452-32244-11-git-send-email-michael.g.wood@intel.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1443204452-32244-1-git-send-email-michael.g.wood@intel.com> References: <1443204452-32244-1-git-send-email-michael.g.wood@intel.com> Subject: [PATCH 10/21] toaster: implement decorator for REST responses X-BeenThere: toaster@yoctoproject.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: Web based interface for BitBake List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 25 Sep 2015 18:07:51 -0000 From: Ed Bartosh 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 --- bitbake/lib/toaster/toastergui/views.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/bitbake/lib/toaster/toastergui/views.py b/bitbake/lib/toaster/toastergui/views.py index 2e3b822..95df60e 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() -- 2.1.4