diff --git a/doorcontrol/models.py b/doorcontrol/models.py index e15d80f..d697cbe 100644 --- a/doorcontrol/models.py +++ b/doorcontrol/models.py @@ -1,4 +1,5 @@ from datetime import datetime +from typing import Self from django.conf import settings from django.db import models @@ -179,6 +180,10 @@ class HIDEvent(models.Model): DOOR_UNLOCKED = 12032, "Door Unlocked" DOOR_LOCKED = 12033, "Door Locked" + @classmethod + def any_granted_access(cls) -> list[Self]: + return [t for t in cls if t.name.startswith("GRANTED_ACCESS")] + door = models.ForeignKey(Door, on_delete=models.CASCADE) timestamp = models.DateTimeField() event_type = models.IntegerField(db_column="eventType", choices=EventType.choices) diff --git a/doorcontrol/tables.py b/doorcontrol/tables.py index 1ee561d..404d373 100644 --- a/doorcontrol/tables.py +++ b/doorcontrol/tables.py @@ -46,6 +46,7 @@ class DetailByDayTable(tables.Table): timestamp__date = tables.DateColumn(verbose_name="Date") name = tables.Column() access_count = tables.Column() + granted_access_count = tables.Column() class BusiestDayOfWeekTable(tables.Table): diff --git a/doorcontrol/views.py b/doorcontrol/views.py index 7b9791b..dd651be 100644 --- a/doorcontrol/views.py +++ b/doorcontrol/views.py @@ -3,7 +3,7 @@ import datetime from django.contrib.auth.mixins import PermissionRequiredMixin from django.core.exceptions import BadRequest from django.core.paginator import Page -from django.db.models import Count, F, FloatField, Window +from django.db.models import Count, F, FloatField, Q, Window from django.db.models.functions import Lead, Trunc from django.urls import path, reverse_lazy from django.utils.text import slugify @@ -165,13 +165,10 @@ class AccessPerUnitTime(BaseAccessReport): if unit_time not in self.UNIT_TIMES: raise BadRequest("unit time must be one of day, week, month, or year") - granted_event_types = [ - t for t in HIDEvent.EventType if t.name.startswith("GRANTED_ACCESS") - ] return ( super() .get_table_data() - .filter(event_type__in=granted_event_types) + .filter(event_type__in=HIDEvent.EventType.any_granted_access()) .with_member_id() .values(unit_time=Trunc("timestamp", unit_time)) .annotate( @@ -255,6 +252,10 @@ class DetailByDay(BaseAccessReport): .filter(member_id__isnull=False) .annotate( access_count=Count("member_id"), + granted_access_count=Count( + "member_id", + filter=Q(event_type__in=HIDEvent.EventType.any_granted_access()), + ), name=GroupConcat( ConcatWS("forename", "surname", separator=" "), distinct=True ),