comparison gcalendar/views.py @ 581:ee87ea74d46b

For Django 1.4, rearranged project structure for new manage.py.
author Brian Neal <bgneal@gmail.com>
date Sat, 05 May 2012 17:10:48 -0500
parents gpp/gcalendar/views.py@f7fbb404241f
children 9e803323a0d0
comparison
equal deleted inserted replaced
580:c525f3e0b5d0 581:ee87ea74d46b
1 """
2 Views for the gcalendar application.
3 """
4
5 from django.contrib.auth.decorators import login_required
6 from django.core.urlresolvers import reverse
7 from django.http import HttpResponse
8 from django.http import HttpResponseBadRequest
9 from django.http import HttpResponseForbidden
10 from django.http import HttpResponseRedirect
11 from django.http import Http404
12 from django.shortcuts import render_to_response
13 from django.shortcuts import get_object_or_404
14 from django.template import RequestContext
15
16 from gcalendar.forms import EventEntryForm
17 from gcalendar.models import Event
18
19
20 def index(request):
21 user = request.user
22 if user.is_authenticated():
23 profile = user.get_profile()
24 tz = profile.time_zone
25 else:
26 tz = 'US/Pacific'
27
28 return render_to_response('gcalendar/index.html', {
29 'tz': tz,
30 },
31 context_instance = RequestContext(request))
32
33
34 @login_required
35 def add_event(request):
36 if request.method == 'POST':
37 form = EventEntryForm(request.POST)
38 if form.is_valid():
39 event = form.save(commit=False)
40 event.user = request.user
41 event.repeat = 'none'
42 event.save()
43 return HttpResponseRedirect(reverse('gcalendar-add_thanks'))
44 else:
45 form = EventEntryForm()
46
47 return render_to_response('gcalendar/event.html', {
48 'title': 'Add Calendar Event',
49 'form': form,
50 },
51 context_instance = RequestContext(request))
52
53
54 @login_required
55 def add_thanks(request):
56 return render_to_response('gcalendar/thanks_add.html', {
57 },
58 context_instance = RequestContext(request))
59
60
61 @login_required
62 def edit_events(request):
63 events = Event.objects.filter(user=request.user, status=Event.ON_CAL).order_by('start_date')
64 return render_to_response('gcalendar/edit.html', {
65 'events': events,
66 },
67 context_instance = RequestContext(request))
68
69
70 @login_required
71 def edit_event(request, event_id):
72 event = get_object_or_404(Event, pk=event_id)
73 if event.user != request.user:
74 raise Http404
75
76 if request.method == 'POST':
77 form = EventEntryForm(request.POST, instance=event)
78 if form.is_valid():
79 event = form.save(commit=False)
80 event.user = request.user
81 event.repeat = 'none'
82 event.status = Event.EDIT_REQ
83 event.save()
84 return HttpResponseRedirect(reverse('gcalendar-edit_thanks'))
85 else:
86 form = EventEntryForm(instance=event)
87
88 return render_to_response('gcalendar/event.html', {
89 'title': 'Change Calendar Event',
90 'form': form,
91 },
92 context_instance = RequestContext(request))
93
94
95 @login_required
96 def edit_thanks(request):
97 return render_to_response('gcalendar/thanks_edit.html', {
98 },
99 context_instance = RequestContext(request))
100
101
102 def delete_event(request):
103 """This view marks an event for deletion. It is called via AJAX."""
104 if request.user.is_authenticated():
105 id = request.POST.get('id', None)
106 if id is None or not id.isdigit():
107 return HttpResponseBadRequest()
108 try:
109 event = Event.objects.get(pk=id)
110 except Event.DoesNotExist:
111 return HttpResponseBadRequest()
112 if request.user != event.user:
113 return HttpResponseForbidden()
114
115 event.status = Event.DEL_REQ
116 event.save()
117 return HttpResponse(id)
118
119 return HttpResponseForbidden()
120
121
122 # vim: ts=4 sw=4