annotate gcalendar/views.py @ 1048:f2a945222593

Add Rob_J's 10 years banner.
author Brian Neal <bgneal@gmail.com>
date Sun, 14 Feb 2016 11:32:31 -0600
parents e1c03da72818
children 110bbc78a482
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
gremmie@1 6 from django.core.urlresolvers import reverse
bgneal@817 7 from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
gremmie@1 8 from django.http import HttpResponse
gremmie@1 9 from django.http import HttpResponseBadRequest
gremmie@1 10 from django.http import HttpResponseForbidden
gremmie@1 11 from django.http import HttpResponseRedirect
gremmie@1 12 from django.http import Http404
gremmie@1 13 from django.shortcuts import get_object_or_404
bgneal@1031 14 from django.shortcuts import render
gremmie@1 15
gremmie@1 16 from gcalendar.forms import EventEntryForm
gremmie@1 17 from gcalendar.models import Event
gremmie@1 18
gremmie@1 19
gremmie@1 20 def index(request):
bgneal@345 21 user = request.user
bgneal@345 22 if user.is_authenticated():
bgneal@789 23 tz = user.profile.time_zone
bgneal@345 24 else:
bgneal@345 25 tz = 'US/Pacific'
bgneal@345 26
bgneal@1031 27 return render(request, 'gcalendar/index.html', {
bgneal@345 28 'tz': tz,
bgneal@1031 29 })
gremmie@1 30
gremmie@1 31
gremmie@1 32 @login_required
gremmie@1 33 def add_event(request):
gremmie@1 34 if request.method == 'POST':
gremmie@1 35 form = EventEntryForm(request.POST)
gremmie@1 36 if form.is_valid():
gremmie@1 37 event = form.save(commit=False)
gremmie@1 38 event.user = request.user
gremmie@1 39 event.repeat = 'none'
bgneal@1021 40 event.save(html=form.html)
gremmie@1 41 return HttpResponseRedirect(reverse('gcalendar-add_thanks'))
gremmie@1 42 else:
gremmie@1 43 form = EventEntryForm()
gremmie@1 44
bgneal@1031 45 return render(request, 'gcalendar/event.html', {
gremmie@1 46 'title': 'Add Calendar Event',
gremmie@1 47 'form': form,
bgneal@1031 48 })
gremmie@1 49
gremmie@1 50
gremmie@1 51 @login_required
gremmie@1 52 def add_thanks(request):
bgneal@1031 53 return render(request, 'gcalendar/thanks_add.html', {
bgneal@1031 54 })
gremmie@1 55
gremmie@1 56
gremmie@1 57 @login_required
gremmie@1 58 def edit_events(request):
bgneal@817 59 events = Event.objects.filter(user=request.user, status=Event.ON_CAL).\
bgneal@817 60 order_by('-start_date')
bgneal@817 61 paginator = Paginator(events, 25)
bgneal@817 62 num = request.GET.get('page')
bgneal@817 63 try:
bgneal@817 64 page = paginator.page(num)
bgneal@817 65 except PageNotAnInteger:
bgneal@817 66 page = paginator.page(1)
bgneal@817 67 except EmptyPage:
bgneal@817 68 page = paginator.page(paginator.num_pages)
bgneal@817 69
bgneal@1031 70 return render(request, 'gcalendar/edit.html', {
bgneal@817 71 'page': page,
bgneal@1031 72 })
gremmie@1 73
gremmie@1 74
gremmie@1 75 @login_required
gremmie@1 76 def edit_event(request, event_id):
gremmie@1 77 event = get_object_or_404(Event, pk=event_id)
gremmie@1 78 if event.user != request.user:
gremmie@1 79 raise Http404
gremmie@1 80
gremmie@1 81 if request.method == 'POST':
gremmie@1 82 form = EventEntryForm(request.POST, instance=event)
gremmie@1 83 if form.is_valid():
gremmie@1 84 event = form.save(commit=False)
gremmie@1 85 event.user = request.user
gremmie@1 86 event.repeat = 'none'
gremmie@1 87 event.status = Event.EDIT_REQ
gremmie@1 88 event.save()
gremmie@1 89 return HttpResponseRedirect(reverse('gcalendar-edit_thanks'))
gremmie@1 90 else:
gremmie@1 91 form = EventEntryForm(instance=event)
gremmie@1 92
bgneal@1031 93 return render(request, 'gcalendar/event.html', {
gremmie@1 94 'title': 'Change Calendar Event',
gremmie@1 95 'form': form,
bgneal@1031 96 })
gremmie@1 97
gremmie@1 98
gremmie@1 99 @login_required
gremmie@1 100 def edit_thanks(request):
bgneal@1031 101 return render(request, 'gcalendar/thanks_edit.html', {
bgneal@1031 102 })
gremmie@1 103
gremmie@1 104
gremmie@1 105 def delete_event(request):
gremmie@1 106 """This view marks an event for deletion. It is called via AJAX."""
gremmie@1 107 if request.user.is_authenticated():
gremmie@1 108 id = request.POST.get('id', None)
gremmie@1 109 if id is None or not id.isdigit():
gremmie@1 110 return HttpResponseBadRequest()
gremmie@1 111 try:
gremmie@1 112 event = Event.objects.get(pk=id)
gremmie@1 113 except Event.DoesNotExist:
gremmie@1 114 return HttpResponseBadRequest()
gremmie@1 115 if request.user != event.user:
gremmie@1 116 return HttpResponseForbidden()
gremmie@1 117
gremmie@1 118 event.status = Event.DEL_REQ
gremmie@1 119 event.save()
gremmie@1 120 return HttpResponse(id)
gremmie@1 121
gremmie@1 122 return HttpResponseForbidden()