comparison gpp/gcalendar/views.py @ 1:dbd703f7d63a

Initial import of sg101 stuff from private repository.
author gremmie
date Mon, 06 Apr 2009 02:43:12 +0000
parents
children f7fbb404241f
comparison
equal deleted inserted replaced
0:900ba3c7b765 1:dbd703f7d63a
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 return render_to_response('gcalendar/index.html', {
22 },
23 context_instance = RequestContext(request))
24
25
26 @login_required
27 def add_event(request):
28 if request.method == 'POST':
29 form = EventEntryForm(request.POST)
30 if form.is_valid():
31 event = form.save(commit=False)
32 event.user = request.user
33 event.repeat = 'none'
34 event.save()
35 return HttpResponseRedirect(reverse('gcalendar-add_thanks'))
36 else:
37 form = EventEntryForm()
38
39 return render_to_response('gcalendar/event.html', {
40 'title': 'Add Calendar Event',
41 'form': form,
42 },
43 context_instance = RequestContext(request))
44
45
46 @login_required
47 def add_thanks(request):
48 return render_to_response('gcalendar/thanks_add.html', {
49 },
50 context_instance = RequestContext(request))
51
52
53 @login_required
54 def edit_events(request):
55 events = Event.objects.filter(user=request.user, status=Event.ON_CAL).order_by('start_date')
56 return render_to_response('gcalendar/edit.html', {
57 'events': events,
58 },
59 context_instance = RequestContext(request))
60
61
62 @login_required
63 def edit_event(request, event_id):
64 event = get_object_or_404(Event, pk=event_id)
65 if event.user != request.user:
66 raise Http404
67
68 if request.method == 'POST':
69 form = EventEntryForm(request.POST, instance=event)
70 if form.is_valid():
71 event = form.save(commit=False)
72 event.user = request.user
73 event.repeat = 'none'
74 event.status = Event.EDIT_REQ
75 event.save()
76 return HttpResponseRedirect(reverse('gcalendar-edit_thanks'))
77 else:
78 form = EventEntryForm(instance=event)
79
80 return render_to_response('gcalendar/event.html', {
81 'title': 'Change Calendar Event',
82 'form': form,
83 },
84 context_instance = RequestContext(request))
85
86
87 @login_required
88 def edit_thanks(request):
89 return render_to_response('gcalendar/thanks_edit.html', {
90 },
91 context_instance = RequestContext(request))
92
93
94 def delete_event(request):
95 """This view marks an event for deletion. It is called via AJAX."""
96 if request.user.is_authenticated():
97 id = request.POST.get('id', None)
98 if id is None or not id.isdigit():
99 return HttpResponseBadRequest()
100 try:
101 event = Event.objects.get(pk=id)
102 except Event.DoesNotExist:
103 return HttpResponseBadRequest()
104 if request.user != event.user:
105 return HttpResponseForbidden()
106
107 event.status = Event.DEL_REQ
108 event.save()
109 return HttpResponse(id)
110
111 return HttpResponseForbidden()
112
113
114 # vim: ts=4 sw=4