blechreiz-website/eventplanner_gcal/signals.py

62 lines
2.0 KiB
Python
Raw Normal View History

from django.db.models.signals import post_save, pre_delete
from django.dispatch import receiver
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
logger = logging.getLogger(__name__)
2014-04-18 13:43:02 +02:00
# @receiver( post_save, sender=User )
# def user_changed( **kwargs ):
# logger.info("Synchronizing with google - user information changed")
# syncFromLocalToGoogle( getServiceObject() )
@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:
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))
@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))
@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))