From 5c9edeb5b87c527073d8ea4667e5cb7e2c1075c4 Mon Sep 17 00:00:00 2001 From: bkfox Date: Sat, 14 Jan 2017 20:57:19 +0100 Subject: [PATCH] fix rendering of links of the publication --- aircox_cms/models.py | 10 +-- aircox_cms/sections.py | 72 +------------------ aircox_cms/template.py | 72 +++++++++++++++++++ .../templates/aircox_cms/publication.html | 4 ++ 4 files changed, 83 insertions(+), 75 deletions(-) create mode 100644 aircox_cms/template.py diff --git a/aircox_cms/models.py b/aircox_cms/models.py index f145838..13314be 100755 --- a/aircox_cms/models.py +++ b/aircox_cms/models.py @@ -31,6 +31,7 @@ import aircox.models import aircox_cms.settings as settings from aircox_cms.utils import image_url +from aircox_cms.template import TemplateMixin from aircox_cms.sections import * @@ -193,7 +194,6 @@ class Comment(models.Model): _('comment'), ) - def __str__(self): # Translators: text shown in the comments list (in admin) return _('{date}, {author}: {content}...').format( @@ -222,8 +222,9 @@ class Comment(models.Model): return super().save(*args, **kwargs) -class PublicationRelatedLink(RelatedLinkBase): - parent = ParentalKey('Publication', related_name='related_links') +class PublicationRelatedLink(RelatedLinkBase,TemplateMixin): + template = 'aircox_cms/snippets/link.html' + parent = ParentalKey('Publication', related_name='links') class PublicationTag(TaggedItemBase): @@ -280,6 +281,7 @@ class Publication(Page): blank=True ) + class Meta: verbose_name = _('Publication') verbose_name_plural = _('Publication') @@ -297,7 +299,7 @@ class Publication(Page): FieldPanel('tags'), FieldPanel('focus'), ], heading=_('Content')), - InlinePanel('related_links', label=_('Links')) + InlinePanel('links', label=_('Links')) ] + Page.promote_panels settings_panels = Page.settings_panels + [ FieldPanel('publish_as'), diff --git a/aircox_cms/sections.py b/aircox_cms/sections.py index 1298a58..550e910 100755 --- a/aircox_cms/sections.py +++ b/aircox_cms/sections.py @@ -8,7 +8,6 @@ from django.utils import timezone as tz from django.utils.functional import cached_property from django.core.urlresolvers import reverse from django.template import Template, Context -from django.template.loader import render_to_string from django.contrib.contenttypes.models import ContentType from django.contrib.staticfiles.templatetags.staticfiles import static from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger @@ -20,8 +19,6 @@ from wagtail.wagtailadmin.edit_handlers import FieldPanel, FieldRowPanel, \ MultiFieldPanel, InlinePanel, PageChooserPanel, StreamFieldPanel from wagtail.wagtailsearch import index -from wagtail.wagtailcore.utils import camelcase_to_underscore - # snippets from wagtail.wagtailsnippets.edit_handlers import SnippetChooserPanel from wagtail.wagtailsnippets.models import register_snippet @@ -34,6 +31,7 @@ from taggit.models import TaggedItemBase # aircox import aircox.models +from aircox_cms.template import TemplateMixin def related_pages_filter(reset_cache=False): @@ -469,74 +467,6 @@ class DatedListBase(models.Model): } -class TemplateMixinMeta(models.base.ModelBase): - """ - Metaclass for SectionItem, assigning needed values such as `template`. - - It needs to load the item's template if the section uses the default - one, and throw error if there is an error in the template. - """ - def __new__(cls, name, bases, attrs): - from django.template.loader import get_template - from django.template import TemplateDoesNotExist - - cl = super().__new__(cls, name, bases, attrs) - if not hasattr(cl, '_meta'): - return cl - - if not 'template' in attrs: - cl.snake_name = camelcase_to_underscore(name) - cl.template = '{}/sections/{}.html'.format( - cl._meta.app_label, - cl.snake_name, - ) - if name != 'SectionItem': - try: - get_template(cl.template) - except TemplateDoesNotExist: - cl.template = 'aircox_cms/sections/section_item.html' - return cl - - -class TemplateMixin(metaclass=TemplateMixinMeta): - def get_context(self, request, page): - """ - Default context attributes: - * self: section being rendered - * page: current page being rendered - * request: request used to render the current page - - Other context attributes usable in the default template: - * content: **safe string** set as content of the section - * hide: DO NOT render the section, render only an empty string - """ - return { - 'self': self, - 'page': page, - 'request': request, - } - - def render(self, request, page, context, *args, **kwargs): - """ - Render the section. Page is the current publication being rendered. - - Rendering is similar to pages, using 'template' attribute set - by default to the app_label/sections/model_name_snake_case.html - - If the default template is not found, use SectionItem's one, - that can have a context attribute 'content' that is used to render - content. - """ - context_ = self.get_context(request, *args, page=page, **kwargs) - if context: - context_.update(context) - - if context.get('hide'): - return '' - return render_to_string(self.template, context_) - - - # # Sections # diff --git a/aircox_cms/template.py b/aircox_cms/template.py new file mode 100644 index 0000000..0e2d4dd --- /dev/null +++ b/aircox_cms/template.py @@ -0,0 +1,72 @@ +from django.db import models +from django.template.loader import render_to_string +from wagtail.wagtailcore.utils import camelcase_to_underscore + + +class TemplateMixinMeta(models.base.ModelBase): + """ + Metaclass for SectionItem, assigning needed values such as `template`. + + It needs to load the item's template if the section uses the default + one, and throw error if there is an error in the template. + """ + def __new__(cls, name, bases, attrs): + from django.template.loader import get_template + from django.template import TemplateDoesNotExist + + cl = super().__new__(cls, name, bases, attrs) + if not hasattr(cl, '_meta'): + return cl + + if not 'template' in attrs: + cl.snake_name = camelcase_to_underscore(name) + cl.template = '{}/sections/{}.html'.format( + cl._meta.app_label, + cl.snake_name, + ) + if name != 'SectionItem': + try: + get_template(cl.template) + except TemplateDoesNotExist: + cl.template = 'aircox_cms/sections/section_item.html' + return cl + + +class TemplateMixin(metaclass=TemplateMixinMeta): + def get_context(self, request, page): + """ + Default context attributes: + * self: section being rendered + * page: current page being rendered + * request: request used to render the current page + + Other context attributes usable in the default template: + * content: **safe string** set as content of the section + * hide: DO NOT render the section, render only an empty string + """ + return { + 'self': self, + 'page': page, + 'request': request, + } + + def render(self, request, page, context, *args, **kwargs): + """ + Render the section. Page is the current publication being rendered. + + Rendering is similar to pages, using 'template' attribute set + by default to the app_label/sections/model_name_snake_case.html + + If the default template is not found, use SectionItem's one, + that can have a context attribute 'content' that is used to render + content. + """ + context_ = self.get_context(request, *args, page=page, **kwargs) + if context: + context_.update(context) + + if context.get('hide'): + return '' + return render_to_string(self.template, context_) + + diff --git a/aircox_cms/templates/aircox_cms/publication.html b/aircox_cms/templates/aircox_cms/publication.html index 426a998..caa1c11 100755 --- a/aircox_cms/templates/aircox_cms/publication.html +++ b/aircox_cms/templates/aircox_cms/publication.html @@ -45,6 +45,10 @@ {{ page.body|richtext}} + {% if page.links %} + {% include "aircox_cms/sections/section_link_list.html" %} + {% endif %} + {% block content_extras %}{% endblock %}