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-19 20:36:01 +02:00
|
|
|
from eventplanner.models import Event, EventParticipation
|
|
|
|
from django.contrib.auth.models import User
|
2014-04-18 13:43:02 +02:00
|
|
|
|
|
|
|
|
2014-04-19 20:36:01 +02:00
|
|
|
import eventplanner_gcal.models
|
2014-04-18 13:43:02 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
2014-04-19 20:36:01 +02:00
|
|
|
@receiver( post_save, sender=User )
|
|
|
|
def user_changed( **kwargs ):
|
|
|
|
if not signalLock.isLocked():
|
|
|
|
with signalLock:
|
|
|
|
eventplanner_gcal.models.syncGCalEvents()
|
|
|
|
|
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")
|
2014-04-19 20:36:01 +02:00
|
|
|
eventplanner_gcal.models.createGCalEvent( event ).execute()
|
2014-04-18 13:43:02 +02:00
|
|
|
else:
|
|
|
|
print( "Updating Gcal event")
|
2014-04-19 20:36:01 +02:00
|
|
|
eventplanner_gcal.models.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")
|
2014-04-19 20:36:01 +02:00
|
|
|
eventplanner_gcal.models.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")
|
2014-04-19 20:36:01 +02:00
|
|
|
eventplanner_gcal.models.updateGCalEvent( participation.event ).execute()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# -------------- For management commands ------------------------
|
|
|
|
|
|
|
|
|
|
|
|
def stopAllGCalSubscriptions():
|
|
|
|
if not signalLock.isLocked():
|
|
|
|
with signalLock:
|
|
|
|
eventplanner_gcal.models.stopAllGCalSubscriptions()
|
|
|
|
|
|
|
|
def checkGCalSubscription():
|
|
|
|
if not signalLock.isLocked():
|
|
|
|
with signalLock:
|
|
|
|
eventplanner_gcal.models.checkGCalSubscription()
|
2014-03-09 18:13:30 +01:00
|
|
|
|
|
|
|
|
2014-04-19 20:36:01 +02:00
|
|
|
def deleteAllGCalEvents():
|
2014-04-18 13:43:02 +02:00
|
|
|
if not signalLock.isLocked():
|
|
|
|
with signalLock:
|
2014-04-19 20:36:01 +02:00
|
|
|
eventplanner_gcal.models.deleteAllGCalEvents()
|
|
|
|
|
|
|
|
def syncGCalEvents():
|
|
|
|
if not signalLock.isLocked():
|
|
|
|
with signalLock:
|
|
|
|
eventplanner_gcal.models.syncGCalEvents()
|
|
|
|
|