annotate gcalendar/views.py @ 661:15dbe0ccda95

Prevent exceptions when viewing downloads in the admin when the file doesn't exist on the filesystem. This is usually seen in development but can also happen in production if the file is missing.
author Brian Neal <bgneal@gmail.com>
date Tue, 14 May 2013 21:02:47 -0500
parents ee87ea74d46b
children 9e803323a0d0
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@345 23 profile = user.get_profile()
bgneal@345 24 tz = 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):
gremmie@1 63 events = Event.objects.filter(user=request.user, status=Event.ON_CAL).order_by('start_date')
gremmie@1 64 return render_to_response('gcalendar/edit.html', {
gremmie@1 65 'events': events,
gremmie@1 66 },
gremmie@1 67 context_instance = RequestContext(request))
gremmie@1 68
gremmie@1 69
gremmie@1 70 @login_required
gremmie@1 71 def edit_event(request, event_id):
gremmie@1 72 event = get_object_or_404(Event, pk=event_id)
gremmie@1 73 if event.user != request.user:
gremmie@1 74 raise Http404
gremmie@1 75
gremmie@1 76 if request.method == 'POST':
gremmie@1 77 form = EventEntryForm(request.POST, instance=event)
gremmie@1 78 if form.is_valid():
gremmie@1 79 event = form.save(commit=False)
gremmie@1 80 event.user = request.user
gremmie@1 81 event.repeat = 'none'
gremmie@1 82 event.status = Event.EDIT_REQ
gremmie@1 83 event.save()
gremmie@1 84 return HttpResponseRedirect(reverse('gcalendar-edit_thanks'))
gremmie@1 85 else:
gremmie@1 86 form = EventEntryForm(instance=event)
gremmie@1 87
gremmie@1 88 return render_to_response('gcalendar/event.html', {
gremmie@1 89 'title': 'Change Calendar Event',
gremmie@1 90 'form': form,
gremmie@1 91 },
gremmie@1 92 context_instance = RequestContext(request))
gremmie@1 93
gremmie@1 94
gremmie@1 95 @login_required
gremmie@1 96 def edit_thanks(request):
gremmie@1 97 return render_to_response('gcalendar/thanks_edit.html', {
gremmie@1 98 },
gremmie@1 99 context_instance = RequestContext(request))
gremmie@1 100
gremmie@1 101
gremmie@1 102 def delete_event(request):
gremmie@1 103 """This view marks an event for deletion. It is called via AJAX."""
gremmie@1 104 if request.user.is_authenticated():
gremmie@1 105 id = request.POST.get('id', None)
gremmie@1 106 if id is None or not id.isdigit():
gremmie@1 107 return HttpResponseBadRequest()
gremmie@1 108 try:
gremmie@1 109 event = Event.objects.get(pk=id)
gremmie@1 110 except Event.DoesNotExist:
gremmie@1 111 return HttpResponseBadRequest()
gremmie@1 112 if request.user != event.user:
gremmie@1 113 return HttpResponseForbidden()
gremmie@1 114
gremmie@1 115 event.status = Event.DEL_REQ
gremmie@1 116 event.save()
gremmie@1 117 return HttpResponse(id)
gremmie@1 118
gremmie@1 119 return HttpResponseForbidden()
gremmie@1 120
gremmie@1 121
gremmie@1 122 # vim: ts=4 sw=4