doorcontrol: Use django-filters for date range filter in access reports

This commit is contained in:
Adam Goldsmith 2024-01-26 13:58:14 -05:00
parent f20d85e2e4
commit 968a47c723
2 changed files with 14 additions and 29 deletions

View File

@ -20,8 +20,8 @@
<input type="date" <input type="date"
class="form-control" class="form-control"
id="startDate" id="startDate"
name="timestamp__gte" name="timestamp_after"
value="{{ timestamp__gte|date:'Y-m-d' }}"> value="{{ filter.form.timestamp.value.0 }}">
<label for="startDate">Start Date</label> <label for="startDate">Start Date</label>
</div> </div>
</div> </div>
@ -30,8 +30,8 @@
<input type="date" <input type="date"
class="form-control" class="form-control"
id="endDate" id="endDate"
name="timestamp__lte" name="timestamp_before"
value="{{ timestamp__lte|date:'Y-m-d' }}"> value="{{ filter.form.timestamp.value.1 }}">
<label for="endDate">End Date</label> <label for="endDate">End Date</label>
</div> </div>
</div> </div>

View File

@ -7,12 +7,12 @@ from django.core.paginator import Page
from django.db.models import Count, F, FloatField, Window from django.db.models import Count, F, FloatField, Window
from django.db.models.functions import Lead, Trunc from django.db.models.functions import Lead, Trunc
from django.urls import path, reverse_lazy from django.urls import path, reverse_lazy
from django.utils import dateparse
from django.utils.text import slugify from django.utils.text import slugify
from django.utils.timezone import localtime
from django.views.generic.list import ListView from django.views.generic.list import ListView
import django_filters
import django_tables2 as tables import django_tables2 as tables
from django_filters.views import BaseFilterView
from django_tables2 import SingleTableMixin from django_tables2 import SingleTableMixin
from django_tables2.export.views import ExportMixin from django_tables2.export.views import ExportMixin
@ -26,8 +26,12 @@ def register_report(cls: "BaseAccessReport"):
return cls return cls
class DateTimeFilters(django_filters.FilterSet):
timestamp = django_filters.DateFromToRangeFilter()
class BaseAccessReport( class BaseAccessReport(
ExportMixin, SingleTableMixin, PermissionRequiredMixin, ListView BaseFilterView, ExportMixin, SingleTableMixin, PermissionRequiredMixin, ListView
): ):
model = HIDEvent model = HIDEvent
permission_required = "doorcontrol.view_hidevent" permission_required = "doorcontrol.view_hidevent"
@ -37,6 +41,8 @@ class BaseAccessReport(
export_formats = ("csv", "xlsx", "ods") export_formats = ("csv", "xlsx", "ods")
filterset_class = DateTimeFilters
_report_name = None _report_name = None
@classmethod @classmethod
@ -58,30 +64,13 @@ class BaseAccessReport(
def _selected_report(self): def _selected_report(self):
return self._report_name 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: def get_paginate_by(self, queryset) -> int:
if "items_per_page" in self.request.GET: if "items_per_page" in self.request.GET:
return int(self.request.GET.get("items_per_page")) return int(self.request.GET.get("items_per_page"))
return super().get_paginate_by(queryset) return super().get_paginate_by(queryset)
def get_queryset(self): def get_queryset(self):
return ( return super().get_queryset().select_related("door")
super()
.get_queryset()
.filter(timestamp__range=self._get_timestamp_range())
.select_related("door")
)
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
@ -94,10 +83,6 @@ class BaseAccessReport(
context["selected_report"] = self._selected_report() context["selected_report"] = self._selected_report()
context["items_per_page"] = self.get_paginate_by(None) 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() query_params = self.request.GET.copy()
if "page" in query_params: if "page" in query_params:
query_params.pop("page") query_params.pop("page")