doorcontrol: Use django-filters for date range filter in access reports
This commit is contained in:
parent
f20d85e2e4
commit
968a47c723
@ -20,8 +20,8 @@
|
||||
<input type="date"
|
||||
class="form-control"
|
||||
id="startDate"
|
||||
name="timestamp__gte"
|
||||
value="{{ timestamp__gte|date:'Y-m-d' }}">
|
||||
name="timestamp_after"
|
||||
value="{{ filter.form.timestamp.value.0 }}">
|
||||
<label for="startDate">Start Date</label>
|
||||
</div>
|
||||
</div>
|
||||
@ -30,8 +30,8 @@
|
||||
<input type="date"
|
||||
class="form-control"
|
||||
id="endDate"
|
||||
name="timestamp__lte"
|
||||
value="{{ timestamp__lte|date:'Y-m-d' }}">
|
||||
name="timestamp_before"
|
||||
value="{{ filter.form.timestamp.value.1 }}">
|
||||
<label for="endDate">End Date</label>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -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")
|
||||
|
Loading…
Reference in New Issue
Block a user