Browse Source

Moved StyleMixin code.

master
Erik Stein 7 years ago
parent
commit
33de5a18e7
  1. 33
      content_plugins/base.py
  2. 0
      content_plugins/plugins/__init__.py
  3. 38
      content_plugins/plugins/mixins.py

33
content_plugins/base.py

@ -9,6 +9,7 @@ from shared.utils.fields import AutoSlugField
from .admin import ContentInlineBase, RichTextInlineBase
from .plugins.mixins import StyleMixin
from . import USE_TRANSLATABLE_FIELDS
@ -82,38 +83,6 @@ class StringRendererPlugin(BasePlugin):
raise NotImplementedError
class StyleMixin(models.Model):
class StyleField(models.CharField):
# Allow overriding of STYLE_CHOICES in subclasses
def contribute_to_class(self, cls, name, **kwargs):
if hasattr(cls, 'STYLE_CHOICES'):
self.choices = cls.STYLE_CHOICES
super().contribute_to_class(cls, name, **kwargs)
STYLE_CHOICES = (
('default', _("default")),
)
style = StyleField(_("style"), max_length=50, null=True, blank=True)
class Meta:
abstract = True
def get_style_slug(self):
return getattr(self, 'style', None) or 'default'
def get_template_names(self):
# Should only be called by classes using filesystem templates
template_names = super().get_template_names() or []
template_names.extend([
"{path_prefix}style/_{style}.html".format(
path_prefix=self.get_path_prefix(),
style=self.get_style_slug()),
])
return template_names
class FilesystemTemplateRendererPlugin(BasePlugin):
template_name = None
path_prefix = 'plugins/' # TODO Rename to 'template_name_prefix'

0
content_plugins/plugins/__init__.py

38
content_plugins/plugins/mixins.py

@ -0,0 +1,38 @@
from django.db import models
from django.utils.translation import ugettext_lazy as _
class StyleMixin(models.Model):
class StyleField(models.CharField):
"""
Allows overriding of STYLE_CHOICES in subclasses.
"""
def contribute_to_class(self, cls, name, **kwargs):
if hasattr(cls, 'STYLE_CHOICES'):
self.choices = cls.STYLE_CHOICES
super().contribute_to_class(cls, name, **kwargs)
STYLE_CHOICES = (
('default', _("default")),
)
style = StyleField(_("style"), max_length=50, null=True, blank=True)
class Meta:
abstract = True
def get_style_slug(self):
return getattr(self, 'style', None) or 'default'
def get_template_names(self):
# Should only be called by classes using filesystem templates
template_names = super().get_template_names() or []
template_names.extend([
"{path_prefix}style/_{style}.html".format(
path_prefix=self.get_path_prefix(),
style=self.get_style_slug()),
])
return template_names
Loading…
Cancel
Save