forked from rc/aircox
		
	fix rendering of links of the publication
This commit is contained in:
		@ -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
 | 
			
		||||
#
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user