blechreiz-website/eventplanner_gcal/views.py

65 lines
2.2 KiB
Python

from django.shortcuts import redirect
from eventplanner_gcal.google_sync import syncFromGoogleToLocal, syncFromLocalToGoogle
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from pprint import pformat
from eventplanner_gcal.google_sync import checkIfGoogleCallbackIsValid
from eventplanner_gcal.models import UserGCalCoupling
from django.shortcuts import render
import logging
logger = logging.getLogger( __name__ )
def runSync( request ):
syncFromLocalToGoogle()
return redirect("/")
def manage( request ):
if request.method == 'POST':
if request.POST['activate'] == "1":
UserGCalCoupling.objects.filter( user=request.user ).delete()
c = UserGCalCoupling( user=request.user, email = request.POST['email'] )
c.save()
syncFromLocalToGoogle()
else:
UserGCalCoupling.objects.filter( user=request.user ).delete()
syncFromLocalToGoogle()
context = {}
userCoupling = UserGCalCoupling.objects.filter( user = request.user )
context['enabled'] = len(userCoupling)
assert( len(userCoupling) < 2 )
if len(userCoupling) == 1:
context['mail'] = userCoupling[0].email
return render( request, 'eventplanner_gcal/management.html', context )
@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 = ""
if 'HTTP_X_GOOG_CHANNEL_ID' in request.META: channelID = request.META['HTTP_X_GOOG_CHANNEL_ID']
resourceID = ""
if 'HTTP_X_GOOG_RESOURCE_ID' in request.META: resourceID = request.META['HTTP_X_GOOG_RESOURCE_ID']
valid = checkIfGoogleCallbackIsValid( token, channelID, resourceID)
if not valid:
return HttpResponse('<h1>Old Channel - no update triggered</h1>')
logger.info( "Received Google Callback with the following headers Token: %s ID %s ResID %s " % ( token, channelID, resourceID ) )
result = syncFromGoogleToLocal()
logger.info("Finished processing callback from GCal - New Information present: %d " %(result, ) )
return HttpResponse('<h1>Callback successful</h1>')