From 968a47c723a3c0b9245a94c941582f147d2303c3 Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Fri, 26 Jan 2024 13:58:14 -0500 Subject: [PATCH] doorcontrol: Use django-filters for date range filter in access reports --- .../doorcontrol/access_report.dj.html | 8 ++--- doorcontrol/views.py | 35 ++++++------------- 2 files changed, 14 insertions(+), 29 deletions(-) diff --git a/doorcontrol/templates/doorcontrol/access_report.dj.html b/doorcontrol/templates/doorcontrol/access_report.dj.html index 3b31a37..e6a1baf 100644 --- a/doorcontrol/templates/doorcontrol/access_report.dj.html +++ b/doorcontrol/templates/doorcontrol/access_report.dj.html @@ -20,8 +20,8 @@ + name="timestamp_after" + value="{{ filter.form.timestamp.value.0 }}"> @@ -30,8 +30,8 @@ + name="timestamp_before" + value="{{ filter.form.timestamp.value.1 }}"> diff --git a/doorcontrol/views.py b/doorcontrol/views.py index ad46135..e2188ed 100644 --- a/doorcontrol/views.py +++ b/doorcontrol/views.py @@ -7,12 +7,12 @@ from django.core.paginator import Page from django.db.models import Count, F, FloatField, Window from django.db.models.functions import Lead, Trunc from django.urls import path, reverse_lazy -from django.utils import dateparse from django.utils.text import slugify -from django.utils.timezone import localtime from django.views.generic.list import ListView +import django_filters import django_tables2 as tables +from django_filters.views import BaseFilterView from django_tables2 import SingleTableMixin from django_tables2.export.views import ExportMixin @@ -26,8 +26,12 @@ def register_report(cls: "BaseAccessReport"): return cls +class DateTimeFilters(django_filters.FilterSet): + timestamp = django_filters.DateFromToRangeFilter() + + class BaseAccessReport( - ExportMixin, SingleTableMixin, PermissionRequiredMixin, ListView + BaseFilterView, ExportMixin, SingleTableMixin, PermissionRequiredMixin, ListView ): model = HIDEvent permission_required = "doorcontrol.view_hidevent" @@ -37,6 +41,8 @@ class BaseAccessReport( export_formats = ("csv", "xlsx", "ods") + filterset_class = DateTimeFilters + _report_name = None @classmethod @@ -58,30 +64,13 @@ class BaseAccessReport( def _selected_report(self): return self._report_name - def _get_timestamp_range(self): - timestamp__gte = dateparse.parse_datetime( - self.request.GET.get("timestamp__gte") or "2019-01-01" - ) - timestamp__lte = self.request.GET.get("timestamp__lte") - if timestamp__lte: - timestamp__lte = dateparse.parse_datetime(timestamp__lte) - else: - timestamp__lte = localtime() - - return timestamp__gte, timestamp__lte - def get_paginate_by(self, queryset) -> int: if "items_per_page" in self.request.GET: return int(self.request.GET.get("items_per_page")) return super().get_paginate_by(queryset) def get_queryset(self): - return ( - super() - .get_queryset() - .filter(timestamp__range=self._get_timestamp_range()) - .select_related("door") - ) + return super().get_queryset().select_related("door") def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) @@ -94,10 +83,6 @@ class BaseAccessReport( context["selected_report"] = self._selected_report() context["items_per_page"] = self.get_paginate_by(None) - timestamp__gte, timestamp__lte = self._get_timestamp_range() - context["timestamp__gte"] = timestamp__gte - context["timestamp__lte"] = timestamp__lte - query_params = self.request.GET.copy() if "page" in query_params: query_params.pop("page")