-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat(registration)/filter participants (#895)
* filtering by year and study * linting fix * added allergy filter * added filter by allergies and participants with allergy count to event. * Lint fix * Add new fixture for admin user * Start testing filtering + finished allergy filter test * Added integration test for participants filtering * lint fix * removed unused import * Update changelog * merge with dev and more filters * Fixed has_paid filter and added filter combination test * ran linting script --------- Co-authored-by: Harry Linrui XU <[email protected]> Co-authored-by: Mads Nylund <[email protected]>
- Loading branch information
1 parent
119bbdb
commit 0e053d4
Showing
5 changed files
with
228 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,71 @@ | ||
from django.db.models import Exists, OuterRef | ||
from django_filters import rest_framework as filters | ||
from django_filters.rest_framework import FilterSet | ||
|
||
from app.common.enums import NativeGroupType as GroupType | ||
from app.content.models.registration import Registration | ||
from app.payment.enums import OrderStatus | ||
from app.payment.models import Order | ||
|
||
|
||
class RegistrationFilter(FilterSet): | ||
|
||
study = filters.CharFilter( | ||
field_name="user__groups__name", lookup_expr="icontains", method="filter_study" | ||
) | ||
year = filters.CharFilter( | ||
field_name="user__groups__name", lookup_expr="icontains", method="filter_year" | ||
) | ||
|
||
has_allergy = filters.BooleanFilter( | ||
field_name="user__allergy", method="filter_has_allergy" | ||
) | ||
|
||
has_paid = filters.BooleanFilter( | ||
field_name="event__orders__status", method="filter_has_paid" | ||
) | ||
|
||
class Meta: | ||
model = Registration | ||
fields = ["has_attended", "is_on_wait"] | ||
fields = [ | ||
"has_attended", | ||
"is_on_wait", | ||
"study", | ||
"year", | ||
"has_allergy", | ||
"allow_photo", | ||
"has_paid", | ||
] | ||
|
||
def filter_study(self, queryset, name, value): | ||
return queryset.filter( | ||
user__memberships__group__name__icontains=value, | ||
user__memberships__group__type=GroupType.STUDY, | ||
) | ||
|
||
def filter_has_paid(self, queryset, name, value): | ||
sale_order_exists = Order.objects.filter( | ||
event=OuterRef("event_id"), | ||
user=OuterRef("user_id"), | ||
status=OrderStatus.SALE, | ||
) | ||
|
||
if value: | ||
return queryset.filter(Exists(sale_order_exists)) | ||
else: | ||
return queryset.exclude(Exists(sale_order_exists)) | ||
|
||
def filter_year(self, queryset, name, value): | ||
return queryset.filter( | ||
user__memberships__group__name__icontains=value, | ||
user__memberships__group__type=GroupType.STUDYYEAR, | ||
) | ||
|
||
def filter_has_allergy(self, queryset, name, value): | ||
if value: | ||
return queryset.exclude(user__allergy__isnull=True).exclude( | ||
user__allergy__exact="" | ||
) | ||
return queryset.filter(user__allergy__isnull=True) | queryset.filter( | ||
user__allergy__exact="" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters