Message ID | 1460744647-9729-5-git-send-email-andy.doan@linaro.org |
---|---|
State | Superseded |
Headers | show |
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@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? > > -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() > -- > 2.7.4 > > _______________________________________________ > Patchwork mailing list > Patchwork@lists.ozlabs.org > https://lists.ozlabs.org/listinfo/patchwork
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@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.
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()
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@linaro.org> --- patchwork/views/rest_api.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-)