Test coverage report: multilingual_events.models

# multilingual_events.models: 108 total statements, 64.8% covered

Generated: Thu 2013-02-28 16:32 SGT

Source file: /Users/martin/Repos/django-multilingual-events/multilingual_events/models.py

Stats: 57 executed, 31 missed, 20 excluded, 224 ignored

1. `"""Models for the ``multilingual_events`` app."""`
2. `from django.conf import settings`
3. `from django.core.urlresolvers import reverse`
4. `from django.db import models`
5. `from django.utils.translation import get_language`
6. `from django.utils.translation import ugettext_lazy as _`
7. ``
8. `from cms.models import CMSPlugin`
9. `from django_countries import CountryField`
10. `from djangocms_utils.fields import M2MPlaceholderField`
11. `from simple_translation.actions import SimpleTranslationPlaceholderActions`
12. `from simple_translation.utils import get_preferred_translation_from_lang`
13. ``
14. ``
15. `class EventCategory(models.Model):`
16. `"""`
17. `Events are grouped in categories.`
18. ``
19. `For translateable fields see ``EventCategoryTitle``.`
20. ``
21. `:position: Use this if you want to change the ordering of categories.`
22. ``
23. `"""`
24. `position = models.PositiveIntegerField(`
25. `verbose_name=_('Position'),`
26. `null=True, blank=True,`
27. `)`
28. ``
29. `slug = models.CharField(`
30. `max_length=32,`
31. `verbose_name=_('Slug'),`
32. `)`
33. ``
34. `def __unicode__(self):`
35. `return self.get_translation().title`
36. ``
37. `def get_translation(self):`
38. `lang = get_language()`
39. `return get_preferred_translation_from_lang(self, lang)`
40. ``
41. ``
42. `class EventCategoryTitle(models.Model):`
43. `"""`
44. `Translateable fields of the ``EventCategory`` model.`
45. ``
46. `"""`
47. `title = models.CharField(`
48. `max_length=256,`
49. `verbose_name=_('Title'),`
50. `)`
51. ``
52. `# Needed by simple-translation`
53. `category = models.ForeignKey(EventCategory, verbose_name=_('Category'))`
54. `language = models.CharField(max_length=2, verbose_name=_('Language'))`
55. ``
56. ``
57. `class Event(models.Model):`
58. `"""`
59. `An event is something that happens on a specific start date.`
60. ``
61. `For translateable fields see ``EventTitle``.`
62. ``
63. `:start_date: The DateTime when this event starts.`
64. `:is_published: If ``True``, the event would be returned by the`
65. ```get_events`` templatetag.`
66. `:creation_date: The DateTime when this event was created.`
67. `:user: The user who created this event.`
68. ``
69. `"""`
70. ``
71. `placeholders = M2MPlaceholderField(`
72. `actions=SimpleTranslationPlaceholderActions(),`
73. `placeholders=('content', ),`
74. `)`
75. ``
76. `# Allow null for once but remove this later, when data is setup.`
77. `category = models.ForeignKey(`
78. `EventCategory,`
79. `verbose_name=_('Category'),`
80. `)`
81. ``
82. `start_date = models.DateField(`
83. `verbose_name=_('Start date'),`
84. `)`
85. ``
86. `start_time = models.TimeField(`
87. `verbose_name=_('Start time'),`
88. `null=True, blank=True,`
89. `)`
90. ``
91. `end_date = models.DateField(`
92. `verbose_name=_('End date'),`
93. `null=True, blank=True`
94. `)`
95. ``
96. `end_time = models.TimeField(`
97. `verbose_name=_('End time'),`
98. `null=True, blank=True,`
99. `)`
100. ``
101. `timezone = models.CharField(`
102. `max_length=65,`
103. `verbose_name=_('Timezone'),`
104. `blank=True,`
105. `)`
106. ``
107. `url = models.URLField(`
108. `verbose_name=_('External URL'),`
109. `blank=True,`
110. `)`
111. ``
112. `url_name = models.CharField(`
113. `max_length=256,`
114. `verbose_name=_('External URL name'),`
115. `blank=True,`
116. `)`
117. ``
118. `venue_url = models.URLField(`
119. `verbose_name=_('Venue URL'),`
120. `blank=True,`
121. `)`
122. ``
123. `google_maps_embed_code = models.CharField(`
124. `max_length=4000,`
125. `verbose_name=_('Google Maps embed code'),`
126. `blank=True,`
127. `)`
128. ``
129. `is_published = models.BooleanField(`
130. `default=False,`
131. `verbose_name=_('Is published'),`
132. `)`
133. ``
134. `creation_date = models.DateTimeField(`
135. `auto_now_add=True,`
136. `verbose_name=('Creation date'),`
137. `)`
138. ``
139. `last_update = models.DateTimeField(`
140. `auto_now=True,`
141. `)`
142. ``
143. `user = models.ForeignKey(`
144. `'auth.User',`
145. `verbose_name=_('User'),`
146. `null=True, blank=True,`
147. `)`
148. ``
149. `country = CountryField(`
150. `verbose_name=_('Country'),`
151. `null=True, blank=True,`
152. `)`
153. ``
154. `class Meta:`
155. `ordering = ('start_date', )`
156. ``
157. `def __unicode__(self):`
158. `return self.get_title()`
159. ``
160. `def get_address(self):`
161. `"""Returns the address with country."""`
162. `trans = self.get_translation()`
163. `full_address = u''`
164. `if trans.venue_name:`
165. `full_address += u'{0}<br />'.format(trans.venue_name)`
166. `if trans.address_1:`
167. `full_address += u'{0}<br />'.format(trans.address_1)`
168. `if trans.address_2:`
169. `full_address += u'{0}<br />'.format(trans.address_2)`
170. `if trans.city:`
171. `if trans.postal_code:`
172. `full_address += u'{0} '.format(trans.postal_code)`
173. `full_address += u'{0}<br />'.format(trans.city)`
174. `full_address += u'{0}'.format(self.country.name.encode())`
175. `return full_address`
176. ``
177. `def get_alternative_events(self):`
178. `return Event.objects.filter(category=self.category).exclude(`
179. `pk=self.pk).order_by('start_date')`
180. ``
181. `def get_city_and_country(self):`
182. `trans = self.get_translation()`
183. `result = u''`
184. `if trans.city:`
185. `result += u'{0}, '.format(trans.city)`
186. `result += self.country.name.encode()`
187. `return result`
188. ``
189. `def get_number_of_days(self):`
190. `"""Returns the number of days for this event."""`
191. `if not self.end_date:`
192. `return 1`
193. `amount = self.end_date - self.start_date`
194. `return amount.days + 1`
195. ``
196. `def get_title(self):`
197. `lang = get_language()`
198. `return get_preferred_translation_from_lang(self, lang).title`
199. ``
200. `def get_translation(self):`
201. `lang = get_language()`
202. `return get_preferred_translation_from_lang(self, lang)`
203. ``
204. ``
205. `class EventTitle(models.Model):`
206. `"""`
207. `Translateable fields of the ``Event`` model.`
208. ``
209. `"""`
210. `title = models.CharField(`
211. `max_length=512,`
212. `verbose_name=_('Title'),`
213. `)`
214. ``
215. `venue_name = models.CharField(`
216. `max_length=256,`
217. `verbose_name=_('Venue name'),`
218. `blank=True,`
219. `)`
220. ``
221. `city = models.CharField(`
222. `max_length=256,`
223. `verbose_name=_('City'),`
224. `blank=True,`
225. `)`
226. ``
227. `postal_code = models.CharField(`
228. `max_length=256,`
229. `verbose_name=_('Postal code'),`
230. `blank=True,`
231. `)`
232. ``
233. `address_1 = models.CharField(`
234. `max_length=256,`
235. `verbose_name=_('Address 1'),`
236. `blank=True,`
237. `)`
238. ``
239. `address_2 = models.CharField(`
240. `max_length=256,`
241. `verbose_name=_('Address 2'),`
242. `blank=True,`
243. `)`
244. ``
245. `room = models.CharField(`
246. `max_length=256,`
247. `verbose_name=_('Block / Room'),`
248. `blank=True,`
249. `)`
250. ``
251. `description = models.TextField(`
252. `verbose_name=_('Description'),`
253. `blank=True,`
254. `)`
255. ``
256. `# Needed by simple_translation`
257. `event = models.ForeignKey(Event, verbose_name=_('Event'))`
258. `language = models.CharField(max_length=5, verbose_name=('Language'))`
259. ``
260. `def get_absolute_url(self):`
261. `middleware = (`
262. `'simple_translation.middleware.MultilingualGenericsMiddleware')`
263. `language_namespace = middleware in settings.MIDDLEWARE_CLASSES \`
264. `and '%s:' % self.language or ''`
265. `return reverse(`
266. `'%ssimple_events_detail' % language_namespace,`
267. `args=(),`
268. `kwargs={'pk': self.event.pk, }`
269. `)`
270. ``
271. ``
272. `class EventAgendaDay(CMSPlugin):`
273. `"""The day and title of an event."""`
274. `date = models.DateField(`
275. `verbose_name=_('Date'),`
276. `)`
277. ``
278. `title = models.CharField(`
279. `max_length=256,`
280. `verbose_name=_('Title'),`
281. `)`
282. ``
283. `def __unicode__(self):`
284. `return self.title`
285. ``
286. ``
287. `class EventAgendaSession(CMSPlugin):`
288. `"""A day can consist of several sessions."""`
289. `start_time = models.DateTimeField(`
290. `verbose_name=_('Start time'),`
291. `)`
292. ``
293. `end_time = models.DateTimeField(`
294. `verbose_name=_('End time'),`
295. `)`
296. ``
297. `title = models.CharField(`
298. `max_length=256,`
299. `verbose_name=_('Title'),`
300. `)`
301. ``
302. `description = models.TextField(`
303. `max_length=4000,`
304. `verbose_name=_('Description'),`
305. `blank=True,`
306. `)`
307. ``
308. `document = models.ForeignKey(`
309. `'document_library.Document',`
310. `verbose_name=_('Document'),`
311. `null=True, blank=True,`
312. `)`
313. ``
314. ``
315. `class EventAgendaTalk(CMSPlugin):`
316. `"""A session can have several talks."""`
317. `title = models.CharField(`
318. `max_length=256,`
319. `verbose_name=_('Title'),`
320. `)`
321. ``
322. `description = models.TextField(`
323. `max_length=4000,`
324. `verbose_name=_('Description'),`
325. `blank=True,`
326. `)`
327. ``
328. `document = models.ForeignKey(`
329. `'document_library.Document',`
330. `verbose_name=_('Document'),`
331. `null=True, blank=True,`
332. `)`