doorcontrol: Add is_door_active to prevent forgetting "Misc Access"

This commit is contained in:
Adam Goldsmith 2024-12-25 10:52:32 -05:00
parent 71d2227854
commit b3a7ef8232
2 changed files with 10 additions and 15 deletions

View File

@ -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

View File

@ -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)