Skip to content

Instantly share code, notes, and snippets.

@nimasmi
Last active August 29, 2015 14:21
Show Gist options
  • Save nimasmi/a0847d5cdef9a9461a21 to your computer and use it in GitHub Desktop.
Save nimasmi/a0847d5cdef9a9461a21 to your computer and use it in GitHub Desktop.
class TranslatedField(object):
def __init__(self, fieldname):
self.fieldname = fieldname
def get_translated_field(self, instance, language):
return getattr(instance, "_".join([self.fieldname, language]))
def __get__(self, instance, owner):
language = get_language()
translation = self.get_translated_field(instance, language)
return translation or language != 'en' and self.get_translated_field(instance, 'en') or translation
class TranslatedTitleMixin(models.Model):
title_fr = models.CharField(max_length=255, blank=True)
title_es = models.CharField(max_length=255, blank=True)
def get_title(self):
language = get_language()
fr = self.title_fr
es = self.title_es
en = self.title
if language == 'fr' and fr:
return fr
elif language == 'es' and es:
return es
else:
return en
class Meta:
abstract = True
class TranslatedPageMeta(PageBase):
""" Metaclass for `TranslatedPage` abstract base class, ensuring subclasses
have the correct `.edit_handler` attribute. This can't be directly declared
on the base class itself, because it refers to the foreign-language content
panels, which are only declared on the child classes. We don't bother about
declaring the `.edit_handler` attribute on the (abstract) base class, hence
the try/except clause.
"""
def __init__(classobject, classname, baseclasses, attrs):
try:
classobject.edit_handler = TabbedInterface([
ObjectList(classobject.english_content_panels, heading='En'),
ObjectList(classobject.french_content_panels + [classobject.french_extra_panels], heading='Fr'),
ObjectList(classobject.spanish_content_panels + [classobject.spanish_extra_panels], heading='Es'),
ObjectList(classobject.content_panels, heading='Common'),
ObjectList(classobject.promote_panels, heading='Promote'),
ObjectList(Page.settings_panels, heading='Settings', classname="settings"),
])
except AttributeError:
# Presumably one of the .english .french or .spanish_content_panels
# attributes is missing. Aborting is quite safe: we just default to
# the return value of wagtailadmin.views.page.get_page_edit_handler
pass
super().__init__(classname, baseclasses, attrs)
class TranslatedPage(Page, TranslatedTitleMixin, metaclass=TranslatedPageMeta):
is_abstract = True
feed_title_en = models.CharField(max_length=30, blank=True, help_text="The title when this page is displayed as a link elsewhere on the site.")
feed_title_fr = models.CharField(max_length=30, blank=True, help_text="The title when this page is displayed as a link elsewhere on the site.")
feed_title_es = models.CharField(max_length=30, blank=True, help_text="The title when this page is displayed as a link elsewhere on the site.")
feed_summary_en = models.TextField(blank=True, help_text="A description for when this page is displayed as a link elsewhere on the site.")
feed_summary_fr = models.TextField(blank=True, help_text="A description for when this page is displayed as a link elsewhere on the site.")
feed_summary_es = models.TextField(blank=True, help_text="A description for when this page is displayed as a link elsewhere on the site.")
_feed_title = TranslatedField('listings_title')
_feed_summary = TranslatedField('listings_summary')
class Meta:
abstract = True
def feed_title(self):
return self._feed_title or self.get_title()
def feed_summary(self):
try:
summary_text = self.summary_text
except AttributeError:
summary_text = ""
return self._feed_summary or summary_text
search_fields = Page.search_fields + (
index.SearchField('title_fr'),
index.SearchField('title_es'),
)
french_extra_panels = MultiFieldPanel(
(
FieldPanel('feed_title_fr'),
FieldPanel('feed_summary_fr'),
),
"Feeds"
)
spanish_extra_panels = MultiFieldPanel(
(
FieldPanel('feed_title_es'),
FieldPanel('feed_summary_es'),
),
"Feeds"
)
promote_panels = [
MultiFieldPanel(
(
FieldPanel('slug'),
FieldPanel('show_in_menus'),
),
"Common page configuration"
),
MultiFieldPanel(
(
ImageChooserPanel('listings_image'),
FieldPanel('feed_title_en'),
FieldPanel('feed_summary_en'),
),
"Feeds"
),
MultiFieldPanel(
(
FieldPanel('seo_title'),
FieldPanel('search_description'),
),
"Search Engines"
),
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment