You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

26 lines
878 B

2 years ago
2 years ago
2 years ago
  1. from django.db.models import Count, Q
  2. from django_filters.rest_framework import BooleanFilter, FilterSet
  3. from .models import Example
  4. class ExampleFilter(FilterSet):
  5. confirmed = BooleanFilter(field_name="states", method="filter_by_state")
  6. def filter_by_state(self, queryset, field_name, is_confirmed: bool):
  7. queryset = queryset.annotate(
  8. num_confirm=Count(
  9. expression=field_name,
  10. filter=Q(**{f"{field_name}__confirmed_by": self.request.user})
  11. | Q(project__collaborative_annotation=True),
  12. )
  13. )
  14. if is_confirmed:
  15. queryset = queryset.filter(num_confirm__gte=1)
  16. else:
  17. queryset = queryset.filter(num_confirm__lte=0)
  18. return queryset
  19. class Meta:
  20. model = Example
  21. fields = ("project", "text", "created_at", "updated_at")