import os from django.db import models from django.utils.translation import ugettext_lazy as _ from shared.utils.text import slugify 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) class StyleMixin(models.Model): STYLE_CHOICES = tuple() style = StyleField(_("style"), max_length=50, null=True, blank=True) class Meta: abstract = True def get_style_slug(self): style = getattr(self, 'style', None) or 'default' return slugify(style).replace("_", "-") # TODO Remove stale code # # Compatibiliy with super classes not having a prefixed_path method # def prefixed_path(self, path): # if hasattr(super(), 'prefixed_path'): # return super().prefixed_path(path) # else: # 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/_