From patchwork Fri Apr 15 18:24:00 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Doan X-Patchwork-Id: 611111 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3qmmGM6w99z9t3n for ; Sat, 16 Apr 2016 04:26:51 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b=I+HWgn0w; dkim-atps=neutral Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 3qmmGM5s6RzDqB2 for ; Sat, 16 Apr 2016 04:26:51 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b=I+HWgn0w; dkim-atps=neutral X-Original-To: patchwork@lists.ozlabs.org Delivered-To: patchwork@lists.ozlabs.org Received: from mail-oi0-x230.google.com (mail-oi0-x230.google.com [IPv6:2607:f8b0:4003:c06::230]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 3qmmF40RBxzDqBR for ; Sat, 16 Apr 2016 04:25:44 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b=I+HWgn0w; dkim-atps=neutral Received: by mail-oi0-x230.google.com with SMTP id y204so132144079oie.3 for ; Fri, 15 Apr 2016 11:25:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=adhlgnXiteUiaya410atv73IBkFyJhj3uL0JBzYSZEE=; b=I+HWgn0wLku+U2hRrBQ0Bw03BdkijVDvIHww+zfhhdeumVz4kOqO9pSGEDYW3MNbDq JmVJz42Ok+ABXmDl4jvJgPJYNkBgUOtuiKcs2N8Bs2da8viDk/ixkmQst48qfkf+AXq8 wOx+0hGTR/Es1R4xQ0J5IgXtJ9t9EnlQqTQy8= 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=adhlgnXiteUiaya410atv73IBkFyJhj3uL0JBzYSZEE=; b=hdVhRVonElTCI1l56OU4V30lrgG1yJXwIJ6VUWhD0lwDSZujtmNoh/IOqMwCyG3sdM gcH97sgHxXC9TuiVD4C+v7dJ1fCTkqlnJBSvUPykEuibydmaeV0UJ+vfZMaKM0SFkosX XBQrXaicEL3/Omwg4AlwldiRP6qPV+oclHVeild9Bvu2d7kPZzenmh213uN+4vsnFE6Q OH2ljFTnZG+//OVfPYXd+BSi6uSXFj9ToEOk2jDg06gFVe7X94t3zd890dtXLlIJJOj5 u1Kf1M7/bZRUN+O3UePd1mYst4CSNIP3+9G9te5oQgUcEraOUKdfbh2ucolYixBPcSPJ gp7g== X-Gm-Message-State: AOPr4FVojtlM20xl0PkeRzLc9+c91ht4mNb0Kwj4OHv7UqANnsM7IAz0JZ45wr4JU4QWipuq X-Received: by 10.202.205.5 with SMTP id d5mr11090539oig.119.1460744741833; Fri, 15 Apr 2016 11:25:41 -0700 (PDT) Received: from doanac-xps.local (cpe-70-117-126-139.austin.res.rr.com. [70.117.126.139]) by smtp.gmail.com with ESMTPSA id n6sm15322610obi.4.2016.04.15.11.25.40 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 15 Apr 2016 11:25:41 -0700 (PDT) From: Andy Doan To: patchwork@lists.ozlabs.org Subject: [RFC 04/11] REST: Introduce some helper code Date: Fri, 15 Apr 2016 13:24:00 -0500 Message-Id: <1460744647-9729-5-git-send-email-andy.doan@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1460744647-9729-1-git-send-email-andy.doan@linaro.org> References: <1460744647-9729-1-git-send-email-andy.doan@linaro.org> X-BeenThere: patchwork@lists.ozlabs.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: Patchwork development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: patchwork-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Patchwork" DRF lends its self to a lot of repetitive code like: Serializer: Meta: model = Foo ViewSet: queryset = Foo.objects This reduces the amount of boiler plate code needed for most of our endpoints so that its easier to audit. Signed-off-by: Andy Doan --- patchwork/views/rest_api.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/patchwork/views/rest_api.py b/patchwork/views/rest_api.py index a4f6886..f6385a3 100644 --- a/patchwork/views/rest_api.py +++ b/patchwork/views/rest_api.py @@ -48,16 +48,23 @@ class PatchworkPermission(permissions.BasePermission): return obj.is_editable(request.user) -class ProjectSerializer(ModelSerializer): - class Meta: - model = Project +class PatchworkViewSet(ModelViewSet): + pagination_class = PageSizePagination + + def get_queryset(self): + return self.serializer_class.Meta.model.objects.all() + +def create_model_serializer(model_class): + class PatchworkSerializer(ModelSerializer): + class Meta: + model = model_class + return PatchworkSerializer -class ProjectViewSet(ModelViewSet): + +class ProjectViewSet(PatchworkViewSet): permission_classes = (PatchworkPermission,) - queryset = Project.objects.all() - serializer_class = ProjectSerializer - pagination_class = PageSizePagination + serializer_class = create_model_serializer(Project) router = DefaultRouter()