annotate gcalendar/views.py @ 1198:3a03c2b2df05

Fix url return value bug.
author Brian Neal <bgneal@gmail.com>
date Sun, 07 May 2023 19:27:19 -0500
parents deed541ca79d
children
rev   line source
gremmie@1 1 """
gremmie@1 2 Views for the gcalendar application.
gremmie@1 3 """
gremmie@1 4
gremmie@1 5 from django.contrib.auth.decorators import login_required
bgneal@1094 6 from django.core.exceptions import PermissionDenied
bgneal@1094 7 from django.core.paginator import InvalidPage
gremmie@1 8 from django.core.urlresolvers import reverse
gremmie@1 9 from django.http import HttpResponse
gremmie@1 10 from django.http import HttpResponseBadRequest
gremmie@1 11 from django.http import HttpResponseForbidden
gremmie@1 12 from django.http import HttpResponseRedirect
gremmie@1 13 from django.http import Http404
gremmie@1 14 from django.shortcuts import get_object_or_404
bgneal@1031 15 from django.shortcuts import render
gremmie@1 16
bgneal@1094 17 from core.functions import get_page
bgneal@1094 18 from core.paginator import DiggPaginator
gremmie@1 19 from gcalendar.forms import EventEntryForm
gremmie@1 20 from gcalendar.models import Event
gremmie@1 21
gremmie@1 22
gremmie@1 23 def index(request):
bgneal@345 24 user = request.user
bgneal@345 25 if user.is_authenticated():
bgneal@789 26 tz = user.profile.time_zone
bgneal@345 27 else:
bgneal@345 28 tz = 'US/Pacific'
bgneal@345 29
bgneal@1031 30 return render(request, 'gcalendar/index.html', {
bgneal@345 31 'tz': tz,
bgneal@1094 32 'V3_DESIGN': True,
bgneal@1031 33 })
gremmie@1 34
gremmie@1 35
gremmie@1 36 @login_required
gremmie@1 37 def add_event(request):
bgneal@1097 38 thanks_flag = False
gremmie@1 39 if request.method == 'POST':
gremmie@1 40 form = EventEntryForm(request.POST)
gremmie@1 41 if form.is_valid():
gremmie@1 42 event = form.save(commit=False)
gremmie@1 43 event.user = request.user
gremmie@1 44 event.repeat = 'none'
bgneal@1021 45 event.save(html=form.html)
bgneal@1097 46 return HttpResponseRedirect(reverse('gcalendar-add') + '?thanks=1')
gremmie@1 47 else:
gremmie@1 48 form = EventEntryForm()
bgneal@1097 49 thanks_flag = bool(request.GET.get('thanks', False))
gremmie@1 50
bgneal@1031 51 return render(request, 'gcalendar/event.html', {
gremmie@1 52 'title': 'Add Calendar Event',
gremmie@1 53 'form': form,
bgneal@1096 54 'is_new': True,
bgneal@1097 55 'thanks_flag': thanks_flag,
bgneal@1094 56 'V3_DESIGN': True,
bgneal@1031 57 })
gremmie@1 58
gremmie@1 59
gremmie@1 60 @login_required
gremmie@1 61 def edit_events(request):
bgneal@817 62 events = Event.objects.filter(user=request.user, status=Event.ON_CAL).\
bgneal@817 63 order_by('-start_date')
bgneal@1094 64 paginator = DiggPaginator(events, 25, body=5, tail=2, margin=3, padding=2)
bgneal@1094 65 num = get_page(request.GET)
bgneal@817 66 try:
bgneal@817 67 page = paginator.page(num)
bgneal@1094 68 except InvalidPage:
bgneal@1094 69 raise Http404
bgneal@817 70
bgneal@1031 71 return render(request, 'gcalendar/edit.html', {
bgneal@817 72 'page': page,
bgneal@1097 73 'thanks_flag': bool(request.GET.get('thanks', False)),
bgneal@1094 74 'V3_DESIGN': True,
bgneal@1031 75 })
gremmie@1 76
gremmie@1 77
gremmie@1 78 @login_required
gremmie@1 79 def edit_event(request, event_id):
gremmie@1 80 event = get_object_or_404(Event, pk=event_id)
gremmie@1 81 if event.user != request.user:
bgneal@1094 82 raise PermissionDenied
gremmie@1 83
gremmie@1 84 if request.method == 'POST':
gremmie@1 85 form = EventEntryForm(request.POST, instance=event)
gremmie@1 86 if form.is_valid():
gremmie@1 87 event = form.save(commit=False)
gremmie@1 88 event.user = request.user
gremmie@1 89 event.repeat = 'none'
gremmie@1 90 event.status = Event.EDIT_REQ
gremmie@1 91 event.save()
bgneal@1097 92 return HttpResponseRedirect(reverse('gcalendar-edit_events') + '?thanks=1')
gremmie@1 93 else:
gremmie@1 94 form = EventEntryForm(instance=event)
gremmie@1 95
bgneal@1031 96 return render(request, 'gcalendar/event.html', {
gremmie@1 97 'title': 'Change Calendar Event',
gremmie@1 98 'form': form,
bgneal@1096 99 'is_new': False,
bgneal@1096 100 'V3_DESIGN': True,
bgneal@1031 101 })
gremmie@1 102
gremmie@1 103
gremmie@1 104 def delete_event(request):
gremmie@1 105 """This view marks an event for deletion. It is called via AJAX."""
gremmie@1 106 if request.user.is_authenticated():
bgneal@1094 107 event_id = request.POST.get('id')
bgneal@1094 108 if event_id is None or not event_id.isdigit():
gremmie@1 109 return HttpResponseBadRequest()
gremmie@1 110 try:
bgneal@1094 111 event = Event.objects.get(pk=event_id)
gremmie@1 112 except Event.DoesNotExist:
gremmie@1 113 return HttpResponseBadRequest()
gremmie@1 114 if request.user != event.user:
gremmie@1 115 return HttpResponseForbidden()
gremmie@1 116
gremmie@1 117 event.status = Event.DEL_REQ
gremmie@1 118 event.save()
bgneal@1094 119 return HttpResponse(event_id, content_type="text/plain")
gremmie@1 120
gremmie@1 121 return HttpResponseForbidden()