def get(self, request): try: allowed_taxon = AllowedTaxon() taxon_list = allowed_taxon.get(request.user) records = BiologicalCollectionRecord.objects.filter( validated=False, ready_for_validation=True, taxon_gbif_id__in=taxon_list) paginator = Paginator(records, self.page_limit) page = request.GET.get('page') try: records = paginator.page(page) current_page = int(page) except PageNotAnInteger: records = paginator.page(1) current_page = 1 except EmptyPage: records = paginator.page(paginator.num_pages) current_page = paginator.num_pages serializer = BioCollectionSerializer(records, many=True) response_data = { 'data': serializer.data, 'pagination': { 'current_page': current_page, 'max_page': paginator.num_pages, 'page_limit': self.page_limit } } return Response(response_data) except BiologicalCollectionRecord.DoesNotExist: return HttpResponse('Object Does Not Exist', status=status.HTTP_400_BAD_REQUEST)
def test_func(self): allowed_taxon = AllowedTaxon() taxon_list = allowed_taxon.get(self.request.user) collection_pk = self.request.GET.get('pk', None) if not collection_pk: return False collection = BiologicalCollectionRecord.objects.get(pk=collection_pk) return collection.taxonomy in taxon_list
def user_passes_test(self, request): if request.user.is_authenticated(): self.object = self.get_object() allowed_taxon = AllowedTaxon() taxon_list = allowed_taxon.get(request.user) return (self.object.owner == request.user or self.object.taxonomy in taxon_list) return False
def get_queryset(self): filter_name = self.request.GET.get('original_species_name', None) filter_owner = self.request.GET.get('owner', None) filter_date_to = self.request.GET.get('date_to', None) filter_date_from = self.request.GET.get('date_from', None) filter_pk = self.request.GET.get('pk', None) if self.queryset is None: allowed_taxon = AllowedTaxon().get(self.request.user) queryset = \ BiologicalCollectionRecord.objects.filter( taxon_gbif_id__in=allowed_taxon, ready_for_validation=True, validated=False).order_by('original_species_name') if filter_pk is not None: queryset = queryset.filter(pk=filter_pk) if filter_name is not None: queryset = queryset.filter( original_species_name__icontains=filter_name) if filter_owner is not None: queryset = queryset.filter( owner__username__icontains=filter_owner) if filter_date_from is not None: queryset = queryset.filter( collection_date__gte=filter_date_from) if filter_date_to is not None: queryset = queryset.filter(collection_date__lte=filter_date_to) return queryset return self.queryset