port to new django, AI automated
This commit is contained in:
@@ -1,63 +1,101 @@
|
||||
from django.shortcuts import redirect
|
||||
from eventplanner_gcal.google_sync import sync_from_google_to_local, sync_from_local_to_google
|
||||
from django.http import HttpResponse
|
||||
from django.views.decorators.csrf import csrf_exempt
|
||||
from eventplanner_gcal.google_sync import check_if_google_callback_is_valid
|
||||
from eventplanner_gcal.models import UserGCalCoupling
|
||||
from django.shortcuts import render
|
||||
"""
|
||||
Views for Google Calendar integration management.
|
||||
"""
|
||||
|
||||
import logging
|
||||
|
||||
from django.http import HttpResponse
|
||||
from django.shortcuts import redirect, render
|
||||
from django.views.decorators.csrf import csrf_exempt
|
||||
|
||||
from .google_sync import (
|
||||
check_if_google_callback_is_valid,
|
||||
sync_from_google_to_local,
|
||||
sync_from_local_to_google,
|
||||
)
|
||||
from .models import UserGCalCoupling
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def run_sync(request):
|
||||
"""Manually trigger a sync from local to Google Calendar."""
|
||||
sync_from_local_to_google()
|
||||
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()
|
||||
sync_from_local_to_google()
|
||||
"""
|
||||
View for managing Google Calendar integration settings.
|
||||
|
||||
Allows users to enable/disable GCal sync and configure their email.
|
||||
"""
|
||||
if request.method == "POST":
|
||||
activate = request.POST.get("activate", "0")
|
||||
|
||||
if activate == "1":
|
||||
# Enable GCal coupling
|
||||
email = request.POST.get("email", "")
|
||||
if email:
|
||||
UserGCalCoupling.objects.filter(user=request.user).delete()
|
||||
coupling = UserGCalCoupling(user=request.user, email=email)
|
||||
coupling.save()
|
||||
sync_from_local_to_google()
|
||||
else:
|
||||
# Disable GCal coupling
|
||||
UserGCalCoupling.objects.filter(user=request.user).delete()
|
||||
sync_from_local_to_google()
|
||||
|
||||
context = {}
|
||||
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
|
||||
context["enabled"] = user_coupling.exists()
|
||||
|
||||
return render(request, 'eventplanner_gcal/management.html', context)
|
||||
if user_coupling.count() > 1:
|
||||
logger.warning(
|
||||
f"User {request.user.username} has multiple GCal couplings. "
|
||||
"This should not happen."
|
||||
)
|
||||
|
||||
if user_coupling.exists():
|
||||
context["mail"] = user_coupling.first().email
|
||||
|
||||
return render(request, "eventplanner_gcal/management.html", context)
|
||||
|
||||
|
||||
@csrf_exempt
|
||||
def gcal_api_callback(request):
|
||||
token = ""
|
||||
if 'HTTP_X_GOOG_CHANNEL_TOKEN' in request.META:
|
||||
token = request.META['HTTP_X_GOOG_CHANNEL_TOKEN']
|
||||
"""
|
||||
Callback endpoint for Google Calendar push notifications.
|
||||
|
||||
channel_id = ""
|
||||
if 'HTTP_X_GOOG_CHANNEL_ID' in request.META:
|
||||
channel_id = request.META['HTTP_X_GOOG_CHANNEL_ID']
|
||||
|
||||
resource_id = ""
|
||||
if 'HTTP_X_GOOG_RESOURCE_ID' in request.META:
|
||||
resource_id = request.META['HTTP_X_GOOG_RESOURCE_ID']
|
||||
This is called by Google when calendar events are updated.
|
||||
"""
|
||||
token = request.META.get("HTTP_X_GOOG_CHANNEL_TOKEN", "")
|
||||
channel_id = request.META.get("HTTP_X_GOOG_CHANNEL_ID", "")
|
||||
resource_id = request.META.get("HTTP_X_GOOG_RESOURCE_ID", "")
|
||||
|
||||
valid = check_if_google_callback_is_valid(token, channel_id, resource_id)
|
||||
|
||||
if not valid:
|
||||
return HttpResponse('<h1>Old Channel - no update triggered</h1>')
|
||||
logger.warning(
|
||||
f"Received invalid GCal callback: token={token}, "
|
||||
f"channel_id={channel_id}, resource_id={resource_id}"
|
||||
)
|
||||
return HttpResponse("<h1>Old Channel - no update triggered</h1>")
|
||||
|
||||
logger.info(
|
||||
f"Received Google Callback with headers - "
|
||||
f"Token: {token}, ID: {channel_id}, ResID: {resource_id}"
|
||||
)
|
||||
|
||||
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,))
|
||||
return HttpResponse('<h1>Callback successful</h1>')
|
||||
|
||||
logger.info(
|
||||
f"Finished processing callback from GCal - New Information present: {result}"
|
||||
)
|
||||
|
||||
return HttpResponse("<h1>Callback successful</h1>")
|
||||
|
||||
|
||||
# Backwards compatibility aliases
|
||||
runSync = run_sync
|
||||
gcalApiCallback = gcal_api_callback
|
||||
|
||||
Reference in New Issue
Block a user