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
|
bgneal@1094
|
6 from django.core.exceptions import PermissionDenied
|
bgneal@1094
|
7 from django.core.paginator import InvalidPage
|
gremmie@1
|
8 from django.core.urlresolvers import reverse
|
gremmie@1
|
9 from django.http import HttpResponse
|
gremmie@1
|
10 from django.http import HttpResponseBadRequest
|
gremmie@1
|
11 from django.http import HttpResponseForbidden
|
gremmie@1
|
12 from django.http import HttpResponseRedirect
|
gremmie@1
|
13 from django.http import Http404
|
gremmie@1
|
14 from django.shortcuts import get_object_or_404
|
bgneal@1031
|
15 from django.shortcuts import render
|
gremmie@1
|
16
|
bgneal@1094
|
17 from core.functions import get_page
|
bgneal@1094
|
18 from core.paginator import DiggPaginator
|
gremmie@1
|
19 from gcalendar.forms import EventEntryForm
|
gremmie@1
|
20 from gcalendar.models import Event
|
gremmie@1
|
21
|
gremmie@1
|
22
|
gremmie@1
|
23 def index(request):
|
bgneal@345
|
24 user = request.user
|
bgneal@345
|
25 if user.is_authenticated():
|
bgneal@789
|
26 tz = user.profile.time_zone
|
bgneal@345
|
27 else:
|
bgneal@345
|
28 tz = 'US/Pacific'
|
bgneal@345
|
29
|
bgneal@1031
|
30 return render(request, 'gcalendar/index.html', {
|
bgneal@345
|
31 'tz': tz,
|
bgneal@1094
|
32 'V3_DESIGN': True,
|
bgneal@1031
|
33 })
|
gremmie@1
|
34
|
gremmie@1
|
35
|
gremmie@1
|
36 @login_required
|
gremmie@1
|
37 def add_event(request):
|
gremmie@1
|
38 if request.method == 'POST':
|
gremmie@1
|
39 form = EventEntryForm(request.POST)
|
gremmie@1
|
40 if form.is_valid():
|
gremmie@1
|
41 event = form.save(commit=False)
|
gremmie@1
|
42 event.user = request.user
|
gremmie@1
|
43 event.repeat = 'none'
|
bgneal@1021
|
44 event.save(html=form.html)
|
gremmie@1
|
45 return HttpResponseRedirect(reverse('gcalendar-add_thanks'))
|
gremmie@1
|
46 else:
|
gremmie@1
|
47 form = EventEntryForm()
|
gremmie@1
|
48
|
bgneal@1031
|
49 return render(request, 'gcalendar/event.html', {
|
gremmie@1
|
50 'title': 'Add Calendar Event',
|
gremmie@1
|
51 'form': form,
|
bgneal@1094
|
52 'V3_DESIGN': True,
|
bgneal@1031
|
53 })
|
gremmie@1
|
54
|
gremmie@1
|
55
|
gremmie@1
|
56 @login_required
|
gremmie@1
|
57 def add_thanks(request):
|
bgneal@1031
|
58 return render(request, 'gcalendar/thanks_add.html', {
|
bgneal@1031
|
59 })
|
gremmie@1
|
60
|
gremmie@1
|
61
|
gremmie@1
|
62 @login_required
|
gremmie@1
|
63 def edit_events(request):
|
bgneal@817
|
64 events = Event.objects.filter(user=request.user, status=Event.ON_CAL).\
|
bgneal@817
|
65 order_by('-start_date')
|
bgneal@1094
|
66 paginator = DiggPaginator(events, 25, body=5, tail=2, margin=3, padding=2)
|
bgneal@1094
|
67 num = get_page(request.GET)
|
bgneal@817
|
68 try:
|
bgneal@817
|
69 page = paginator.page(num)
|
bgneal@1094
|
70 except InvalidPage:
|
bgneal@1094
|
71 raise Http404
|
bgneal@817
|
72
|
bgneal@1031
|
73 return render(request, 'gcalendar/edit.html', {
|
bgneal@817
|
74 'page': page,
|
bgneal@1094
|
75 'V3_DESIGN': True,
|
bgneal@1031
|
76 })
|
gremmie@1
|
77
|
gremmie@1
|
78
|
gremmie@1
|
79 @login_required
|
gremmie@1
|
80 def edit_event(request, event_id):
|
gremmie@1
|
81 event = get_object_or_404(Event, pk=event_id)
|
gremmie@1
|
82 if event.user != request.user:
|
bgneal@1094
|
83 raise PermissionDenied
|
gremmie@1
|
84
|
gremmie@1
|
85 if request.method == 'POST':
|
gremmie@1
|
86 form = EventEntryForm(request.POST, instance=event)
|
gremmie@1
|
87 if form.is_valid():
|
gremmie@1
|
88 event = form.save(commit=False)
|
gremmie@1
|
89 event.user = request.user
|
gremmie@1
|
90 event.repeat = 'none'
|
gremmie@1
|
91 event.status = Event.EDIT_REQ
|
gremmie@1
|
92 event.save()
|
gremmie@1
|
93 return HttpResponseRedirect(reverse('gcalendar-edit_thanks'))
|
gremmie@1
|
94 else:
|
gremmie@1
|
95 form = EventEntryForm(instance=event)
|
gremmie@1
|
96
|
bgneal@1031
|
97 return render(request, 'gcalendar/event.html', {
|
gremmie@1
|
98 'title': 'Change Calendar Event',
|
gremmie@1
|
99 'form': form,
|
bgneal@1031
|
100 })
|
gremmie@1
|
101
|
gremmie@1
|
102
|
gremmie@1
|
103 @login_required
|
gremmie@1
|
104 def edit_thanks(request):
|
bgneal@1031
|
105 return render(request, 'gcalendar/thanks_edit.html', {
|
bgneal@1031
|
106 })
|
gremmie@1
|
107
|
gremmie@1
|
108
|
gremmie@1
|
109 def delete_event(request):
|
gremmie@1
|
110 """This view marks an event for deletion. It is called via AJAX."""
|
gremmie@1
|
111 if request.user.is_authenticated():
|
bgneal@1094
|
112 event_id = request.POST.get('id')
|
bgneal@1094
|
113 if event_id is None or not event_id.isdigit():
|
gremmie@1
|
114 return HttpResponseBadRequest()
|
gremmie@1
|
115 try:
|
bgneal@1094
|
116 event = Event.objects.get(pk=event_id)
|
gremmie@1
|
117 except Event.DoesNotExist:
|
gremmie@1
|
118 return HttpResponseBadRequest()
|
gremmie@1
|
119 if request.user != event.user:
|
gremmie@1
|
120 return HttpResponseForbidden()
|
gremmie@1
|
121
|
gremmie@1
|
122 event.status = Event.DEL_REQ
|
gremmie@1
|
123 event.save()
|
bgneal@1094
|
124 return HttpResponse(event_id, content_type="text/plain")
|
gremmie@1
|
125
|
gremmie@1
|
126 return HttpResponseForbidden()
|