Skip to content

Commit

Permalink
Add signal to automatically sync access changes in realtime
Browse files Browse the repository at this point in the history
  • Loading branch information
jabelone committed May 23, 2024
1 parent dc30244 commit 4c52514
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
8 changes: 8 additions & 0 deletions memberportal/profile/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.apps import AppConfig


class ProfileConfig(AppConfig):
name = "profile"

def ready(self):
import profile.signals
40 changes: 40 additions & 0 deletions memberportal/profile/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import logging

logger = logging.getLogger("profile")

from django.db.models.signals import post_save
from django.dispatch import receiver
from django.contrib import auth
from .models import Profile

User = auth.get_user_model()


@receiver(post_save, sender=Profile)
def update_profile(sender, instance, created, **kwargs):
# disable the handler during fixture loading
if kwargs["raw"]:
return

door_access_changed = False
interlock_access_changed = False
profile = Profile.objects.get(pk=instance.id)

# if we didn't just create the profile check if the doors or interlocks properties have changed
if not created:
# TODO: check every single door and interlock for changes
door_access_changed = profile.doors != instance.doors
interlock_access_changed = profile.interlocks != instance.interlocks

if profile.state != instance.state:
# If our profile state changed, then sync everything.
door_access_changed = True
interlock_access_changed = True

if door_access_changed:
for door in profile.doors:
door.sync()

if interlock_access_changed:
for interlock in profile.interlocks:
interlock.sync()
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion src-frontend/src/mixins/formatMixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import utc from 'dayjs/plugin/utc';
import timezone from 'dayjs/plugin/timezone';
import relativeTime from 'dayjs/plugin/relativeTime';
import duration from 'dayjs/plugin/duration';
import { i18n } from 'boot/i18n';

dayjs.extend(duration);
dayjs.extend(utc);
Expand Down

0 comments on commit 4c52514

Please sign in to comment.