Bugfixes in Eventplanning & gcal coupling module

This commit is contained in:
Martin Bauer 2014-06-22 11:52:26 +02:00
parent df22b838fa
commit 568376fbe7
4 changed files with 41 additions and 23 deletions

View File

@ -51,12 +51,12 @@ class Event ( models.Model ):
# Create a "Don't Know" participation for each Musician # Create a "Don't Know" participation for each Musician
for u in User.objects.all(): for u in User.objects.all():
if not u in self.participants.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 @property
def title(self): def title(self):
res = self.get_type_display() res = self.get_type_display()
if ( self.short_desc ): if self.short_desc:
res += " (" + self.short_desc + ") " res += " (" + self.short_desc + ") "
return res return res

View File

@ -58,18 +58,27 @@ def buildGCalAttendeesObj( event ):
for userMapping in UserGCalCoupling.objects.all(): for userMapping in UserGCalCoupling.objects.all():
u = userMapping.user 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" status = "needsAction"
if participation.status == "?" : status = "tentative" if localStatus == "?" : status = "tentative"
if participation.status == 'Yes': status = "accepted" if localStatus == 'Yes': status = "accepted"
if participation.status == 'No' : status = "declined" if localStatus == 'No' : status = "declined"
o = { o = {
'id': userMapping.email, 'id': userMapping.email,
'email': userMapping.email, 'email': userMapping.email,
'displayName': u.username, 'displayName': u.username,
'comment': participation.comment, 'comment': localComment,
'responseStatus': status, 'responseStatus': status,
} }
result.append( o ) 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 ------------------------------------------------- # ------------------------------ GCal Api Calls -------------------------------------------------
@ -142,19 +163,23 @@ def getAllGCalEvents( service, fromNow=False ):
def createGCalEvent( service, event, timezone="Europe/Berlin" ): def createGCalEvent( service, event, timezone="Europe/Berlin" ):
"""Creates a new gcal event using a local event""" """Creates a new gcal event using a local event"""
googleEvent = buildGCalEvent(event,timezone) 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"): def updateGCalEvent( service, event, timezone="Europe/Berlin"):
"""Updates an existing gcal event, using a local event""" """Updates an existing gcal event, using a local event"""
googleEvent = buildGCalEvent(event,timezone) googleEvent = buildGCalEvent(event,timezone)
mapping = GCalMapping.objects.get( event=event ) try:
gcalId = mapping.gcal_id mapping = GCalMapping.objects.get( event=event )
return service.events().patch(calendarId='primary', eventId= gcalId, body=googleEvent) except GCalMapping.DoesNotExist:
return createGCalEvent( service, event, timezone )
return service.events().patch(calendarId='primary', eventId= mapping.gcal_id, body=googleEvent)
def deleteGCalEvent( service, event ): def deleteGCalEvent( service, event ):
"""Deletes gcal that belongs to the given local event""" """Deletes gcal that belongs to the given local event"""
mapping = GCalMapping.objects.get( event=event ) mapping = GCalMapping.objects.get( event=event )
gcalId = mapping.gcal_id gcalId = mapping.gcal_id
mapping.delete()
return service.events().delete(calendarId='primary', eventId=gcalId) 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.add( service.events().delete(calendarId='primary', eventId=id) )
batch.execute() batch.execute()
GCalMapping.objects.all().delete()
return l return l
def syncFromLocalToGoogle( service = None ): def syncFromLocalToGoogle( service = None ):
@ -198,16 +225,7 @@ def syncFromLocalToGoogle( service = None ):
eventsToCreate_djangoID = localEvents_djangoID - eventsAtGoogle_djangoID eventsToCreate_djangoID = localEvents_djangoID - eventsAtGoogle_djangoID
eventsToDelete_googleID = eventsAtGoogle_googleID - localEvents_googleID 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()

View File

@ -3,7 +3,7 @@ from django.dispatch import receiver
from eventplanner.models import Event, EventParticipation from eventplanner.models import Event, EventParticipation
from django.contrib.auth.models import User from django.contrib.auth.models import User
from eventplanner_gcal.google_sync import getServiceObject, syncFromLocalToGoogle,\ from eventplanner_gcal.google_sync import getServiceObject, syncFromLocalToGoogle,\
createGCalEvent, deleteGCalEvent, updateGCalEvent createGCalEvent, deleteGCalEvent, updateGCalEvent, onGcalEventCreated
import logging import logging
logger = logging.getLogger( __name__ ) logger = logging.getLogger( __name__ )
@ -21,7 +21,8 @@ def event_post_save_handler( **kwargs):
created = kwargs['created'] created = kwargs['created']
if created: if created:
logger.info("Creating Gcal event") logger.info("Creating Gcal event")
createGCalEvent( getServiceObject(), event ).execute() response = createGCalEvent( getServiceObject(), event ).execute()
onGcalEventCreated( None, response, None )
else: else:
logger.info( "Updating Gcal event") logger.info( "Updating Gcal event")
updateGCalEvent( getServiceObject(),event ).execute() updateGCalEvent( getServiceObject(),event ).execute()

View File

@ -41,7 +41,6 @@ def manage( request ):
@csrf_exempt @csrf_exempt
def gcalApiCallback( request ): def gcalApiCallback( request ):
# TODO check channel info here
token = "" token = ""
if 'HTTP_X_GOOG_CHANNEL_TOKEN' in request.META: token = request.META['HTTP_X_GOOG_CHANNEL_TOKEN'] if 'HTTP_X_GOOG_CHANNEL_TOKEN' in request.META: token = request.META['HTTP_X_GOOG_CHANNEL_TOKEN']
channelID = "" channelID = ""