Coverage for memberships/filters.py: 79%
36 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-04-03 10:12 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-04-03 10:12 -0600
1from django.db import models
2from django_filters import rest_framework as filters
3from django_filters import CharFilter
4from .models import Membership
7class MembershipFilter(filters.FilterSet):
8 """
9 Filter for membership directory
10 """
12 bio = filters.CharFilter(field_name="bio", lookup_expr="icontains")
14 class Meta:
15 model = Membership
16 fields = ["segment", "bio"]
19def filter_extra_field(queryset, name, value):
20 queryset = queryset.filter(**{f"extra_fields__{name}": value})
21 return queryset
24class AdminMembershipFilter(filters.FilterSet):
25 """Filter class to support json fields for extra fields"""
27 class Meta:
28 model = Membership
29 fields = ["segment", "type_of", "holder"]
31 def __init__(self, *args, **kwargs):
32 super(AdminMembershipFilter, self).__init__(*args, **kwargs)
33 request = kwargs.get("request")
34 organization = request.user.employee.organization
35 extra_fields = organization.membership_fields.all()
37 for f in extra_fields: 37 ↛ 38line 37 didn't jump to line 38, because the loop on line 37 never started
38 self.filters[f.name] = CharFilter(method=filter_extra_field, field_name=f.name)
41class MembershipRenewalFilter(filters.FilterSet):
42 """Filter class for Renewal"""
44 class Type(models.TextChoices):
45 INITIAL = "initial", "Initial"
46 RENEWAL = "renewal", "Renewal"
48 type = filters.ChoiceFilter(choices=Type.choices, method="filter_type")
50 class Meta:
51 model = Membership
52 fields = {"type_of": ["exact"], "segment": ["exact"], "products__membershipperiod__period_end": ["lte", "gte"]}
54 def filter_type(self, queryset, name, value):
55 if value == self.Type.INITIAL:
56 return queryset.filter(products__membershipperiod__isnull=True)
57 if value == self.Type.RENEWAL:
58 return queryset.filter(products__membershipperiod__isnull=False).distinct()