-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add filters to Uploader CouncilListView
- Loading branch information
Showing
4 changed files
with
123 additions
and
22 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import django_filters | ||
from councils.models import Council | ||
from dc_utils.filter_widgets import DSLinkWidget | ||
from django.db.models import Exists, OuterRef | ||
from pollingstations.models import PollingStation | ||
|
||
|
||
class CouncilListUploadFilter(django_filters.FilterSet): | ||
class Meta: | ||
model = Council | ||
fields = [ | ||
"name", | ||
"pollingstations", | ||
# "upload", | ||
] | ||
|
||
name = django_filters.CharFilter(label="Council Name", method="filter_name") | ||
pollingstations = django_filters.ChoiceFilter( | ||
method="filter_pollingstation", | ||
label="has stations", | ||
widget=DSLinkWidget(), | ||
choices=[ | ||
(1, "With Stations"), | ||
(0, "Without Stations"), | ||
], | ||
) | ||
upload = django_filters.ChoiceFilter( | ||
label="uploads", | ||
method="filter_upload", | ||
widget=DSLinkWidget(), | ||
choices=[ | ||
("valid", "Valid"), | ||
("invalid", "Invalid"), | ||
("pending", "Pending"), | ||
("no_upload", "No upload"), | ||
], | ||
) | ||
|
||
def filter_name(self, queryset, name, value): | ||
return queryset.filter(name__icontains=value) | ||
|
||
def filter_pollingstation(self, queryset, name, value): | ||
""" | ||
Filter on whether stations have been imported | ||
""" | ||
pollingstations = PollingStation.objects.filter(council_id=OuterRef("pk")) | ||
if value == "1": | ||
queryset = queryset.filter(Exists(pollingstations)) | ||
if value == "0": | ||
queryset = queryset.filter(~Exists(pollingstations)) | ||
return queryset | ||
|
||
def filter_upload(self, queryset, name, value): | ||
queryset = Council.objects.with_future_upload_details() | ||
|
||
if value == "valid": | ||
queryset = queryset.filter(latest_upload_files_valid=True) | ||
if value == "invalid": | ||
queryset = queryset.filter( | ||
latest_upload_files_valid=False, latest_upload_id__isnull=False | ||
) | ||
if value == "pending": | ||
queryset = queryset.filter( | ||
latest_upload_files_valid__isnull=True, latest_upload_id__isnull=False | ||
) | ||
if value == "no_upload": | ||
queryset = queryset.filter( | ||
latest_upload_files_valid__isnull=True, latest_upload_id__isnull=True | ||
) | ||
|
||
return queryset |
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