Newer
Older
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.template.loader import select_template
from django.utils.encoding import smart_bytes
from django.utils.translation import ugettext_lazy as _
from cms.plugin_base import CMSPluginBase
from cms_highlighted_content.admin import HighlightedContentItemInlineBase
from cms_highlighted_content.models import HighlightedContentItemBase
12
13
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
class HighlightedContentPluginBase(CMSPluginBase):
cache = True
name = _('HighlightedContent')
raw_id_fields = ('page_link',)
inlines = []
def __init__(self, *args, **kwargs):
if not hasattr(self.__class__, 'highlighted_content_item_inline_class'):
highlighted_content_item_model = None
for m2m in self.model._meta.related_objects:
if issubclass(m2m.related_model, HighlightedContentItemBase):
highlighted_content_item_model = m2m.related_model
break
if not highlighted_content_item_model:
raise Exception('Can not get your HighlightedContentItem model. '
'Do you add a foreignkey to '
'your HighlightedContent model with a valid related_name ?')
class_name = smart_bytes(highlighted_content_item_model.__name__ + 'Inline')
bases = (HighlightedContentItemInlineBase, )
attributes = {'model': highlighted_content_item_model}
inline_class = type(class_name, bases, attributes)
setattr(self.__class__, 'highlighted_content_item_inline_class', inline_class)
if self.__class__.highlighted_content_item_inline_class not in self.inlines:
self.inlines.append(self.__class__.highlighted_content_item_inline_class)
super(HighlightedContentPluginBase, self).__init__(*args, **kwargs)
def get_render_template(self, context, instance, placeholder):
if not hasattr(self, '_render_template'):
kwargs = {'app_label': self.model._meta.app_label,
'model_name': self.model._meta.model_name}
templates = ['%(app_label)s/%(model_name)s/highlighted_content.html' % kwargs,
'%(app_label)s/highlighted_content.html' % kwargs,
'cms_highlighted_content/highlighted_content.html' % kwargs]
self._render_template = select_template(templates).template.name
return self._render_template
def get_items(self, context, instance, placeholder):
return instance.get_items()
def render(self, context, instance, placeholder):
items = self.get_items(context, instance, placeholder)
context.update({
'object_list': items,
'count': (items.count() if hasattr(items, 'count') and callable(items.count)
else len(items)),
})
return super(HighlightedContentPluginBase, self).render(context, instance, placeholder)