2019-01-05 13:04:20 +01:00
|
|
|
from django.shortcuts import redirect
|
|
|
|
from eventplanner_gcal.google_sync import sync_from_google_to_local, sync_from_local_to_google
|
2014-04-21 09:21:59 +02:00
|
|
|
from django.http import HttpResponse
|
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
2019-01-05 13:04:20 +01:00
|
|
|
from eventplanner_gcal.google_sync import check_if_google_callback_is_valid
|
2014-06-20 12:24:01 +02:00
|
|
|
from eventplanner_gcal.models import UserGCalCoupling
|
|
|
|
from django.shortcuts import render
|
2014-04-21 09:21:59 +02:00
|
|
|
|
|
|
|
import logging
|
|
|
|
|
2019-01-05 13:04:20 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
2014-03-08 22:36:25 +01:00
|
|
|
|
|
|
|
|
2019-01-05 13:04:20 +01:00
|
|
|
def run_sync(request):
|
|
|
|
sync_from_local_to_google()
|
2014-03-08 22:36:25 +01:00
|
|
|
return redirect("/")
|
|
|
|
|
2014-06-20 12:24:01 +02:00
|
|
|
|
2019-01-05 13:04:20 +01:00
|
|
|
def manage(request):
|
2014-06-20 12:24:01 +02:00
|
|
|
if request.method == 'POST':
|
|
|
|
if request.POST['activate'] == "1":
|
2019-01-05 13:04:20 +01:00
|
|
|
UserGCalCoupling.objects.filter(user=request.user).delete()
|
|
|
|
c = UserGCalCoupling(user=request.user, email=request.POST['email'])
|
2014-06-20 12:24:01 +02:00
|
|
|
c.save()
|
2019-01-05 13:04:20 +01:00
|
|
|
sync_from_local_to_google()
|
2014-06-20 12:24:01 +02:00
|
|
|
else:
|
2019-01-05 13:04:20 +01:00
|
|
|
UserGCalCoupling.objects.filter(user=request.user).delete()
|
|
|
|
sync_from_local_to_google()
|
2014-06-20 12:24:01 +02:00
|
|
|
|
|
|
|
context = {}
|
2019-01-05 13:04:20 +01:00
|
|
|
user_coupling = UserGCalCoupling.objects.filter(user=request.user)
|
|
|
|
context['enabled'] = len(user_coupling)
|
|
|
|
assert (len(user_coupling) < 2)
|
|
|
|
if len(user_coupling) == 1:
|
|
|
|
context['mail'] = user_coupling[0].email
|
2014-06-20 12:24:01 +02:00
|
|
|
|
2019-01-05 13:04:20 +01:00
|
|
|
return render(request, 'eventplanner_gcal/management.html', context)
|
2014-06-20 12:24:01 +02:00
|
|
|
|
|
|
|
|
2014-04-21 09:21:59 +02:00
|
|
|
@csrf_exempt
|
2019-01-05 13:04:20 +01:00
|
|
|
def gcal_api_callback(request):
|
2014-06-20 08:57:04 +02:00
|
|
|
token = ""
|
2019-01-05 13:04:20 +01:00
|
|
|
if 'HTTP_X_GOOG_CHANNEL_TOKEN' in request.META:
|
|
|
|
token = request.META['HTTP_X_GOOG_CHANNEL_TOKEN']
|
|
|
|
|
|
|
|
channel_id = ""
|
|
|
|
if 'HTTP_X_GOOG_CHANNEL_ID' in request.META:
|
|
|
|
channel_id = request.META['HTTP_X_GOOG_CHANNEL_ID']
|
2014-04-19 20:36:01 +02:00
|
|
|
|
2019-01-05 13:04:20 +01:00
|
|
|
resource_id = ""
|
|
|
|
if 'HTTP_X_GOOG_RESOURCE_ID' in request.META:
|
|
|
|
resource_id = request.META['HTTP_X_GOOG_RESOURCE_ID']
|
|
|
|
|
|
|
|
valid = check_if_google_callback_is_valid(token, channel_id, resource_id)
|
2014-04-19 20:36:01 +02:00
|
|
|
|
2014-06-20 08:57:04 +02:00
|
|
|
if not valid:
|
|
|
|
return HttpResponse('<h1>Old Channel - no update triggered</h1>')
|
2014-04-19 20:36:01 +02:00
|
|
|
|
2019-01-05 13:04:20 +01:00
|
|
|
logger.info("Received Google Callback with the following headers Token: "
|
|
|
|
"%s ID %s ResID %s " % (token, channel_id, resource_id))
|
|
|
|
result = sync_from_google_to_local()
|
|
|
|
logger.info("Finished processing callback from GCal - New Information present: %d " % (result,))
|
2014-06-20 08:57:04 +02:00
|
|
|
return HttpResponse('<h1>Callback successful</h1>')
|