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 getServiceObject, \ createGCalEvent, deleteGCalEvent, updateGCalEvent, onGcalEventCreated import logging 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): event = kwargs['instance'] created = kwargs['created'] try: if created: logger.info("Creating Gcal event") response = createGCalEvent(getServiceObject(), event).execute() onGcalEventCreated(None, response, None) else: logger.info("Updating Gcal event") updateGCalEvent(getServiceObject(), event).execute() except: logger.error("Error updating Gcal event") @receiver(pre_delete, sender=Event) def event_pre_delete_handler(**kwargs): try: event = kwargs['instance'] logger.info("Deleting GCAL event") deleteGCalEvent(getServiceObject(), event).execute() except: logger.error("Error deleting GCAL event") @receiver(post_save, sender=EventParticipation) def participation_post_save_handler(**kwargs): try: participation = kwargs['instance'] logger.info("Participation post save -> update gcal") updateGCalEvent(getServiceObject(), participation.event).execute() except: logger.error("Error deleting GCAL event")