annotate gcalendar/views.py @ 793:fa7377d90160

Issue #71: in production, celeryd now needs tools on the PYTHONPATH. This is because it is now using site markup, which requires our Markdown extensions.
author Brian Neal <bgneal@gmail.com>
date Mon, 26 May 2014 19:34:49 -0500
parents 9e803323a0d0
children 74e84f5fc948
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
gremmie@1 7 from django.http import HttpResponse
gremmie@1 8 from django.http import HttpResponseBadRequest
gremmie@1 9 from django.http import HttpResponseForbidden
gremmie@1 10 from django.http import HttpResponseRedirect
gremmie@1 11 from django.http import Http404
gremmie@1 12 from django.shortcuts import render_to_response
gremmie@1 13 from django.shortcuts import get_object_or_404
gremmie@1 14 from django.template import RequestContext
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
gremmie@1 27 return render_to_response('gcalendar/index.html', {
bgneal@345 28 'tz': tz,
gremmie@1 29 },
gremmie@1 30 context_instance = RequestContext(request))
gremmie@1 31
gremmie@1 32
gremmie@1 33 @login_required
gremmie@1 34 def add_event(request):
gremmie@1 35 if request.method == 'POST':
gremmie@1 36 form = EventEntryForm(request.POST)
gremmie@1 37 if form.is_valid():
gremmie@1 38 event = form.save(commit=False)
gremmie@1 39 event.user = request.user
gremmie@1 40 event.repeat = 'none'
gremmie@1 41 event.save()
gremmie@1 42 return HttpResponseRedirect(reverse('gcalendar-add_thanks'))
gremmie@1 43 else:
gremmie@1 44 form = EventEntryForm()
gremmie@1 45
gremmie@1 46 return render_to_response('gcalendar/event.html', {
gremmie@1 47 'title': 'Add Calendar Event',
gremmie@1 48 'form': form,
gremmie@1 49 },
gremmie@1 50 context_instance = RequestContext(request))
gremmie@1 51
gremmie@1 52
gremmie@1 53 @login_required
gremmie@1 54 def add_thanks(request):
gremmie@1 55 return render_to_response('gcalendar/thanks_add.html', {
gremmie@1 56 },
gremmie@1 57 context_instance = RequestContext(request))
gremmie@1 58
gremmie@1 59
gremmie@1 60 @login_required
gremmie@1 61 def edit_events(request):
gremmie@1 62 events = Event.objects.filter(user=request.user, status=Event.ON_CAL).order_by('start_date')
gremmie@1 63 return render_to_response('gcalendar/edit.html', {
gremmie@1 64 'events': events,
gremmie@1 65 },
gremmie@1 66 context_instance = RequestContext(request))
gremmie@1 67
gremmie@1 68
gremmie@1 69 @login_required
gremmie@1 70 def edit_event(request, event_id):
gremmie@1 71 event = get_object_or_404(Event, pk=event_id)
gremmie@1 72 if event.user != request.user:
gremmie@1 73 raise Http404
gremmie@1 74
gremmie@1 75 if request.method == 'POST':
gremmie@1 76 form = EventEntryForm(request.POST, instance=event)
gremmie@1 77 if form.is_valid():
gremmie@1 78 event = form.save(commit=False)
gremmie@1 79 event.user = request.user
gremmie@1 80 event.repeat = 'none'
gremmie@1 81 event.status = Event.EDIT_REQ
gremmie@1 82 event.save()
gremmie@1 83 return HttpResponseRedirect(reverse('gcalendar-edit_thanks'))
gremmie@1 84 else:
gremmie@1 85 form = EventEntryForm(instance=event)
gremmie@1 86
gremmie@1 87 return render_to_response('gcalendar/event.html', {
gremmie@1 88 'title': 'Change Calendar Event',
gremmie@1 89 'form': form,
gremmie@1 90 },
gremmie@1 91 context_instance = RequestContext(request))
gremmie@1 92
gremmie@1 93
gremmie@1 94 @login_required
gremmie@1 95 def edit_thanks(request):
gremmie@1 96 return render_to_response('gcalendar/thanks_edit.html', {
gremmie@1 97 },
gremmie@1 98 context_instance = RequestContext(request))
gremmie@1 99
gremmie@1 100
gremmie@1 101 def delete_event(request):
gremmie@1 102 """This view marks an event for deletion. It is called via AJAX."""
gremmie@1 103 if request.user.is_authenticated():
gremmie@1 104 id = request.POST.get('id', None)
gremmie@1 105 if id is None or not id.isdigit():
gremmie@1 106 return HttpResponseBadRequest()
gremmie@1 107 try:
gremmie@1 108 event = Event.objects.get(pk=id)
gremmie@1 109 except Event.DoesNotExist:
gremmie@1 110 return HttpResponseBadRequest()
gremmie@1 111 if request.user != event.user:
gremmie@1 112 return HttpResponseForbidden()
gremmie@1 113
gremmie@1 114 event.status = Event.DEL_REQ
gremmie@1 115 event.save()
gremmie@1 116 return HttpResponse(id)
gremmie@1 117
gremmie@1 118 return HttpResponseForbidden()
gremmie@1 119
gremmie@1 120
gremmie@1 121 # vim: ts=4 sw=4