diff --git a/content_plugins/base.py b/content_plugins/base.py index 2ab5fe4..3d67268 100644 --- a/content_plugins/base.py +++ b/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' diff --git a/content_plugins/plugins/__init__.py b/content_plugins/plugins/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/content_plugins/plugins/mixins.py b/content_plugins/plugins/mixins.py new file mode 100644 index 0000000..5c62807 --- /dev/null +++ b/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 + +