Compare commits

...

2 Commits

Author SHA1 Message Date
b582caebff doorcontrol: Add filtering by door to access reports
All checks were successful
Ruff / ruff (push) Successful in 22s
2024-02-08 10:47:32 -05:00
37feea9623 doorcontrol: Fix size of "items per page" input in reports 2024-02-08 10:45:08 -05:00
2 changed files with 36 additions and 13 deletions

View File

@ -38,6 +38,25 @@
</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="form-floating">
<input type="number"
@ -48,6 +67,7 @@
min="10"
max="200"
step="10"
size="10"
required>
<label for="itemsPerPage">Items Per Page</label>
</div>

View File

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