annotate gcalendar/views.py @ 1096:d9cd3180c12c

More GCalendar V3 conversion in progress. Built a brand new post editor. It is hardcoded into GCalendar right now. We will make it more general in the future.
author Brian Neal <bgneal@gmail.com>
date Tue, 14 Jun 2016 21:16:09 -0500
parents 110bbc78a482
children deed541ca79d
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):
gremmie@1 38 if request.method == 'POST':
gremmie@1 39 form = EventEntryForm(request.POST)
gremmie@1 40 if form.is_valid():
gremmie@1 41 event = form.save(commit=False)
gremmie@1 42 event.user = request.user
gremmie@1 43 event.repeat = 'none'
bgneal@1021 44 event.save(html=form.html)
gremmie@1 45 return HttpResponseRedirect(reverse('gcalendar-add_thanks'))
gremmie@1 46 else:
gremmie@1 47 form = EventEntryForm()
gremmie@1 48
bgneal@1031 49 return render(request, 'gcalendar/event.html', {
gremmie@1 50 'title': 'Add Calendar Event',
gremmie@1 51 'form': form,
bgneal@1096 52 'is_new': True,
bgneal@1094 53 'V3_DESIGN': True,
bgneal@1031 54 })
gremmie@1 55
gremmie@1 56
gremmie@1 57 @login_required
gremmie@1 58 def add_thanks(request):
bgneal@1031 59 return render(request, 'gcalendar/thanks_add.html', {
bgneal@1031 60 })
gremmie@1 61
gremmie@1 62
gremmie@1 63 @login_required
gremmie@1 64 def edit_events(request):
bgneal@817 65 events = Event.objects.filter(user=request.user, status=Event.ON_CAL).\
bgneal@817 66 order_by('-start_date')
bgneal@1094 67 paginator = DiggPaginator(events, 25, body=5, tail=2, margin=3, padding=2)
bgneal@1094 68 num = get_page(request.GET)
bgneal@817 69 try:
bgneal@817 70 page = paginator.page(num)
bgneal@1094 71 except InvalidPage:
bgneal@1094 72 raise Http404
bgneal@817 73
bgneal@1031 74 return render(request, 'gcalendar/edit.html', {
bgneal@817 75 'page': page,
bgneal@1094 76 'V3_DESIGN': True,
bgneal@1031 77 })
gremmie@1 78
gremmie@1 79
gremmie@1 80 @login_required
gremmie@1 81 def edit_event(request, event_id):
gremmie@1 82 event = get_object_or_404(Event, pk=event_id)
gremmie@1 83 if event.user != request.user:
bgneal@1094 84 raise PermissionDenied
gremmie@1 85
gremmie@1 86 if request.method == 'POST':
gremmie@1 87 form = EventEntryForm(request.POST, instance=event)
gremmie@1 88 if form.is_valid():
gremmie@1 89 event = form.save(commit=False)
gremmie@1 90 event.user = request.user
gremmie@1 91 event.repeat = 'none'
gremmie@1 92 event.status = Event.EDIT_REQ
gremmie@1 93 event.save()
gremmie@1 94 return HttpResponseRedirect(reverse('gcalendar-edit_thanks'))
gremmie@1 95 else:
gremmie@1 96 form = EventEntryForm(instance=event)
gremmie@1 97
bgneal@1031 98 return render(request, 'gcalendar/event.html', {
gremmie@1 99 'title': 'Change Calendar Event',
gremmie@1 100 'form': form,
bgneal@1096 101 'is_new': False,
bgneal@1096 102 'V3_DESIGN': True,
bgneal@1031 103 })
gremmie@1 104
gremmie@1 105
gremmie@1 106 @login_required
gremmie@1 107 def edit_thanks(request):
bgneal@1031 108 return render(request, 'gcalendar/thanks_edit.html', {
bgneal@1031 109 })
gremmie@1 110
gremmie@1 111
gremmie@1 112 def delete_event(request):
gremmie@1 113 """This view marks an event for deletion. It is called via AJAX."""
gremmie@1 114 if request.user.is_authenticated():
bgneal@1094 115 event_id = request.POST.get('id')
bgneal@1094 116 if event_id is None or not event_id.isdigit():
gremmie@1 117 return HttpResponseBadRequest()
gremmie@1 118 try:
bgneal@1094 119 event = Event.objects.get(pk=event_id)
gremmie@1 120 except Event.DoesNotExist:
gremmie@1 121 return HttpResponseBadRequest()
gremmie@1 122 if request.user != event.user:
gremmie@1 123 return HttpResponseForbidden()
gremmie@1 124
gremmie@1 125 event.status = Event.DEL_REQ
gremmie@1 126 event.save()
bgneal@1094 127 return HttpResponse(event_id, content_type="text/plain")
gremmie@1 128
gremmie@1 129 return HttpResponseForbidden()