Coverage for accounting/filters.py: 84%
19 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-03-29 14:03 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-03-29 14:03 -0600
1import django_filters
2from app.filters import TimeStampedFilter
3from app.utils import get_subclass_as_dict
4from products.models import Product
5from .models import ProductCharge
8class ProductChargeFilter(TimeStampedFilter):
9 ctype_dict = get_subclass_as_dict(Product)
11 product_type = django_filters.ChoiceFilter(
12 method="product_type_filter",
13 choices=list(ctype_dict.items()),
14 )
16 class Meta:
17 model = ProductCharge
18 fields = {
19 "membership": ["exact"],
20 "membership__name": ["icontains"],
21 "membership__segment": ["exact"],
22 "date": ["gte", "lte"],
23 "product": ["exact"],
24 "payment_status": ["exact"],
25 }
27 def product_type_filter(self, queryset, name, value):
28 selected_product_type = self.ctype_dict[value] if value in self.ctype_dict.keys() else None
29 selected_products = Product.objects.instance_of(selected_product_type)
30 return queryset.filter(product__in=selected_products)
33class AnnualProductChargeFilter(django_filters.rest_framework.FilterSet):
34 year = django_filters.NumberFilter(method="year_filter")
36 def year_filter(self, queryset, name, value):
37 return queryset.filter(date__year=value)