Test coverage report: calendarium.views

# calendarium.views: 168 total statements, 93.6% covered

Generated: Sun 2013-03-24 21:11 CET

Source file: /home/tobi/Projects/calendarium/src/calendarium/views.py

Stats: 146 executed, 10 missed, 12 excluded, 86 ignored

1. `"""Views for the ``calendarium`` app."""`
2. `import calendar`
3. ``
4. `from django.contrib.auth.decorators import permission_required`
5. `from django.core.urlresolvers import reverse`
6. `from django.forms.models import model_to_dict`
7. `from django.http import Http404, HttpResponseRedirect`
8. `from django.utils.decorators import method_decorator`
9. `from django.utils.timezone import datetime, now, timedelta, utc`
10. `from django.views.generic import (`
11. `CreateView,`
12. `DeleteView,`
13. `DetailView,`
14. `RedirectView,`
15. `TemplateView,`
16. `UpdateView,`
17. `)`
18. ``
19. `from calendarium.constants import OCCURRENCE_DECISIONS`
20. `from calendarium.forms import OccurrenceForm`
21. `from calendarium.models import Event, Occurrence`
22. `from calendarium.utils import monday_of_week`
23. ``
24. ``
25. `class CalendariumRedirectView(RedirectView):`
26. `"""View to redirect to the current month view."""`
27. `def get_redirect_url(self, **kwargs):`
28. `return reverse('calendar_month', kwargs={'year': now().year,`
29. `'month': now().month})`
30. ``
31. ``
32. `class MonthView(TemplateView):`
33. `"""View to return all occurrences of an event for a whole month."""`
34. `template_name = 'calendarium/calendar_month.html'`
35. ``
36. `def dispatch(self, request, *args, **kwargs):`
37. `self.month = int(kwargs.get('month'))`
38. `self.year = int(kwargs.get('year'))`
39. `if self.month not in range(1, 13):`
40. `raise Http404`
41. `if request.method == 'POST':`
42. `if request.POST.get('next'):`
43. `new_date = datetime(self.year, self.month, 1) + timedelta(`
44. `days=31)`
45. `return HttpResponseRedirect(reverse('calendar_month', kwargs={`
46. `'year': new_date.year, 'month': new_date.month}))`
47. `elif request.POST.get('previous'):`
48. `new_date = datetime(self.year, self.month, 1) - timedelta(`
49. `days=1)`
50. `return HttpResponseRedirect(reverse('calendar_month', kwargs={`
51. `'year': new_date.year, 'month': new_date.month}))`
52. `elif request.POST.get('today'):`
53. `return HttpResponseRedirect(reverse('calendar_month', kwargs={`
54. `'year': now().year, 'month': now().month}))`
55. `if request.is_ajax():`
56. `self.template_name = 'calendarium/partials/calendar_month.html'`
57. `return super(MonthView, self).dispatch(request, *args, **kwargs)`
58. ``
59. `def get_context_data(self, **kwargs):`
60. `month = [[]]`
61. `week = 0`
62. `for day in calendar.Calendar().itermonthdays(self.year, self.month):`
63. `current = False`
64. `if day:`
65. `date = datetime(year=self.year, month=self.month, day=day,`
66. `tzinfo=utc)`
67. `occurrences = Event.objects.get_occurrences(date, date)`
68. `if date.date() == now().date():`
69. `current = True`
70. `else:`
71. `occurrences = []`
72. `month[week].append((day, occurrences, current))`
73. `if len(month[week]) == 7:`
74. `month.append([])`
75. `week += 1`
76. `ctx = {'month': month, 'date': date}`
77. `return ctx`
78. ``
79. ``
80. `class WeekView(TemplateView):`
81. `"""View to return all occurrences of an event for one week."""`
82. `template_name = 'calendarium/calendar_week.html'`
83. ``
84. `def dispatch(self, request, *args, **kwargs):`
85. `self.week = int(kwargs.get('week'))`
86. `self.year = int(kwargs.get('year'))`
87. `if self.week not in range(1, 53):`
88. `raise Http404`
89. `if request.method == 'POST':`
90. `if request.POST.get('next'):`
91. `date = monday_of_week(self.year, self.week) + timedelta(days=7)`
92. `return HttpResponseRedirect(reverse('calendar_week', kwargs={`
93. `'year': date.year, 'week': date.date().isocalendar()[1]}))`
94. `elif request.POST.get('previous'):`
95. `date = monday_of_week(self.year, self.week) - timedelta(days=7)`
96. `return HttpResponseRedirect(reverse('calendar_week', kwargs={`
97. `'year': date.year, 'week': date.date().isocalendar()[1]}))`
98. `elif request.POST.get('today'):`
99. `return HttpResponseRedirect(reverse('calendar_week', kwargs={`
100. `'year': now().year,`
101. `'week': now().date().isocalendar()[1]}))`
102. `if request.is_ajax():`
103. `self.template_name = 'calendarium/partials/calendar_week.html'`
104. `return super(WeekView, self).dispatch(request, *args, **kwargs)`
105. ``
106. `def get_context_data(self, **kwargs):`
107. `date = monday_of_week(self.year, self.week)`
108. `week = []`
109. `day = 0`
110. `while day < 7:`
111. `current = False`
112. `occurrences = Event.objects.get_occurrences(date, date)`
113. `if date.date() == now().date():`
114. `current = True`
115. `week.append((date, occurrences, current))`
116. `day += 1`
117. `date = date + timedelta(days=1)`
118. `ctx = {'week': week, 'date': date, 'week_nr': self.week}`
119. `return ctx`
120. ``
121. ``
122. `class DayView(TemplateView):`
123. `"""View to return all occurrences of an event for one day."""`
124. `template_name = 'calendarium/calendar_day.html'`
125. ``
126. `def dispatch(self, request, *args, **kwargs):`
127. `self.day = int(kwargs.get('day'))`
128. `self.month = int(kwargs.get('month'))`
129. `self.year = int(kwargs.get('year'))`
130. `try:`
131. `self.date = datetime(year=self.year, month=self.month,`
132. `day=self.day, tzinfo=utc)`
133. `except ValueError:`
134. `raise Http404`
135. `if request.method == 'POST':`
136. `if request.POST.get('next'):`
137. `date = self.date + timedelta(days=1)`
138. `return HttpResponseRedirect(reverse('calendar_day', kwargs={`
139. `'year': date.year, 'month': date.month, 'day': date.day}))`
140. `elif request.POST.get('previous'):`
141. `date = self.date - timedelta(days=1)`
142. `return HttpResponseRedirect(reverse('calendar_day', kwargs={`
143. `'year': date.year, 'month': date.month, 'day': date.day}))`
144. `elif request.POST.get('today'):`
145. `return HttpResponseRedirect(reverse('calendar_day', kwargs={`
146. `'year': now().year, 'month': now().month,`
147. `'day': now().day}))`
148. `if request.is_ajax():`
149. `self.template_name = 'calendarium/partials/calendar_day.html'`
150. `return super(DayView, self).dispatch(request, *args, **kwargs)`
151. ``
152. `def get_context_data(self, **kwargs):`
153. `occurrences = Event.objects.get_occurrences(self.date, self.date)`
154. `ctx = {'date': self.date, 'occurrences': occurrences}`
155. `return ctx`
156. ``
157. ``
158. `class EventDetailView(DetailView):`
159. `"""View to return information of an event."""`
160. `model = Event`
161. ``
162. ``
163. `class EventMixin(object):`
164. `"""Mixin to handle event-related functions."""`
165. `model = Event`
166. ``
167. `@method_decorator(permission_required('calendarium.add_event'))`
168. `def dispatch(self, request, *args, **kwargs):`
169. `return super(EventMixin, self).dispatch(request, *args, **kwargs)`
170. ``
171. ``
172. `class EventUpdateView(EventMixin, UpdateView):`
173. `"""View to update information of an event."""`
174. `pass`
175. ``
176. ``
177. `class EventCreateView(EventMixin, CreateView):`
178. `"""View to create an event."""`
179. `pass`
180. ``
181. ``
182. `class EventDeleteView(EventMixin, DeleteView):`
183. `"""View to delete an event."""`
184. `pass`
185. ``
186. ``
187. `class OccurrenceViewMixin(object):`
188. `"""Mixin to avoid repeating code for the Occurrence view classes."""`
189. `form_class = OccurrenceForm`
190. ``
191. `def dispatch(self, request, *args, **kwargs):`
192. `try:`
193. `self.event = Event.objects.get(pk=kwargs.get('pk'))`
194. `except Event.DoesNotExist:`
195. `raise Http404`
196. `year = int(kwargs.get('year'))`
197. `month = int(kwargs.get('month'))`
198. `day = int(kwargs.get('day'))`
199. `try:`
200. `date = datetime(year, month, day, tzinfo=utc)`
201. `except TypeError:`
202. `raise Http404`
203. `# this should retrieve the one single occurrence, that has a`
204. `# matching start date`
205. `try:`
206. `occ = Occurrence.objects.get(`
207. `start__year=year, start__month=month, start__day=day)`
208. `except Occurrence.DoesNotExist:`
209. `occ_gen = self.event.get_occurrences(self.event.start)`
210. `occ = occ_gen.next()`
211. `while occ.start.date() < date.date():`
212. `occ = occ_gen.next()`
213. `if occ.start.date() == date.date():`
214. `self.occurrence = occ`
215. `else:`
216. `raise Http404`
217. `self.object = self.occurrence`
218. `return super(OccurrenceViewMixin, self).dispatch(`
219. `request, *args, **kwargs)`
220. ``
221. `def get_object(self):`
222. `return self.occurrence`
223. ``
224. `def get_form_kwargs(self):`
225. `kwargs = super(OccurrenceViewMixin, self).get_form_kwargs()`
226. `kwargs.update({'initial': model_to_dict(self.occurrence)})`
227. `return kwargs`
228. ``
229. ``
230. `class OccurrenceDeleteView(OccurrenceViewMixin, DeleteView):`
231. `"""View to delete an occurrence of an event."""`
232. `def delete(self, request, *args, **kwargs):`
233. `self.object = self.get_object()`
234. `decision = self.request.POST.get('decision')`
235. `self.object.delete_period(decision)`
236. `return HttpResponseRedirect(self.get_success_url())`
237. ``
238. `def get_context_data(self, object):`
239. `ctx = super(OccurrenceDeleteView, self).get_context_data()`
240. `ctx.update({`
241. `'decisions': OCCURRENCE_DECISIONS,`
242. `'object': self.object`
243. `})`
244. `return ctx`
245. ``
246. ``
247. `class OccurrenceDetailView(OccurrenceViewMixin, DetailView):`
248. `"""View to show information of an occurrence of an event."""`
249. `pass`
250. ``
251. ``
252. `class OccurrenceUpdateView(OccurrenceViewMixin, UpdateView):`
253. `"""View to edit an occurrence of an event."""`
254. `pass`