From 6912d6eeab7b654fbaf6f620ee41c22dc20e1cb8 Mon Sep 17 00:00:00 2001 From: Erik Stein Date: Mon, 20 Aug 2018 19:45:09 +0200 Subject: [PATCH] Added PageTitlesFunctionMixin. --- CHANGES | 1 + shared/utils/models/pages.py | 61 +++++++++++++++++++----------------- 2 files changed, 34 insertions(+), 28 deletions(-) create mode 100644 CHANGES diff --git a/CHANGES b/CHANGES new file mode 100644 index 0000000..f319300 --- /dev/null +++ b/CHANGES @@ -0,0 +1 @@ +Added PageTitlesFunctionMixin. diff --git a/shared/utils/models/pages.py b/shared/utils/models/pages.py index d4f5dfb..93926df 100644 --- a/shared/utils/models/pages.py +++ b/shared/utils/models/pages.py @@ -9,9 +9,9 @@ from django.utils.text import normalize_newlines from django.utils.translation import ugettext_lazy as _ from shared.multilingual.utils import i18n_fields_list -from shared.utils.text import slimdown from ..fields import AutoSlugField from ..functional import firstof +from ..text import slimdown USE_TRANSLATABLE_FIELDS = getattr(settings, 'CONTENT_USE_TRANSLATABLE_FIELDS', False) @@ -25,44 +25,24 @@ if USE_TRANSLATABLE_FIELDS: # TODO Leave window_title alone, do not slimdown -# TODO Use translatable fields by default -@python_2_unicode_compatible -class PageTitlesMixin(models.Model): - """ - A model mixin containg title and slug field for models serving as website - pages with an URL. - """ - # FIXME signals are not sent from abstract models, therefore AutoSlugField doesn't work - if USE_TRANSLATABLE_FIELDS: - short_title = TranslatableCharField(_("Name"), max_length=50) - title = TranslatableTextField(_("Titel (Langform)"), null=True, blank=True, max_length=300) - window_title = TranslatableCharField(_("Fenster-/Suchmaschinentitel"), null=True, blank=True, max_length=300) - # FIXME populate_from should use settings.LANGUAGE - slug = AutoSlugField(_("URL-Name"), max_length=200, populate_from='short_title_de', unique_slug=True, blank=True) - - else: - short_title = models.CharField(_("Name"), max_length=50) - title = models.TextField(_("Titel (Langform)"), null=True, blank=True, max_length=300) - window_title = models.CharField(_("Fenster-/Suchmaschinentitel"), null=True, blank=True, max_length=300) - slug = AutoSlugField(_("URL-Name"), max_length=200, populate_from='short_title', unique_slug=True, blank=True) - - class Meta: - abstract = True - +class PageTitlesFunctionMixin(object): def __str__(self): - return strip_tags(slimdown(self.short_title)) + return strip_tags(slimdown(self.get_short_title())) def get_title(self): return slimdown(firstof( self.title, - self.short_title + self.get_short_title() )) + def get_short_title(self): + return self.short_title + def get_window_title(self): return strip_tags(slimdown( firstof( self.window_title, - self.short_title, + self.get_short_title(), self.get_first_title_line(), ) )) @@ -84,6 +64,31 @@ class PageTitlesMixin(models.Model): ) +# TODO Use translatable fields by default +@python_2_unicode_compatible +class PageTitlesMixin(models.Model, PageTitlesFunctionMixin): + """ + A model mixin containg title and slug field for models serving as website + pages with an URL. + """ + # FIXME signals are not sent from abstract models, therefore AutoSlugField doesn't work + if USE_TRANSLATABLE_FIELDS: + short_title = TranslatableCharField(_("Name"), max_length=50) + title = TranslatableTextField(_("Titel (Langform)"), null=True, blank=True, max_length=300) + window_title = TranslatableCharField(_("Fenster-/Suchmaschinentitel"), null=True, blank=True, max_length=300) + # FIXME populate_from should use settings.LANGUAGE + slug = AutoSlugField(_("URL-Name"), max_length=200, populate_from='short_title_de', unique_slug=True, blank=True) + + else: + short_title = models.CharField(_("Name"), max_length=50) + title = models.TextField(_("Titel (Langform)"), null=True, blank=True, max_length=300) + window_title = models.CharField(_("Fenster-/Suchmaschinentitel"), null=True, blank=True, max_length=300) + slug = AutoSlugField(_("URL-Name"), max_length=200, populate_from='short_title', unique_slug=True, blank=True) + + class Meta: + abstract = True + + class PageTitleAdminMixin(object): search_fields = ['short_title', 'title', 'window_title'] list_display = ['short_title', 'slug']