Test coverage report: hero_slider.models

# hero_slider.models: 31 total statements, 95.8% covered

Generated: Tue 2013-05-21 16:43 CEST

Source file: /home/dkaufhold/projects/django-hero-slider/src/hero_slider/models.py

Stats: 23 executed, 1 missed, 7 excluded, 65 ignored

1. `"""Models for the ``hero_slider`` app."""`
2. `from django.contrib.contenttypes import generic`
3. `from django.contrib.contenttypes.models import ContentType`
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 simple_translation.utils import get_preferred_translation_from_lang`
9. ``
10. ``
11. `from filer.fields.file import FilerFileField`
12. ``
13. ``
14. `class SliderItemManager(models.Manager):`
15. `"""Custom manager for the ``SliderItem`` model."""`
16. `def published(self, request):`
17. `"""`
18. `Returns the published slider items in the current language.`
19. ``
20. `:param request: A Request instance.`
21. ``
22. `"""`
23. `language = getattr(request, 'LANGUAGE_CODE', None)`
24. `if not language:`
25. `return self.model.objects.none()`
26. `qs = self.get_query_set()`
27. `qs = qs.filter(`
28. `slideritemtitle__is_published=True,`
29. `slideritemtitle__language=language,`
30. `)`
31. `return qs`
32. ``
33. ``
34. `class SliderItem(models.Model):`
35. `"""`
36. `Resembles an item that should be shown on the front page in a slider.`
37. ``
38. `For translateable fields see the ``SliderItemTitle`` model.`
39. ``
40. `:image: A filer file. This will be the image shown in the slider.`
41. `:position: Can be set in order to control the ordering of the slider items.`
42. `:content_type: The contenttype of the object this slider item links to.`
43. `:object_id: The PK of the object this slider item links to.`
44. ``
45. `"""`
46. `image = FilerFileField(`
47. `verbose_name=_('Image'),`
48. `)`
49. ``
50. `position = models.PositiveIntegerField(`
51. `verbose_name=_('Position'),`
52. `null=True, blank=True,`
53. `)`
54. ``
55. `# Generic foreign key`
56. `content_type = models.ForeignKey(ContentType)`
57. `object_id = models.PositiveIntegerField()`
58. `content_object = generic.GenericForeignKey('content_type', 'object_id')`
59. ``
60. `objects = SliderItemManager()`
61. ``
62. `def get_trans(self):`
63. `"""Returns the translation object for this slider item."""`
64. `lang = get_language()`
65. `return get_preferred_translation_from_lang(self, lang)`
66. ``
67. ``
68. `class SliderItemTitle(models.Model):`
69. `"""`
70. `Translateable fields of the ``SliderItem`` model.`
71. ``
72. `:title: The title of this slider item.`
73. `:description: A short description of this slider item.`
74. `:is_published: If True, this will not show up.`
75. ``
76. `"""`
77. `title = models.CharField(`
78. `max_length=256,`
79. `verbose_name=_('Title'),`
80. `blank=True,`
81. `)`
82. ``
83. `description = models.CharField(`
84. `max_length=512,`
85. `verbose_name=_('Description'),`
86. `blank=True,`
87. `)`
88. ``
89. `is_published = models.BooleanField(`
90. `verbose_name=_('Is published'),`
91. `default=False,`
92. `)`
93. ``
94. `# Needed by simple-translation`
95. `slider_item = models.ForeignKey(SliderItem, verbose_name=_('Slider item'))`
96. `language = models.CharField(max_length=2, verbose_name=_('Language'))`