some fixes

This commit is contained in:
bkfox 2016-11-15 21:58:55 +01:00
parent f183669c04
commit fc8b15eb03
4 changed files with 75 additions and 22 deletions

View File

@ -222,9 +222,10 @@ class Comment(models.Model):
return super().save(*args, **kwargs) return super().save(*args, **kwargs)
class RelatedLink(RelatedLinkBase): class PublicationRelatedLink(RelatedLinkBase):
parent = ParentalKey('Publication', related_name='related_links') parent = ParentalKey('Publication', related_name='related_links')
class PublicationTag(TaggedItemBase): class PublicationTag(TaggedItemBase):
content_object = ParentalKey('Publication', related_name='tagged_items') content_object = ParentalKey('Publication', related_name='tagged_items')

View File

@ -9,6 +9,7 @@ from django.utils.functional import cached_property
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.template.loader import render_to_string from django.template.loader import render_to_string
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.contrib.staticfiles.templatetags.staticfiles import static
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from wagtail.wagtailcore.models import Page, Orderable from wagtail.wagtailcore.models import Page, Orderable
@ -99,18 +100,32 @@ class RelatedLinkBase(Orderable):
null=True, blank=True, null=True, blank=True,
on_delete=models.SET_NULL, on_delete=models.SET_NULL,
related_name='+', related_name='+',
help_text = _('icon to display before the url'), help_text = _(
'icon from the gallery'
),
)
icon_path = models.CharField(
_('icon path'),
null=True, blank=True,
max_length=128,
help_text = _(
'icon from a given URL or path in the directory of static files'
)
) )
# icon = models.ImageField(
# verbose_name = _('icon'),
# null=True, blank=True,
# help_text = _('icon to display before the url'),
#)
text = models.CharField( text = models.CharField(
_('text'), _('text'),
max_length = 64, max_length = 64,
null = True, blank=True, null = True, blank=True,
help_text = _('text to display of the link'), help_text = _('text of the link'),
)
info = models.CharField(
_('info'),
max_length = 128,
null=True, blank=True,
help_text = _(
'description displayed in a popup when the mouse hovers '
'the link'
)
) )
class Meta: class Meta:
@ -119,25 +134,39 @@ class RelatedLinkBase(Orderable):
panels = [ panels = [
MultiFieldPanel([ MultiFieldPanel([
FieldPanel('text'), FieldPanel('text'),
FieldPanel('info'),
ImageChooserPanel('icon'), ImageChooserPanel('icon'),
FieldPanel('icon_path'),
FieldPanel('url'), FieldPanel('url'),
PageChooserPanel('page'), PageChooserPanel('page'),
], heading=_('link')) ], heading=_('link'))
] ]
def icon_url(self):
"""
Return icon_path as a complete url, since it can either be an
url or a path to static file.
"""
if self.icon_path.startswith('http://') or \
self.icon_path.startswith('https://'):
return self.icon_path
return static(self.icon_path)
def as_dict(self): def as_dict(self):
""" """
Return compiled values from parameters as dict with Return compiled values from parameters as dict with
'url', 'icon', 'text' 'url', 'icon', 'text'
""" """
if self.page: if self.page:
url, text = self.page.url, self.page.title url, text = self.page.url, self.text or self.page.title
else: else:
url, text = self.url, self.url url, text = self.url, self.text or self.url
return { return {
'url': url, 'url': url,
'text': self.text or text, 'text': text,
'icon': self.icon 'info': self.info,
'icon': self.icon,
'icon_path': self.icon_path and self.icon_url(),
} }
@ -467,6 +496,11 @@ class Section(ClusterableModel):
help_text = _('name of the template block in which the section must ' help_text = _('name of the template block in which the section must '
'be set'), 'be set'),
) )
order = models.IntegerField(
_('order'),
default = 100,
help_text = _('order of rendering, the higher the latest')
)
model = models.ForeignKey( model = models.ForeignKey(
ContentType, ContentType,
verbose_name = _('model'), verbose_name = _('model'),
@ -510,7 +544,7 @@ class Section(ClusterableModel):
model = ContentType.objects.get_for_model(page).pk model = ContentType.objects.get_for_model(page).pk
) )
) )
return qs return qs.order_by('order','pk')
def add_item(self, item): def add_item(self, item):
""" """
@ -521,12 +555,9 @@ class Section(ClusterableModel):
item.save() item.save()
def render(self, request, page = None, context = None, *args, **kwargs): def render(self, request, page = None, context = None, *args, **kwargs):
# if self.page and page != self.page.specific:
# return ''
return ''.join([ return ''.join([
item.specific.render(request, page, context, *args, **kwargs) item.specific.render(request, page, context, *args, **kwargs)
for item in self.items.all().order_by('order') for item in self.items.all().order_by('order','pk')
]) ])
def __str__(self): def __str__(self):
@ -567,7 +598,7 @@ class SectionItem(models.Model,metaclass=SectionItemMeta):
order = models.IntegerField( order = models.IntegerField(
_('order'), _('order'),
default = 100, default = 100,
help_text = _('position in the menu. The higher, the latest to render') help_text = _('order of rendering, the higher the latest')
) )
real_type = models.CharField( real_type = models.CharField(
max_length=32, max_length=32,
@ -772,12 +803,27 @@ class SectionImage(SectionRelativeItem):
@register_snippet @register_snippet
class SectionLink(RelatedLinkBase, SectionItem): class SectionLinkList(ClusterableModel, SectionItem):
panels = SectionItem.panels + [
InlinePanel('links', label=_('Links')),
]
@register_snippet
class SectionLink(RelatedLinkBase):
""" """
Render a link to a page or a given url. Render a link to a page or a given url.
Can either be used standalone or in a SectionLinkList Can either be used standalone or in a SectionLinkList
""" """
panels = SectionItem.panels + RelatedLinkBase.panels parent = ParentalKey(
'SectionLinkList', related_name = 'links',
null = True
)
def __str__(self):
return 'link: {} #{}'.format(
self.text or (self.page and self.page.title) or self.title,
self.pk
)
@register_snippet @register_snippet

View File

@ -1,8 +1,10 @@
<section class="section_item {{ self.css_class }} {{ self.snake_name }}"> <section class="section_item {{ self.css_class }} {{ self.snake_name }}">
{% block section_content %}
{% block title %} {% block title %}
{% if self.show_title %}<h2>{{ self.title }}</h2>{% endif %} {% if self.show_title %}<h2>{{ self.title }}</h2>{% endif %}
{% endblock %} {% endblock %}
{% block content %}{{ content|safe }}{% endblock %} {% block content %}{{ content|safe }}{% endblock %}
{% endblock %}
</section> </section>

View File

@ -3,8 +3,12 @@
{% block content %} {% block content %}
{% with link=self.as_dict %} {% with link=self.as_dict %}
<a href="{{ link.url }}"> <a href="{{ link.url }}" {% if self.info %}title="{{ self.info }}"{% endif %}>
{% if link.icon %}{% image link.icon fill-32x32 class="icon link_icon" height='' width='' %}{% endif %} {% if link.icon %}
{% image link.icon fill-32x32 class="icon link_icon" height='' width='' %}
{% elif link.icon_path %}
<img src="{{ link.icon_path }}" class="icon link_icon"/>
{% endif %}
{{ link.text }} {{ link.text }}
</a> </a>
{% endwith %} {% endwith %}