GCal Mapping: Management commands and signals
This commit is contained in:
@@ -65,7 +65,7 @@ def createAttendeesObj( event ):
|
||||
return result
|
||||
|
||||
|
||||
def createEvent( event, timezone="Europe/Berlin" ):
|
||||
def buildGCalEvent( event, timezone="Europe/Berlin" ):
|
||||
if service is None:
|
||||
logger.error("createEvent: Google API connection not configured")
|
||||
return
|
||||
@@ -89,7 +89,7 @@ def createEvent( event, timezone="Europe/Berlin" ):
|
||||
else:
|
||||
endTime = datetime.time( 22, 30 )
|
||||
|
||||
googleEvent = {
|
||||
return {
|
||||
'summary': unicode(settings.GCAL_COUPLING['eventPrefix'] + event.title),
|
||||
'description': unicode(event.desc),
|
||||
'location': unicode(event.location),
|
||||
@@ -103,10 +103,10 @@ def createEvent( event, timezone="Europe/Berlin" ):
|
||||
},
|
||||
'attendees': createAttendeesObj( event ),
|
||||
}
|
||||
return service.events().insert(calendarId='primary', body=googleEvent)
|
||||
|
||||
# -------------------------------------------------------------------------------
|
||||
|
||||
def getAllEvents(pageToken=None):
|
||||
def getAllGCalEvents(pageToken=None):
|
||||
events = service.events().list(
|
||||
calendarId='primary',
|
||||
singleEvents=True,
|
||||
@@ -120,15 +120,43 @@ def getAllEvents(pageToken=None):
|
||||
return events['items']
|
||||
|
||||
|
||||
def onGcalEventCreated( request_id, response, exception ):
|
||||
if exception is not None:
|
||||
print ( "response " + str( response ) )
|
||||
raise exception
|
||||
def createGCalEvent( event, timezone="Europe/Berlin" ):
|
||||
googleEvent = buildGCalEvent(event,timezone)
|
||||
return service.events().insert(calendarId='primary', body=googleEvent)
|
||||
|
||||
def updateGCalEvent( event, timezone="Europe/Berlin"):
|
||||
googleEvent = buildGCalEvent(event,timezone)
|
||||
mapping = GCalMapping.objects.get( event=event )
|
||||
gcalId = mapping.gcal_id
|
||||
return service.events().patch(calendarId='primary', eventId= gcalId, body=googleEvent)
|
||||
|
||||
def deleteGCalEvent( event ):
|
||||
mapping = GCalMapping.objects.get( event=event )
|
||||
gcalId = mapping.gcal_id
|
||||
return service.events().delete(calendarId='primary', eventId=gcalId)
|
||||
|
||||
|
||||
|
||||
# -------------------------------------------------------------------------------
|
||||
|
||||
def deleteAllGCalEvents():
|
||||
if service is None:
|
||||
logger.error("deleteAllGCalEvents: Google API connection not configured")
|
||||
return
|
||||
|
||||
gcalIds = [ ev['id'] for ev in getAllGCalEvents() ]
|
||||
l = len(gcalIds)
|
||||
if l == 0:
|
||||
return l
|
||||
|
||||
batch = BatchHttpRequest()
|
||||
for id in gcalIds:
|
||||
batch.add( service.events().delete(calendarId='primary', eventId=id) )
|
||||
batch.execute()
|
||||
|
||||
return l
|
||||
|
||||
|
||||
googleId = response['id']
|
||||
djangoId = response['extendedProperties']['private']['blechreizID']
|
||||
mapping = GCalMapping( gcal_id = googleId, event = Event.objects.get( pk=djangoId ) )
|
||||
mapping.save()
|
||||
|
||||
|
||||
def syncGCalEvents():
|
||||
@@ -136,7 +164,7 @@ def syncGCalEvents():
|
||||
logger.error("syncGCalEvents: Google API connection not configured")
|
||||
return
|
||||
|
||||
allEvents = getAllEvents()
|
||||
allEvents = getAllGCalEvents()
|
||||
|
||||
eventsAtGoogle_djangoID = set()
|
||||
eventsAtGoogle_googleID = set()
|
||||
@@ -151,11 +179,23 @@ def syncGCalEvents():
|
||||
eventsToDelete_googleID = eventsAtGoogle_googleID - localEvents_googleID
|
||||
|
||||
|
||||
def onGcalEventCreated( request_id, response, exception ):
|
||||
"""Callback function for created events"""
|
||||
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()
|
||||
|
||||
|
||||
batch = BatchHttpRequest()
|
||||
|
||||
batchIsEmpty = True
|
||||
for eventDjangoID in eventsToCreate_djangoID:
|
||||
batch.add( createEvent( Event.objects.get( pk=eventDjangoID ) ), callback=onGcalEventCreated )
|
||||
batch.add( createGCalEvent( Event.objects.get( pk=eventDjangoID ) ), callback=onGcalEventCreated )
|
||||
batchIsEmpty=False
|
||||
|
||||
for eventGoogleID in eventsToDelete_googleID:
|
||||
@@ -165,7 +205,6 @@ def syncGCalEvents():
|
||||
if not batchIsEmpty:
|
||||
batch.execute()
|
||||
|
||||
|
||||
|
||||
return len (eventsToCreate_djangoID), len(eventsToDelete_googleID )
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user