forked from rc/aircox
fix rendering of links of the publication
This commit is contained in:
parent
a650c5a350
commit
5c9edeb5b8
|
@ -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'),
|
||||
|
|
|
@ -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
|
||||
#
|
||||
|
|
72
aircox_cms/template.py
Normal file
72
aircox_cms/template.py
Normal file
|
@ -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_)
|
||||
|
||||
|
|
@ -45,6 +45,10 @@
|
|||
{{ page.body|richtext}}
|
||||
</section>
|
||||
|
||||
{% if page.links %}
|
||||
{% include "aircox_cms/sections/section_link_list.html" %}
|
||||
{% endif %}
|
||||
|
||||
{% block content_extras %}{% endblock %}
|
||||
|
||||
<div class="post_content">
|
||||
|
|
Loading…
Reference in New Issue
Block a user