Location API: filter by group

This commit is contained in:
Laura Klünder 2017-07-10 17:03:44 +02:00
parent 9e184db721
commit f7900e92a0

View file

@ -167,22 +167,26 @@ class LocationGroupViewSet(MapdataViewSet):
class LocationViewSet(RetrieveModelMixin, GenericViewSet): class LocationViewSet(RetrieveModelMixin, GenericViewSet):
""" """
only accesses locations that have can_search or can_describe set to true. only accesses locations that have can_search or can_describe set to true.
add ?detailed=1 to show all attributes. add ?detailed=1 to show all attributes, add ?group=<id> to filter by group.
/{id}/ add ?show_redirect=1 to suppress redirects and show them as JSON. /{id}/ add ?show_redirect=1 to suppress redirects and show them as JSON.
/search/ only accesses locations that have can_search set to true. Add GET Parameter s to search. /search/ only accesses locations that have can_search set to true. Add GET Parameter s to search.
""" """
queryset = LocationSlug.objects.all() queryset = LocationSlug.objects.all()
lookup_field = 'slug' lookup_field = 'slug'
def get_queryset(self, detailed=False, subconditions=None): def get_queryset(self, detailed=False, subconditions=None, group=None):
queryset = super().get_queryset().order_by('id') queryset = super().get_queryset().order_by('id')
conditions = [] conditions = []
for model in get_submodels(Location): for model in get_submodels(Location):
if group is not None and not hasattr(model, 'groups'):
continue
condition = Q(**{model._meta.default_related_name + '__isnull': False}) condition = Q(**{model._meta.default_related_name + '__isnull': False})
if subconditions: if subconditions:
condition &= reduce(operator.or_, (Q(**{model._meta.default_related_name+'__'+name: value}) condition &= reduce(operator.or_, (Q(**{model._meta.default_related_name+'__'+name: value})
for name, value in subconditions.items())) for name, value in subconditions.items()))
if group is not None:
condition &= Q(**{model._meta.default_related_name+'__groups': group})
conditions.append(condition) conditions.append(condition)
queryset = queryset.filter(reduce(operator.or_, conditions)) queryset = queryset.filter(reduce(operator.or_, conditions))
@ -197,7 +201,18 @@ class LocationViewSet(RetrieveModelMixin, GenericViewSet):
def list(self, request, *args, **kwargs): def list(self, request, *args, **kwargs):
detailed = 'detailed' in request.GET detailed = 'detailed' in request.GET
queryset = self.get_queryset(detailed=detailed, subconditions={'can_search': True, 'can_describe': True}) subconditions = {'can_search': True, 'can_describe': True}
group = None
if 'group' in request.GET:
if not request.GET['group'].isdigit():
raise ValidationError(detail={'detail': _('%s is not an integer.') % 'group'})
try:
group = LocationGroup.objects.get(pk=request.GET['group'])
except LocationGroupCategory.DoesNotExist:
raise NotFound(detail=_('group not found.'))
queryset = self.get_queryset(detailed=detailed, subconditions=subconditions, group=group)
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])