doorcontrol: Add filtering by door to access reports
All checks were successful
Ruff / ruff (push) Successful in 22s

This commit is contained in:
Adam Goldsmith 2024-02-08 10:47:32 -05:00
parent 37feea9623
commit b582caebff
2 changed files with 35 additions and 13 deletions

View File

@ -38,6 +38,25 @@
</div> </div>
</div> </div>
<div class="col-12 col-sm-auto dropdown d-grid">
<button type="button"
class="btn btn-outline-secondary btn-lg dropdown-toggle"
data-bs-toggle="dropdown"
data-bs-auto-close="outside"
aria-expanded="false">Doors</button>
<div class="dropdown-menu py-0">
<div class="dropdown-header">Use Ctrl/Shift to select multiple</div>
<select class="form-select"
aria-label="Door"
name="{{ filter.form.door.name }}"
id="id_door"
size="{{ filter.form.door.subwidgets|length }}"
multiple>
{% for widget in filter.form.door.subwidgets %}{{ widget }}{% endfor %}
</select>
</div>
</div>
<div class="col-12 col-sm-auto"> <div class="col-12 col-sm-auto">
<div class="form-floating"> <div class="form-floating">
<input type="number" <input type="number"

View File

@ -16,7 +16,7 @@ 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
from .models import HIDEvent from .models import Door, HIDEvent
REPORTS = [] REPORTS = []
@ -26,8 +26,11 @@ def register_report(cls: "BaseAccessReport"):
return cls return cls
class DateTimeFilters(django_filters.FilterSet): class AccessReportFilterSet(django_filters.FilterSet):
timestamp = django_filters.DateFromToRangeFilter() timestamp = django_filters.DateFromToRangeFilter()
door = django_filters.ModelMultipleChoiceFilter(
queryset=Door.objects.all(), distinct=False
)
class BaseAccessReport( class BaseAccessReport(
@ -41,7 +44,7 @@ class BaseAccessReport(
export_formats = ("csv", "xlsx", "ods") export_formats = ("csv", "xlsx", "ods")
filterset_class = DateTimeFilters filterset_class = AccessReportFilterSet
_report_name = None _report_name = None
@ -162,7 +165,7 @@ class AccessPerUnitTime(BaseAccessReport):
"extra_columns": (("unit_time", unit_time_column),), "extra_columns": (("unit_time", unit_time_column),),
} }
def get_queryset(self): def get_table_data(self):
unit_time = self.kwargs["unit_time"] unit_time = self.kwargs["unit_time"]
if unit_time not in self.UNIT_TIMES: if unit_time not in self.UNIT_TIMES:
raise BadRequest("unit time must be one of day, week, month, or year") raise BadRequest("unit time must be one of day, week, month, or year")
@ -172,7 +175,7 @@ class AccessPerUnitTime(BaseAccessReport):
] ]
return ( return (
super() super()
.get_queryset() .get_table_data()
.filter(event_type__in=granted_event_types) .filter(event_type__in=granted_event_types)
.values(unit_time=Trunc("timestamp", unit_time)) .values(unit_time=Trunc("timestamp", unit_time))
.annotate( .annotate(
@ -226,13 +229,13 @@ class DeniedAccess(BaseAccessReport):
_report_name = "Denied Access" _report_name = "Denied Access"
table_class = DeniedAccessTable table_class = DeniedAccessTable
def get_queryset(self): def get_table_data(self):
denied_event_types = [ denied_event_types = [
t for t in HIDEvent.EventType if t.name.startswith("DENIED_ACCESS") t for t in HIDEvent.EventType if t.name.startswith("DENIED_ACCESS")
] ]
return ( return (
super() super()
.get_queryset() .get_table_data()
.filter(event_type__in=denied_event_types) .filter(event_type__in=denied_event_types)
.with_decoded_card_number() .with_decoded_card_number()
) )
@ -251,10 +254,10 @@ class MostActiveMembers(BaseAccessReport):
_report_name = "Most Active Members" _report_name = "Most Active Members"
table_class = MostActiveMembersTable table_class = MostActiveMembersTable
def get_queryset(self): def get_table_data(self):
return ( return (
super() super()
.get_queryset() .get_table_data()
.values("cardholder_id", "forename", "surname") .values("cardholder_id", "forename", "surname")
.order_by() .order_by()
.annotate(access_count=Count("cardholder_id")) .annotate(access_count=Count("cardholder_id"))
@ -276,10 +279,10 @@ class BusiestDayOfWeek(BaseAccessReport):
table_pagination = False table_pagination = False
table_class = BusiestDayOfWeekTable table_class = BusiestDayOfWeekTable
def get_queryset(self): def get_table_data(self):
return ( return (
super() super()
.get_queryset() .get_table_data()
.values("timestamp__week_day") .values("timestamp__week_day")
.annotate(events=Count("timestamp")) .annotate(events=Count("timestamp"))
) )
@ -296,10 +299,10 @@ class BusiestTimeOfDay(BaseAccessReport):
table_pagination = False table_pagination = False
table_class = BusiestTimeOfDayTable table_class = BusiestTimeOfDayTable
def get_queryset(self): def get_table_data(self):
return ( return (
super() super()
.get_queryset() .get_table_data()
.values("timestamp__hour") .values("timestamp__hour")
.annotate(events=Count("timestamp")) .annotate(events=Count("timestamp"))
) )