2019-01-05 11:27:15 +01:00
|
|
|
from django.db.models.signals import post_save, pre_delete
|
2014-03-09 18:13:30 +01:00
|
|
|
from django.dispatch import receiver
|
2014-04-19 20:36:01 +02:00
|
|
|
from eventplanner.models import Event, EventParticipation
|
2019-01-05 13:04:20 +01:00
|
|
|
from eventplanner_gcal.google_sync import get_service_object, \
|
|
|
|
create_gcal_event, delete_gcal_event, update_gcal_event, on_gcal_event_created
|
2014-04-18 13:43:02 +02:00
|
|
|
|
2014-04-26 11:17:10 +02:00
|
|
|
import logging
|
2023-01-03 20:13:35 +01:00
|
|
|
from django.conf import settings
|
2014-04-18 13:43:02 +02:00
|
|
|
|
2019-01-05 11:27:15 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
2014-04-18 13:43:02 +02:00
|
|
|
|
2019-01-05 11:27:15 +01:00
|
|
|
|
|
|
|
# @receiver( post_save, sender=User )
|
|
|
|
# def user_changed( **kwargs ):
|
2014-06-20 12:24:01 +02:00
|
|
|
# logger.info("Synchronizing with google - user information changed")
|
|
|
|
# syncFromLocalToGoogle( getServiceObject() )
|
2014-04-19 20:36:01 +02:00
|
|
|
|
2014-03-09 18:13:30 +01:00
|
|
|
|
2019-01-05 11:27:15 +01:00
|
|
|
@receiver(post_save, sender=Event)
|
|
|
|
def event_post_save_handler(**kwargs):
|
2023-01-03 20:13:35 +01:00
|
|
|
if not settings.GCAL_SYNC_ENABLED:
|
|
|
|
return
|
|
|
|
|
2014-04-26 11:17:10 +02:00
|
|
|
event = kwargs['instance']
|
|
|
|
created = kwargs['created']
|
2014-09-15 11:12:04 +02:00
|
|
|
try:
|
|
|
|
if created:
|
|
|
|
logger.info("Creating Gcal event")
|
2019-01-05 13:04:20 +01:00
|
|
|
response = create_gcal_event(get_service_object(), event).execute()
|
|
|
|
on_gcal_event_created(None, response, None)
|
2014-09-15 11:12:04 +02:00
|
|
|
else:
|
2019-01-05 11:27:15 +01:00
|
|
|
logger.info("Updating Gcal event")
|
2019-01-05 13:04:20 +01:00
|
|
|
update_gcal_event(get_service_object(), event).execute()
|
|
|
|
except Exception as e:
|
|
|
|
logger.error("Error updating Gcal event" + str(e))
|
2014-03-09 18:13:30 +01:00
|
|
|
|
|
|
|
|
2019-01-05 11:27:15 +01:00
|
|
|
@receiver(pre_delete, sender=Event)
|
|
|
|
def event_pre_delete_handler(**kwargs):
|
2023-01-03 20:13:35 +01:00
|
|
|
if not settings.GCAL_SYNC_ENABLED:
|
|
|
|
return
|
|
|
|
|
2014-09-15 11:12:04 +02:00
|
|
|
try:
|
|
|
|
event = kwargs['instance']
|
2023-01-03 20:13:35 +01:00
|
|
|
logger.info("Deleting Gcal event")
|
2019-01-05 13:04:20 +01:00
|
|
|
delete_gcal_event(get_service_object(), event).execute()
|
|
|
|
except Exception as e:
|
|
|
|
logger.error("Error deleting GCAL event" + str(e))
|
2014-03-09 18:13:30 +01:00
|
|
|
|
|
|
|
|
2019-01-05 11:27:15 +01:00
|
|
|
@receiver(post_save, sender=EventParticipation)
|
|
|
|
def participation_post_save_handler(**kwargs):
|
2023-01-03 20:13:35 +01:00
|
|
|
if not settings.GCAL_SYNC_ENABLED:
|
|
|
|
return
|
|
|
|
|
2014-09-15 11:12:04 +02:00
|
|
|
try:
|
|
|
|
participation = kwargs['instance']
|
|
|
|
logger.info("Participation post save -> update gcal")
|
2019-01-05 13:04:20 +01:00
|
|
|
update_gcal_event(get_service_object(), participation.event).execute()
|
|
|
|
except Exception as e:
|
|
|
|
logger.error("Error deleting GCAL event" + str(e))
|