blechreiz-website/eventplanner_gcal/signals.py

62 lines
2.0 KiB
Python

from django.db.models.signals import post_save, pre_delete
from django.dispatch import receiver
from eventplanner.models import Event, EventParticipation
from eventplanner_gcal.google_sync import get_service_object, \
create_gcal_event, delete_gcal_event, update_gcal_event, on_gcal_event_created
import logging
from django.conf import settings
logger = logging.getLogger(__name__)
# @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):
if not settings.GCAL_SYNC_ENABLED:
return
event = kwargs['instance']
created = kwargs['created']
try:
if created:
logger.info("Creating Gcal event")
response = create_gcal_event(get_service_object(), event).execute()
on_gcal_event_created(None, response, None)
else:
logger.info("Updating Gcal event")
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):
if not settings.GCAL_SYNC_ENABLED:
return
try:
event = kwargs['instance']
logger.info("Deleting Gcal event")
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):
if not settings.GCAL_SYNC_ENABLED:
return
try:
participation = kwargs['instance']
logger.info("Participation post save -> update gcal")
update_gcal_event(get_service_object(), participation.event).execute()
except Exception as e:
logger.error("Error deleting GCAL event" + str(e))