2014-03-09 18:13:30 +01:00
|
|
|
from django.db.models.signals import post_save,pre_delete
|
|
|
|
from django.dispatch import receiver
|
2014-04-18 13:43:02 +02:00
|
|
|
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()
|
2014-03-09 18:13:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
@receiver( post_save,sender= Event)
|
2014-04-18 13:43:02 +02:00
|
|
|
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()
|
2014-03-09 18:13:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2014-04-18 13:43:02 +02:00
|
|
|
@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()
|
2014-03-09 18:13:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
@receiver( post_save, sender=EventParticipation )
|
2014-04-18 13:43:02 +02:00
|
|
|
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()
|
2014-03-09 18:13:30 +01:00
|
|
|
|
|
|
|
|
2014-04-18 13:43:02 +02:00
|
|
|
@receiver( before_read, sender=EventParticipation )
|
|
|
|
def participation_before_read_handler( **kwargs):
|
|
|
|
if not signalLock.isLocked():
|
|
|
|
with signalLock:
|
|
|
|
print("SyncParticipation from google")
|
|
|
|
syncParticipationFromGoogleToLocal()
|