Browse Source

Added PageTitlesFunctionMixin.

master 0.2.3
Erik Stein 7 years ago
parent
commit
6912d6eeab
  1. 1
      CHANGES
  2. 61
      shared/utils/models/pages.py

1
CHANGES

@ -0,0 +1 @@
Added PageTitlesFunctionMixin.

61
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 django.utils.translation import ugettext_lazy as _
from shared.multilingual.utils import i18n_fields_list from shared.multilingual.utils import i18n_fields_list
from shared.utils.text import slimdown
from ..fields import AutoSlugField from ..fields import AutoSlugField
from ..functional import firstof from ..functional import firstof
from ..text import slimdown
USE_TRANSLATABLE_FIELDS = getattr(settings, 'CONTENT_USE_TRANSLATABLE_FIELDS', False) 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 Leave window_title alone, do not slimdown
# TODO Use translatable fields by default class PageTitlesFunctionMixin(object):
@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
def __str__(self): def __str__(self):
return strip_tags(slimdown(self.short_title)) return strip_tags(slimdown(self.get_short_title()))
def get_title(self): def get_title(self):
return slimdown(firstof( return slimdown(firstof(
self.title, self.title,
self.short_title self.get_short_title()
)) ))
def get_short_title(self):
return self.short_title
def get_window_title(self): def get_window_title(self):
return strip_tags(slimdown( return strip_tags(slimdown(
firstof( firstof(
self.window_title, self.window_title,
self.short_title, self.get_short_title(),
self.get_first_title_line(), 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): class PageTitleAdminMixin(object):
search_fields = ['short_title', 'title', 'window_title'] search_fields = ['short_title', 'title', 'window_title']
list_display = ['short_title', 'slug'] list_display = ['short_title', 'slug']

Loading…
Cancel
Save