[RFC 04/11] REST: Introduce some helper code
Andy Doan
andy.doan at linaro.org
Wed May 11 08:30:46 AEST 2016
On 05/09/2016 08:27 AM, Finucane, Stephen wrote:
> On 15 Apr 13:24, Andy Doan wrote:
>> >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<andy.doan at linaro.org>
> Not so sure about this one. See below.
>
> Stephen
>
>> >---
>> > 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
> I haven't looked at any change after this, but this idea only works if
> you plan to expose every field on every object. Are we sure we want to
> do this?
As you saw later, we only expose what we need. However, after removing
the States endpoint there are only 4 endpoints and 2 of them are already
custom. So we aren't saving much code by adding this helper. I'm going
to drop it in the v2 set.
More information about the Patchwork
mailing list