|
|
@ -1,8 +1,9 @@ |
|
|
|
|
|
|
|
import os |
|
|
|
|
|
|
|
|
|
|
|
from django.db import models |
|
|
|
from django.db import models |
|
|
|
from django.utils.translation import ugettext_lazy as _ |
|
|
|
from django.utils.translation import ugettext_lazy as _ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class StyleMixin(models.Model): |
|
|
|
|
|
|
|
class StyleField(models.CharField): |
|
|
|
class StyleField(models.CharField): |
|
|
|
""" |
|
|
|
""" |
|
|
|
Allows overriding of STYLE_CHOICES in subclasses. |
|
|
|
Allows overriding of STYLE_CHOICES in subclasses. |
|
|
@ -13,10 +14,9 @@ class StyleMixin(models.Model): |
|
|
|
self.choices = cls.STYLE_CHOICES |
|
|
|
self.choices = cls.STYLE_CHOICES |
|
|
|
super().contribute_to_class(cls, name, **kwargs) |
|
|
|
super().contribute_to_class(cls, name, **kwargs) |
|
|
|
|
|
|
|
|
|
|
|
STYLE_CHOICES = ( |
|
|
|
|
|
|
|
('default', _("default")), |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class StyleMixin(models.Model): |
|
|
|
|
|
|
|
STYLE_CHOICES = tuple() |
|
|
|
style = StyleField(_("style"), max_length=50, null=True, blank=True) |
|
|
|
style = StyleField(_("style"), max_length=50, null=True, blank=True) |
|
|
|
|
|
|
|
|
|
|
|
class Meta: |
|
|
|
class Meta: |
|
|
@ -25,25 +25,46 @@ class StyleMixin(models.Model): |
|
|
|
def get_style_slug(self): |
|
|
|
def get_style_slug(self): |
|
|
|
return getattr(self, 'style', None) or 'default' |
|
|
|
return getattr(self, 'style', None) or 'default' |
|
|
|
|
|
|
|
|
|
|
|
# Compatibiliy with super classes not having a prefixed_path method |
|
|
|
# # Compatibiliy with super classes not having a prefixed_path method |
|
|
|
def prefixed_path(self, path): |
|
|
|
# def prefixed_path(self, path): |
|
|
|
if hasattr(super(), 'prefixed_path'): |
|
|
|
# if hasattr(super(), 'prefixed_path'): |
|
|
|
return super().prefixed_path(path) |
|
|
|
# return super().prefixed_path(path) |
|
|
|
else: |
|
|
|
# else: |
|
|
|
return path |
|
|
|
# return path |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def add_styled_template_names(self, template_names): |
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
if super().get_template_names(): |
|
|
|
|
|
|
|
[ |
|
|
|
|
|
|
|
"article/_richtext.html", |
|
|
|
|
|
|
|
"_richtext.html", |
|
|
|
|
|
|
|
] |
|
|
|
|
|
|
|
then add_styled_template_names returns |
|
|
|
|
|
|
|
[ |
|
|
|
|
|
|
|
"article/_richtext/_<style>.html", |
|
|
|
|
|
|
|
"article/_richtext.html", |
|
|
|
|
|
|
|
"_richtext/_<style>.html", |
|
|
|
|
|
|
|
"_richtext.html", |
|
|
|
|
|
|
|
] |
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
extended_template_names = [] |
|
|
|
|
|
|
|
for t in template_names: |
|
|
|
|
|
|
|
name, ext = os.path.splitext(t) |
|
|
|
|
|
|
|
extended_template_names += [ |
|
|
|
|
|
|
|
"{name}/_{style}.{ext}".format( |
|
|
|
|
|
|
|
name=name, style=self.style, ext=ext), |
|
|
|
|
|
|
|
t |
|
|
|
|
|
|
|
] |
|
|
|
|
|
|
|
return extended_template_names |
|
|
|
|
|
|
|
|
|
|
|
def get_template_names(self): |
|
|
|
def get_template_names(self): |
|
|
|
if hasattr(super(), 'get_template_names'): |
|
|
|
if hasattr(super(), 'get_template_names'): |
|
|
|
template_names = super().get_template_names() |
|
|
|
template_names = super().get_template_names() |
|
|
|
|
|
|
|
if self.style: |
|
|
|
|
|
|
|
template_names = self.add_styled_template_names(template_names) |
|
|
|
|
|
|
|
return template_names |
|
|
|
else: |
|
|
|
else: |
|
|
|
template_names = [] |
|
|
|
return [] |
|
|
|
|
|
|
|
|
|
|
|
return template_names + [ |
|
|
|
|
|
|
|
self.prefixed_path( |
|
|
|
|
|
|
|
"style/_{style}.html".format( |
|
|
|
|
|
|
|
style=self.get_style_slug()), |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_plugin_context(self, context=None, **kwargs): |
|
|
|
def get_plugin_context(self, context=None, **kwargs): |
|
|
|
if hasattr(super(), 'get_plugin_context'): |
|
|
|
if hasattr(super(), 'get_plugin_context'): |
|
|
|