From b3a7ef823215260569c913628861e59c3c1b6e5e Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Wed, 25 Dec 2024 10:52:32 -0500 Subject: [PATCH] doorcontrol: Add `is_door_active` to prevent forgetting "Misc Access" --- doorcontrol/tasks/update_unifi_access.py | 19 ++++--------------- membershipworks/models.py | 6 ++++++ 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/doorcontrol/tasks/update_unifi_access.py b/doorcontrol/tasks/update_unifi_access.py index 67cf39e..3c4e6eb 100644 --- a/doorcontrol/tasks/update_unifi_access.py +++ b/doorcontrol/tasks/update_unifi_access.py @@ -101,14 +101,8 @@ def update_user_groups( expected_group_members = ( rule.get_matching_members() - .with_is_active() - .filter( - Q(**{door.access_field: True}) - & ( - Q(is_active=True) - | Member.objects.has_flag("folder", "Misc. Access") - ) - ) + .with_is_door_active() + .filter(Q(**{door.access_field: True}) & Q(is_door_active=True)) ) # all members should exist in Access by this point expected_group_member_ids = [ @@ -137,7 +131,7 @@ def sync_members(access_client: AccessClient): if user.employee_number } - for member in Member.objects.with_is_active().all(): + for member in Member.objects.with_is_door_active().all(): logger.info("Syncing member %s", member) expected_user = { "first_name": member.first_name, @@ -149,12 +143,7 @@ def sync_members(access_client: AccessClient): if access_user := access_users_by_employee_number.get(member.uid): expected_user["status"] = ( - UserStatus.ACTIVE - if ( - member.is_active - or member.flags.filter(name="Misc. Access", type="folder").exists() - ) - else UserStatus.DEACTIVATED + UserStatus.ACTIVE if member.is_door_active else UserStatus.DEACTIVATED ) changes = { k: v for k, v in expected_user.items() if getattr(access_user, k) != v diff --git a/membershipworks/models.py b/membershipworks/models.py index 0df157f..def68b2 100644 --- a/membershipworks/models.py +++ b/membershipworks/models.py @@ -123,6 +123,12 @@ class MemberQuerySet(models.QuerySet): ) ) + def with_is_door_active(self): + """Like `is_active`, but also includes "Misc. Access" members""" + return self.with_is_active().annotate( + is_door_active=Q(is_active=True) | self.has_flag("folder", "Misc. Access") + ) + class Member(BaseModel): uid = models.CharField(max_length=24, primary_key=True)