Skip to content

Commit

Permalink
Try to move some code into parent class
Browse files Browse the repository at this point in the history
Before merge of the PR, this should be squashed into different commit.
  • Loading branch information
lubomir committed Nov 5, 2015
1 parent 5b64f53 commit aeaa65e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
10 changes: 10 additions & 0 deletions pdc/apps/common/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ class StrictQueryParamMixin(object):
`query_params` class attribute on the serializer. Note that this should
only include the parameters that are passed via URL query string, not
request body fields.
It also provides some helper functions for examining request.
"""
def initial(self, request, *args, **kwargs):
super(StrictQueryParamMixin, self).initial(request, *args, **kwargs)
Expand All @@ -162,6 +164,14 @@ def initial(self, request, *args, **kwargs):
if extra_keys:
raise FieldError('Unknown query params: %s.' % ', '.join(sorted(extra_keys)))

def is_filtered_list(self):
"""Return True if the current action is list and pagination is enabled.
This may be handy for adding prefetching of related models from
database.
"""
return self.action == 'list' and self.paginator.get_page_size(self.request)


class PDCModelViewSet(StrictQueryParamMixin,
ChangeSetModelMixin,
Expand Down
2 changes: 1 addition & 1 deletion pdc/apps/release/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ def get_queryset(self):
# Preloading with a separate request could be actually be slow if there
# is no filter, so it's done only on list request with paging enabled.
queryset = self.queryset.select_related('variant_type', 'release')
if self.action == 'list' and self.paginator.get_page_size(self.request):
if self.is_filtered_list():
queryset = queryset.prefetch_related('variantarch_set__arch')
return queryset

Expand Down

0 comments on commit aeaa65e

Please sign in to comment.