annotate gcalendar/views.py @ 887:9a15f7c27526

Actually save model object upon change. This commit was tested on the comments model. Additional logging added. Added check for Markdown image references. Added TODOs after observing behavior on comments.
author Brian Neal <bgneal@gmail.com>
date Tue, 03 Feb 2015 21:09:44 -0600
parents 74e84f5fc948
children 68c3343f3318
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 render_to_response
gremmie@1 14 from django.shortcuts import get_object_or_404
gremmie@1 15 from django.template import RequestContext
gremmie@1 16
gremmie@1 17 from gcalendar.forms import EventEntryForm
gremmie@1 18 from gcalendar.models import Event
gremmie@1 19
gremmie@1 20
gremmie@1 21 def index(request):
bgneal@345 22 user = request.user
bgneal@345 23 if user.is_authenticated():
bgneal@789 24 tz = user.profile.time_zone
bgneal@345 25 else:
bgneal@345 26 tz = 'US/Pacific'
bgneal@345 27
gremmie@1 28 return render_to_response('gcalendar/index.html', {
bgneal@345 29 'tz': tz,
gremmie@1 30 },
gremmie@1 31 context_instance = RequestContext(request))
gremmie@1 32
gremmie@1 33
gremmie@1 34 @login_required
gremmie@1 35 def add_event(request):
gremmie@1 36 if request.method == 'POST':
gremmie@1 37 form = EventEntryForm(request.POST)
gremmie@1 38 if form.is_valid():
gremmie@1 39 event = form.save(commit=False)
gremmie@1 40 event.user = request.user
gremmie@1 41 event.repeat = 'none'
gremmie@1 42 event.save()
gremmie@1 43 return HttpResponseRedirect(reverse('gcalendar-add_thanks'))
gremmie@1 44 else:
gremmie@1 45 form = EventEntryForm()
gremmie@1 46
gremmie@1 47 return render_to_response('gcalendar/event.html', {
gremmie@1 48 'title': 'Add Calendar Event',
gremmie@1 49 'form': form,
gremmie@1 50 },
gremmie@1 51 context_instance = RequestContext(request))
gremmie@1 52
gremmie@1 53
gremmie@1 54 @login_required
gremmie@1 55 def add_thanks(request):
gremmie@1 56 return render_to_response('gcalendar/thanks_add.html', {
gremmie@1 57 },
gremmie@1 58 context_instance = RequestContext(request))
gremmie@1 59
gremmie@1 60
gremmie@1 61 @login_required
gremmie@1 62 def edit_events(request):
bgneal@817 63 events = Event.objects.filter(user=request.user, status=Event.ON_CAL).\
bgneal@817 64 order_by('-start_date')
bgneal@817 65 paginator = Paginator(events, 25)
bgneal@817 66 num = request.GET.get('page')
bgneal@817 67 try:
bgneal@817 68 page = paginator.page(num)
bgneal@817 69 except PageNotAnInteger:
bgneal@817 70 page = paginator.page(1)
bgneal@817 71 except EmptyPage:
bgneal@817 72 page = paginator.page(paginator.num_pages)
bgneal@817 73
gremmie@1 74 return render_to_response('gcalendar/edit.html', {
bgneal@817 75 'page': page,
gremmie@1 76 },
gremmie@1 77 context_instance = RequestContext(request))
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:
gremmie@1 84 raise Http404
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
gremmie@1 98 return render_to_response('gcalendar/event.html', {
gremmie@1 99 'title': 'Change Calendar Event',
gremmie@1 100 'form': form,
gremmie@1 101 },
gremmie@1 102 context_instance = RequestContext(request))
gremmie@1 103
gremmie@1 104
gremmie@1 105 @login_required
gremmie@1 106 def edit_thanks(request):
gremmie@1 107 return render_to_response('gcalendar/thanks_edit.html', {
gremmie@1 108 },
gremmie@1 109 context_instance = RequestContext(request))
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():
gremmie@1 115 id = request.POST.get('id', None)
gremmie@1 116 if id is None or not id.isdigit():
gremmie@1 117 return HttpResponseBadRequest()
gremmie@1 118 try:
gremmie@1 119 event = Event.objects.get(pk=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()
gremmie@1 127 return HttpResponse(id)
gremmie@1 128
gremmie@1 129 return HttpResponseForbidden()
gremmie@1 130
gremmie@1 131
gremmie@1 132 # vim: ts=4 sw=4