Newer
Older
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.template.loader import select_template
from django.utils.encoding import force_text
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _
from cms.models import CMSPlugin
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
###################################################################################################
###################################################################################################
@python_2_unicode_compatible
class HighlightedContentPluginBase(CMSPlugin):
title = models.CharField(
verbose_name=_('Title'),
max_length=255,
blank=True, null=True,
help_text=_('Title to display before the list'))
page_link = PageField(
verbose_name=_('Link for the title if set'),
null=True, blank=True,
help_text=_('Page displaying all elements'))
def __str__(self):
if self.title:
return '%s "%s"' % (self._meta.verbose_name, self.title)
return force_text(self._meta.verbose_name)
class Meta:
abstract = True
verbose_name = _('highlighted_content')
verbose_name_plural = _('highlighted_contents')
def get_items_count(self):
if not hasattr(self, '_items_count'):
items = self.get_items()
if hasattr(items, 'count') and callable(items.count):
self._items_count = items.count()
else:
self._items_count = len(items)
return self._items_count
def get_items(self):
if hasattr(self, 'items'):
return self.items.all()
else:
raise Exception(('Must be overwrited if you do not use "items" as the related '
'name of your highlighted_content ForeignKey'))
def copy_relations(self, oldinstance):
"""
copies highlighted_content items when publishing the draft plugin
see http://docs.django-cms.org/en/develop/how_to/custom_plugins.html#handling-relations
"""
self.get_items().delete()
for item in oldinstance.get_items():
item.pk = None
item.highlighted_content = self
item.save()
###################################################################################################
###################################################################################################
@python_2_unicode_compatible
class HighlightedContentItemBase(models.Model):
order = models.PositiveSmallIntegerField(
verbose_name=_('order'),
blank=False, null=False, default=0,
editable=True, db_index=True,
)
content_type = models.ForeignKey(
ContentType,
on_delete=models.CASCADE,
related_name='+',
verbose_name=_('Item\'s type'),
)
object_id = models.PositiveIntegerField(verbose_name=_('Item\'s ID'))
item = GenericForeignKey()
# highlighted_content = models.ForeignKey(YourHighlightedContentPluginModel,
# related_name='items')
_templates = {}
class Meta:
abstract = True
verbose_name = _('item')
verbose_name_plural = _('items')
ordering = ['order']
def __str__(self):
return force_text(self.item)
def save(self, *args, **kwargs):
if not self.pk and not self.order:
manager = self.__class__.objects
self.order = manager.filter(highlighted_content=self.highlighted_content).count() + 1
super(HighlightedContentItemBase, self).save(*args, **kwargs)
def get_template(self):
"""returns the template to use to render this item as a item"""
if self.content_type_id not in self._templates:
kwargs = {'app_label': self._meta.app_label,
'model_name': self._meta.model_name}
templates = ['%(app_label)s/%(model_name)s/highlighted_content_item.html' % kwargs,
'%(app_label)s/highlighted_content_item.html' % kwargs,
'cms_highlighted_content/highlighted_content_item.html']
self._templates[self.content_type_id] = select_template(templates).template.name
return self._templates[self.content_type_id]