blechreiz-website/eventplanner_gcal/signals.py

67 lines
1.9 KiB
Python

from django.db.models.signals import post_save,pre_delete
from django.dispatch import receiver
from eventplanner.models import Event, EventParticipation, before_read
from eventplanner_gcal.models import createGCalEvent, updateGCalEvent
from eventplanner_gcal.models import deleteGCalEvent, syncParticipationFromGoogleToLocal
class SignalLock:
def __init__(self):
self.locked=False
def __enter__(self):
if self.locked:
return False
self.locked=True
return True
def __exit__(self, type, value, traceback):
self.locked=False
def isLocked(self):
return self.locked
signalLock = SignalLock()
@receiver( post_save,sender= Event)
def event_post_save_handler( **kwargs):
if not signalLock.isLocked():
with signalLock:
event = kwargs['instance']
created = kwargs['created']
if created:
print("Creating Gcal event")
createGCalEvent( event ).execute()
else:
print( "Updating Gcal event")
updateGCalEvent( event ).execute()
@receiver( pre_delete,sender= Event)
def event_pre_delete_handler( **kwargs):
if not signalLock.isLocked():
with signalLock:
event = kwargs['instance']
print ("Deleting GCAL event")
deleteGCalEvent( event ).execute()
@receiver( post_save, sender=EventParticipation )
def participation_post_save_handler( **kwargs):
if not signalLock.isLocked():
with signalLock:
participation = kwargs['instance']
print("Participation post save -> update gcal")
updateGCalEvent( participation.event ).execute()
@receiver( before_read, sender=EventParticipation )
def participation_before_read_handler( **kwargs):
if not signalLock.isLocked():
with signalLock:
print("SyncParticipation from google")
syncParticipationFromGoogleToLocal()