-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add signal to automatically sync access changes in realtime
- Loading branch information
Showing
4 changed files
with
50 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters