new get_queryset() for LocationViewSet
This commit is contained in:
parent
ef7b5c973d
commit
3419c9db54
1 changed files with 16 additions and 18 deletions
|
@ -152,15 +152,16 @@ class LocationViewSet(RetrieveModelMixin, GenericViewSet):
|
||||||
queryset = LocationSlug.objects.all()
|
queryset = LocationSlug.objects.all()
|
||||||
lookup_field = 'slug'
|
lookup_field = 'slug'
|
||||||
|
|
||||||
def list(self, request, *args, **kwargs):
|
def get_queryset(self, detailed=False, subconditions=None):
|
||||||
detailed = 'detailed' in request.GET
|
queryset = super().get_queryset().order_by('id')
|
||||||
|
|
||||||
queryset = self.get_queryset().order_by('id')
|
|
||||||
conditions = []
|
conditions = []
|
||||||
for model in get_submodels(Location):
|
for model in get_submodels(Location):
|
||||||
conditions.append(Q(**{model._meta.default_related_name+'__isnull': False}) &
|
condition = Q(**{model._meta.default_related_name + '__isnull': False})
|
||||||
(Q(**{model._meta.default_related_name + '__can_search': True}) |
|
if subconditions:
|
||||||
Q(**{model._meta.default_related_name + '__can_describe': True})))
|
condition &= reduce(operator.or_, (Q(**{model._meta.default_related_name+'__'+name: value})
|
||||||
|
for name, value in subconditions.items()))
|
||||||
|
conditions.append(condition)
|
||||||
queryset = queryset.filter(reduce(operator.or_, conditions))
|
queryset = queryset.filter(reduce(operator.or_, conditions))
|
||||||
|
|
||||||
if detailed:
|
if detailed:
|
||||||
|
@ -168,6 +169,13 @@ class LocationViewSet(RetrieveModelMixin, GenericViewSet):
|
||||||
queryset = queryset.prefetch_related(Prefetch(model._meta.default_related_name + '__groups',
|
queryset = queryset.prefetch_related(Prefetch(model._meta.default_related_name + '__groups',
|
||||||
queryset=LocationGroup.objects.only('id', 'titles')))
|
queryset=LocationGroup.objects.only('id', 'titles')))
|
||||||
|
|
||||||
|
return queryset
|
||||||
|
|
||||||
|
def list(self, request, *args, **kwargs):
|
||||||
|
detailed = 'detailed' in request.GET
|
||||||
|
|
||||||
|
queryset = self.get_queryset(detailed=detailed, subconditions={'can_search': True, 'can_describe': True})
|
||||||
|
|
||||||
return Response([obj.get_child().serialize(include_type=True, detailed=detailed) for obj in queryset])
|
return Response([obj.get_child().serialize(include_type=True, detailed=detailed) for obj in queryset])
|
||||||
|
|
||||||
def retrieve(self, request, slug=None, *args, **kwargs):
|
def retrieve(self, request, slug=None, *args, **kwargs):
|
||||||
|
@ -194,17 +202,7 @@ class LocationViewSet(RetrieveModelMixin, GenericViewSet):
|
||||||
detailed = 'detailed' in request.GET
|
detailed = 'detailed' in request.GET
|
||||||
search = request.GET.get('s')
|
search = request.GET.get('s')
|
||||||
|
|
||||||
queryset = self.get_queryset().order_by('id')
|
queryset = self.get_queryset(detailed=detailed, subconditions={'can_search': True})
|
||||||
conditions = []
|
|
||||||
for model in get_submodels(Location):
|
|
||||||
conditions.append(Q(**{model._meta.default_related_name + '__isnull': False}) &
|
|
||||||
Q(**{model._meta.default_related_name + '__can_search': True}))
|
|
||||||
queryset = queryset.filter(reduce(operator.or_, conditions))
|
|
||||||
|
|
||||||
if detailed:
|
|
||||||
for model in get_submodels(SpecificLocation):
|
|
||||||
queryset = queryset.prefetch_related(Prefetch(model._meta.default_related_name+'__groups',
|
|
||||||
queryset=LocationGroup.objects.only('id', 'titles')))
|
|
||||||
|
|
||||||
if not search:
|
if not search:
|
||||||
return Response([obj.serialize(include_type=True, detailed=detailed) for obj in queryset])
|
return Response([obj.serialize(include_type=True, detailed=detailed) for obj in queryset])
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue