diff --git a/eventplanner/models.py b/eventplanner/models.py index 64b33c2..58eb907 100644 --- a/eventplanner/models.py +++ b/eventplanner/models.py @@ -51,12 +51,12 @@ class Event ( models.Model ): # Create a "Don't Know" participation for each Musician for u in User.objects.all(): if not u in self.participants.all(): - EventParticipation.objects.create( event=self, user = u, status='?', comment = '' ) + EventParticipation.objects.create( event=self, user = u, status='-', comment = '' ) @property def title(self): res = self.get_type_display() - if ( self.short_desc ): + if self.short_desc: res += " (" + self.short_desc + ") " return res diff --git a/eventplanner_gcal/google_sync.py b/eventplanner_gcal/google_sync.py index 2ff1ffb..491109b 100644 --- a/eventplanner_gcal/google_sync.py +++ b/eventplanner_gcal/google_sync.py @@ -58,18 +58,27 @@ def buildGCalAttendeesObj( event ): for userMapping in UserGCalCoupling.objects.all(): u = userMapping.user - participation = EventParticipation.get_or_create( u, event ) + + # No get or create here, since a create would trigger another synchronization + #participation = EventParticipation.get_or_create( u, event ) + try: + participation = EventParticipation.objects.get( event = event, user = u ) + localStatus = participation.status + localComment = participation.comment + except EventParticipation.DoesNotExist: + localStatus = "-" + localComment = "" status = "needsAction" - if participation.status == "?" : status = "tentative" - if participation.status == 'Yes': status = "accepted" - if participation.status == 'No' : status = "declined" + if localStatus == "?" : status = "tentative" + if localStatus == 'Yes': status = "accepted" + if localStatus == 'No' : status = "declined" o = { 'id': userMapping.email, 'email': userMapping.email, 'displayName': u.username, - 'comment': participation.comment, + 'comment': localComment, 'responseStatus': status, } result.append( o ) @@ -115,6 +124,18 @@ def buildGCalEvent( event, timezone="Europe/Berlin" ): } +# ------------------------------ Callback Functions ------------------------------------------------ + +def onGcalEventCreated( request_id, response, exception=None ): + """Callback function for created events to enter new gcal id in the mapping table""" + if exception is not None: + print ( "response " + str( response ) ) + raise exception + + googleId = response['id'] + djangoId = response['extendedProperties']['private']['blechreizID'] + mapping = GCalMapping( gcal_id = googleId, event = Event.objects.get( pk=djangoId ) ) + mapping.save() # ------------------------------ GCal Api Calls ------------------------------------------------- @@ -142,19 +163,23 @@ def getAllGCalEvents( service, fromNow=False ): def createGCalEvent( service, event, timezone="Europe/Berlin" ): """Creates a new gcal event using a local event""" googleEvent = buildGCalEvent(event,timezone) - return service.events().insert(calendarId='primary', body=googleEvent) + return service.events().insert(calendarId='primary', body=googleEvent ) def updateGCalEvent( service, event, timezone="Europe/Berlin"): """Updates an existing gcal event, using a local event""" googleEvent = buildGCalEvent(event,timezone) - mapping = GCalMapping.objects.get( event=event ) - gcalId = mapping.gcal_id - return service.events().patch(calendarId='primary', eventId= gcalId, body=googleEvent) + try: + mapping = GCalMapping.objects.get( event=event ) + except GCalMapping.DoesNotExist: + return createGCalEvent( service, event, timezone ) + + return service.events().patch(calendarId='primary', eventId= mapping.gcal_id, body=googleEvent) def deleteGCalEvent( service, event ): """Deletes gcal that belongs to the given local event""" mapping = GCalMapping.objects.get( event=event ) gcalId = mapping.gcal_id + mapping.delete() return service.events().delete(calendarId='primary', eventId=gcalId) @@ -176,6 +201,8 @@ def deleteAllGCalEvents( service = getServiceObject() ): batch.add( service.events().delete(calendarId='primary', eventId=id) ) batch.execute() + GCalMapping.objects.all().delete() + return l def syncFromLocalToGoogle( service = None ): @@ -198,16 +225,7 @@ def syncFromLocalToGoogle( service = None ): eventsToCreate_djangoID = localEvents_djangoID - eventsAtGoogle_djangoID eventsToDelete_googleID = eventsAtGoogle_googleID - localEvents_googleID - def onGcalEventCreated( request_id, response, exception ): - """Callback function for created events to enter new gcal id in the mapping table""" - if exception is not None: - print ( "response " + str( response ) ) - raise exception - googleId = response['id'] - djangoId = response['extendedProperties']['private']['blechreizID'] - mapping = GCalMapping( gcal_id = googleId, event = Event.objects.get( pk=djangoId ) ) - mapping.save() diff --git a/eventplanner_gcal/signals.py b/eventplanner_gcal/signals.py index 7cd5927..ea0b225 100644 --- a/eventplanner_gcal/signals.py +++ b/eventplanner_gcal/signals.py @@ -3,7 +3,7 @@ from django.dispatch import receiver from eventplanner.models import Event, EventParticipation from django.contrib.auth.models import User from eventplanner_gcal.google_sync import getServiceObject, syncFromLocalToGoogle,\ - createGCalEvent, deleteGCalEvent, updateGCalEvent + createGCalEvent, deleteGCalEvent, updateGCalEvent, onGcalEventCreated import logging logger = logging.getLogger( __name__ ) @@ -21,7 +21,8 @@ def event_post_save_handler( **kwargs): created = kwargs['created'] if created: logger.info("Creating Gcal event") - createGCalEvent( getServiceObject(), event ).execute() + response = createGCalEvent( getServiceObject(), event ).execute() + onGcalEventCreated( None, response, None ) else: logger.info( "Updating Gcal event") updateGCalEvent( getServiceObject(),event ).execute() diff --git a/eventplanner_gcal/views.py b/eventplanner_gcal/views.py index 98252d1..dc1402f 100644 --- a/eventplanner_gcal/views.py +++ b/eventplanner_gcal/views.py @@ -41,7 +41,6 @@ def manage( request ): @csrf_exempt def gcalApiCallback( request ): - # TODO check channel info here token = "" if 'HTTP_X_GOOG_CHANNEL_TOKEN' in request.META: token = request.META['HTTP_X_GOOG_CHANNEL_TOKEN'] channelID = ""