sections rendering + make view for existing section's items
This commit is contained in:
@ -396,7 +396,7 @@ class DiffusionPage(Publication):
|
|||||||
Return a DiffusionPage or ListItem from a Diffusion
|
Return a DiffusionPage or ListItem from a Diffusion
|
||||||
"""
|
"""
|
||||||
if diff.page.all().count():
|
if diff.page.all().count():
|
||||||
item = diff.page.live().first()
|
item = diff.page.all().first()
|
||||||
else:
|
else:
|
||||||
item = ListItem(
|
item = ListItem(
|
||||||
title = '{}, {}'.format(
|
title = '{}, {}'.format(
|
||||||
@ -493,7 +493,7 @@ class ListPage(Page):
|
|||||||
|
|
||||||
def get_context(self, request, *args, **kwargs):
|
def get_context(self, request, *args, **kwargs):
|
||||||
context = super().get_context(request, *args, **kwargs)
|
context = super().get_context(request, *args, **kwargs)
|
||||||
qs = ListBase.get_queryset_from_request(request, context=context)
|
qs = ListBase.from_request(request, context=context)
|
||||||
context['object_list'] = qs
|
context['object_list'] = qs
|
||||||
return context
|
return context
|
||||||
|
|
||||||
|
373
cms/sections.py
373
cms/sections.py
@ -1,11 +1,11 @@
|
|||||||
import datetime
|
import datetime
|
||||||
import re
|
import re
|
||||||
from enum import Enum, IntEnum
|
from enum import IntEnum
|
||||||
|
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.utils.translation import ugettext as _, ugettext_lazy
|
from django.utils.translation import ugettext as _, ugettext_lazy
|
||||||
from django.utils import timezone as tz
|
from django.utils import timezone as tz
|
||||||
|
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.core.paginator import Paginator, EmptyPage, PageNotAnInteger
|
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
|
||||||
@ -23,6 +23,8 @@ from wagtail.wagtailcore.utils import camelcase_to_underscore
|
|||||||
from wagtail.wagtailsnippets.edit_handlers import SnippetChooserPanel
|
from wagtail.wagtailsnippets.edit_handlers import SnippetChooserPanel
|
||||||
from wagtail.wagtailsnippets.models import register_snippet
|
from wagtail.wagtailsnippets.models import register_snippet
|
||||||
|
|
||||||
|
from wagtail.wagtailimages.utils import generate_signature
|
||||||
|
|
||||||
# tags
|
# tags
|
||||||
from modelcluster.models import ClusterableModel
|
from modelcluster.models import ClusterableModel
|
||||||
from modelcluster.fields import ParentalKey
|
from modelcluster.fields import ParentalKey
|
||||||
@ -30,27 +32,28 @@ from modelcluster.tags import ClusterTaggableManager
|
|||||||
from taggit.models import TaggedItemBase
|
from taggit.models import TaggedItemBase
|
||||||
|
|
||||||
|
|
||||||
def related_pages_filter():
|
def related_pages_filter(reset_cache=False):
|
||||||
"""
|
"""
|
||||||
Return a dict that can be used to filter foreignkey to pages'
|
Return a dict that can be used to filter foreignkey to pages'
|
||||||
subtype declared in aircox.cms.models.
|
subtype declared in aircox.cms.models.
|
||||||
|
|
||||||
Note: the filter is calculated at the first call of the function.
|
This value is stored in cache, but it is possible to reset the
|
||||||
|
cache using the `reset_cache` parameter.
|
||||||
"""
|
"""
|
||||||
if hasattr(related_pages_filter, 'filter'):
|
if not reset_cache and hasattr(related_pages_filter, 'cache'):
|
||||||
return related_pages_filter.filter
|
return related_pages_filter.cache
|
||||||
|
|
||||||
import aircox.cms.models as cms
|
import aircox.cms.models as cms
|
||||||
import inspect
|
import inspect
|
||||||
related_pages_filter.filter = {
|
related_pages_filter.cache = {
|
||||||
'model__in': (name.lower() for name, member in
|
'model__in': list(name.lower() for name, member in
|
||||||
inspect.getmembers(cms,
|
inspect.getmembers(cms,
|
||||||
lambda x: inspect.isclass(x) and issubclass(x, Page)
|
lambda x: inspect.isclass(x) and issubclass(x, Page)
|
||||||
)
|
)
|
||||||
if member != Page
|
if member != Page
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
return related_pages_filter.filter
|
return related_pages_filter.cache
|
||||||
|
|
||||||
|
|
||||||
class ListItem:
|
class ListItem:
|
||||||
@ -113,6 +116,21 @@ class RelatedLinkBase(Orderable):
|
|||||||
], heading=_('link'))
|
], heading=_('link'))
|
||||||
]
|
]
|
||||||
|
|
||||||
|
def as_dict(self):
|
||||||
|
"""
|
||||||
|
Return compiled values from parameters as dict with
|
||||||
|
'url', 'icon', 'text'
|
||||||
|
"""
|
||||||
|
if self.page:
|
||||||
|
url, text = self.page.url, self.page.title
|
||||||
|
else:
|
||||||
|
url, text = self.url, self.url
|
||||||
|
return {
|
||||||
|
'url': url,
|
||||||
|
'text': self.text or text,
|
||||||
|
'icon': self.icon
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class ListBase(models.Model):
|
class ListBase(models.Model):
|
||||||
"""
|
"""
|
||||||
@ -125,96 +143,128 @@ class ListBase(models.Model):
|
|||||||
before_related = 0x03,
|
before_related = 0x03,
|
||||||
after_related = 0x04,
|
after_related = 0x04,
|
||||||
|
|
||||||
filter_date = models.SmallIntegerField(
|
date_filter = models.SmallIntegerField(
|
||||||
verbose_name = _('filter by date'),
|
verbose_name = _('filter by date'),
|
||||||
choices = [ (int(y), _(x.replace('_', ' ')))
|
choices = [ (int(y), _(x.replace('_', ' ')))
|
||||||
for x,y in DateFilter.__members__.items() ],
|
for x,y in DateFilter.__members__.items() ],
|
||||||
blank = True, null = True,
|
blank = True, null = True,
|
||||||
)
|
)
|
||||||
filter_model = models.ForeignKey(
|
model = models.ForeignKey(
|
||||||
ContentType,
|
ContentType,
|
||||||
verbose_name = _('filter by type'),
|
verbose_name = _('filter by type'),
|
||||||
blank = True, null = True,
|
blank = True, null = True,
|
||||||
help_text = _('if set, select only elements that are of this type'),
|
help_text = _('if set, select only elements that are of this type'),
|
||||||
limit_choices_to = related_pages_filter,
|
limit_choices_to = related_pages_filter,
|
||||||
)
|
)
|
||||||
filter_related = models.ForeignKey(
|
related = models.ForeignKey(
|
||||||
'Publication',
|
Page,
|
||||||
verbose_name = _('filter by a related publication'),
|
verbose_name = _('filter by a related publication'),
|
||||||
blank = True, null = True,
|
blank = True, null = True,
|
||||||
help_text = _('if set, select children or siblings of this publication'),
|
help_text = _('if set, select children or siblings related to this page'),
|
||||||
)
|
)
|
||||||
related_sibling = models.BooleanField(
|
siblings = models.BooleanField(
|
||||||
verbose_name = _('related is a sibling'),
|
verbose_name = _('select siblings of related'),
|
||||||
default = False,
|
default = False,
|
||||||
help_text = _('if selected select related publications that are '
|
help_text = _('if selected select related publications that are '
|
||||||
'siblings of this one'),
|
'siblings of this one'),
|
||||||
)
|
)
|
||||||
sort_asc = models.BooleanField(
|
asc = models.BooleanField(
|
||||||
verbose_name = _('order ascending'),
|
verbose_name = _('ascending order'),
|
||||||
default = True,
|
default = True,
|
||||||
help_text = _('if selected sort list in the ascending order by date')
|
help_text = _('if selected sort list in the ascending order by date')
|
||||||
)
|
)
|
||||||
|
|
||||||
panels = [
|
panels = [
|
||||||
MultiFieldPanel([
|
MultiFieldPanel([
|
||||||
FieldPanel('filter_model'),
|
FieldPanel('model'),
|
||||||
FieldPanel('filter_related'),
|
PageChooserPanel('related'),
|
||||||
FieldPanel('related_sibling'),
|
FieldPanel('siblings'),
|
||||||
], heading=_('filters')),
|
], heading=_('filters')),
|
||||||
MultiFieldPanel([
|
MultiFieldPanel([
|
||||||
FieldPanel('filter_date'),
|
FieldPanel('date_filter'),
|
||||||
FieldPanel('sort_asc'),
|
FieldPanel('asc'),
|
||||||
], heading=_('sorting'))
|
], heading=_('sorting'))
|
||||||
]
|
]
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
abstract = True
|
abstract = True
|
||||||
|
|
||||||
@classmethod
|
def get_queryset(self):
|
||||||
def get_base_queryset(cl, filter_date = None, filter_model = None,
|
|
||||||
filter_related = None, related_siblings = None,
|
|
||||||
sort_asc = True):
|
|
||||||
"""
|
"""
|
||||||
Get queryset based on the arguments. This class is intended to be
|
Get queryset based on the arguments. This class is intended to be
|
||||||
reusable by other classes if needed.
|
reusable by other classes if needed.
|
||||||
"""
|
"""
|
||||||
|
from aircox.cms.models import Publication
|
||||||
|
related = self.related and self.related.specific()
|
||||||
|
|
||||||
# model
|
# model
|
||||||
if filter_model:
|
if self.model:
|
||||||
qs = filter_model.objects.all()
|
qs = self.model.model_class().objects.all()
|
||||||
else:
|
else:
|
||||||
qs = Publication.objects.all()
|
qs = Publication.objects.all()
|
||||||
qs = qs.live().not_in_section()
|
qs = qs.live().not_in_menu()
|
||||||
|
|
||||||
# related
|
# related
|
||||||
if filter_related:
|
if related:
|
||||||
if related_siblings:
|
if self.siblings:
|
||||||
qs = qs.sibling_of(filter_related)
|
qs = qs.sibling_of(related)
|
||||||
else:
|
else:
|
||||||
qs = qs.descendant_of(filter_related)
|
qs = qs.descendant_of(related)
|
||||||
|
|
||||||
date = filter_related.date
|
date = self.related.date if hasattr('date', related) else \
|
||||||
if filter_date == cl.DateFilter.before_related:
|
self.related.first_published_at
|
||||||
|
if self.date_filter == self.DateFilter.before_related:
|
||||||
qs = qs.filter(date__lt = date)
|
qs = qs.filter(date__lt = date)
|
||||||
elif filter_date == cl.DateFilter.after_related:
|
elif self.date_filter == self.DateFilter.after_related:
|
||||||
qs = qs.filter(date__gte = date)
|
qs = qs.filter(date__gte = date)
|
||||||
# date
|
# date
|
||||||
else:
|
else:
|
||||||
date = tz.now()
|
date = tz.now()
|
||||||
if filter_date == cl.DateFilter.previous:
|
if self.date_filter == self.DateFilter.previous:
|
||||||
qs = qs.filter(date__lt = date)
|
qs = qs.filter(date__lt = date)
|
||||||
elif filter_date == cl.DateFilter.next:
|
elif self.date_filter == self.DateFilter.next:
|
||||||
qs = qs.filter(date__gte = date)
|
qs = qs.filter(date__gte = date)
|
||||||
|
|
||||||
# sort
|
# sort
|
||||||
if sort_asc:
|
if self.asc:
|
||||||
return qs.order_by('date', 'pk')
|
return qs.order_by('date', 'pk')
|
||||||
return qs.order_by('-date', '-pk')
|
return qs.order_by('-date', '-pk')
|
||||||
|
|
||||||
|
def to_url(self, list_page = None, **kwargs):
|
||||||
|
"""
|
||||||
|
Return a url parameters from self. Extra named parameters are used
|
||||||
|
to override values of self or add some to the parameters.
|
||||||
|
|
||||||
|
If there is related field use it to get the page, otherwise use the
|
||||||
|
given list_page or the first ListPage it finds.
|
||||||
|
"""
|
||||||
|
import aircox.cms.models as models
|
||||||
|
|
||||||
|
params = {
|
||||||
|
'date_filter': self.get_date_filter_display(),
|
||||||
|
'model': self.model and self.model.model,
|
||||||
|
'asc': self.asc,
|
||||||
|
'related': self.related,
|
||||||
|
'siblings': self.siblings,
|
||||||
|
}
|
||||||
|
params.update(kwargs)
|
||||||
|
|
||||||
|
page = params.get('related') or list_page or \
|
||||||
|
models.ListPage.objects.all().first()
|
||||||
|
|
||||||
|
if params.get('related'):
|
||||||
|
params['related'] = True
|
||||||
|
|
||||||
|
params = '&'.join([
|
||||||
|
key if value == True else '{}={}'.format(key, value)
|
||||||
|
for key, value in params.items()
|
||||||
|
if value
|
||||||
|
])
|
||||||
|
return page.url + '?' + params
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_queryset_from_request(cl, request, *args,
|
def from_request(cl, request, related = None, context = None,
|
||||||
related = None, context = {},
|
*args, **kwargs):
|
||||||
**kwargs):
|
|
||||||
"""
|
"""
|
||||||
Return a queryset from the request's GET parameters. Context
|
Return a queryset from the request's GET parameters. Context
|
||||||
can be used to update relative informations.
|
can be used to update relative informations.
|
||||||
@ -222,10 +272,12 @@ class ListBase(models.Model):
|
|||||||
This function can be used by other views if needed
|
This function can be used by other views if needed
|
||||||
|
|
||||||
Parameters:
|
Parameters:
|
||||||
|
* date_filter: one of DateFilter attribute's key.
|
||||||
* model: ['program','diffusion','event'] type of the publication
|
* model: ['program','diffusion','event'] type of the publication
|
||||||
* asc: if present, sort ascending instead of descending
|
* asc: if present, sort ascending instead of descending
|
||||||
* related: children of the thread passed in arguments only
|
* related: children of the thread passed in arguments only
|
||||||
* siblings: sibling of the related instead of children
|
* siblings: sibling of the related instead of children
|
||||||
|
|
||||||
* tag: tag to search for
|
* tag: tag to search for
|
||||||
* search: query to search in the publications
|
* search: query to search in the publications
|
||||||
* page: page number
|
* page: page number
|
||||||
@ -236,17 +288,29 @@ class ListBase(models.Model):
|
|||||||
arguments passed to ListBase.get_base_queryset
|
arguments passed to ListBase.get_base_queryset
|
||||||
* paginator: paginator object
|
* paginator: paginator object
|
||||||
"""
|
"""
|
||||||
|
def set(key, value):
|
||||||
|
if context is not None:
|
||||||
|
context[key] = value
|
||||||
|
|
||||||
|
date_filter = request.GET.get('date_filter')
|
||||||
model = request.GET.get('model')
|
model = request.GET.get('model')
|
||||||
|
|
||||||
kwargs = {
|
kwargs = {
|
||||||
'filter_model': ProgramPage if model == 'program' else \
|
'date_filter':
|
||||||
DiffusionPage if model == 'diffusion' else \
|
int(getattr(cl.DateFilter, date_filter))
|
||||||
EventPage if model == 'event' else None,
|
if date_filter and hasattr(cl.DateFilter, date_filter)
|
||||||
'filter_related': 'related' in request.GET and related,
|
else None,
|
||||||
'related_siblings': 'siblings' in request.GET,
|
'model':
|
||||||
'sort_asc': 'asc' in request.GET,
|
ProgramPage if model == 'program' else
|
||||||
|
DiffusionPage if model == 'diffusion' else
|
||||||
|
EventPage if model == 'event' else None,
|
||||||
|
'related': 'related' in request.GET and related,
|
||||||
|
'siblings': 'siblings' in request.GET,
|
||||||
|
'asc': 'asc' in request.GET,
|
||||||
}
|
}
|
||||||
qs = cl.get_base_queryset(**kwargs)
|
|
||||||
|
base_list = cl(**{ k:v for k,v in kwargs.items() if v })
|
||||||
|
qs = base_list.get_queryset()
|
||||||
|
|
||||||
# filter by tag
|
# filter by tag
|
||||||
tag = request.GET.get('tag')
|
tag = request.GET.get('tag')
|
||||||
@ -260,7 +324,7 @@ class ListBase(models.Model):
|
|||||||
kwargs['terms'] = search
|
kwargs['terms'] = search
|
||||||
qs = qs.search(search)
|
qs = qs.search(search)
|
||||||
|
|
||||||
context['list_selector'] = kwargs
|
set('list_selector', kwargs)
|
||||||
|
|
||||||
# paginator
|
# paginator
|
||||||
if qs:
|
if qs:
|
||||||
@ -271,8 +335,8 @@ class ListBase(models.Model):
|
|||||||
qs = paginator.page(1)
|
qs = paginator.page(1)
|
||||||
except EmptyPage:
|
except EmptyPage:
|
||||||
qs = parginator.page(paginator.num_pages)
|
qs = parginator.page(paginator.num_pages)
|
||||||
context['paginator'] = paginator
|
set('paginator', paginator)
|
||||||
context['object_list'] = qs
|
set('object_list', qs)
|
||||||
return qs
|
return qs
|
||||||
|
|
||||||
|
|
||||||
@ -405,10 +469,26 @@ class Section(ClusterableModel):
|
|||||||
def __str__(self):
|
def __str__(self):
|
||||||
return '{}: {}'.format(self.__class__.__name__, self.name or self.pk)
|
return '{}: {}'.format(self.__class__.__name__, self.name or self.pk)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_sections_at (cl, position, page = None):
|
||||||
|
"""
|
||||||
|
Return a queryset of sections that are at the given position.
|
||||||
|
Filter out Section that are not for the given page.
|
||||||
|
"""
|
||||||
|
qs = Section.objects.filter(position = position)
|
||||||
|
if page:
|
||||||
|
qs = qs.filter(
|
||||||
|
models.Q(model__isnull = True) |
|
||||||
|
models.Q(
|
||||||
|
model = ContentType.objects.get_for_model(page).pk
|
||||||
|
)
|
||||||
|
)
|
||||||
|
return qs
|
||||||
|
|
||||||
def render(self, request, page = None, *args, **kwargs):
|
def render(self, request, page = None, *args, **kwargs):
|
||||||
return ''.join([
|
return ''.join([
|
||||||
place.item.render(request, page, *args, **kwargs)
|
place.item.specific().render(request, page, *args, **kwargs)
|
||||||
for place in self.places
|
for place in self.places.all()
|
||||||
])
|
])
|
||||||
|
|
||||||
|
|
||||||
@ -419,26 +499,42 @@ class SectionPlace(Orderable):
|
|||||||
verbose_name=_('item')
|
verbose_name=_('item')
|
||||||
)
|
)
|
||||||
|
|
||||||
panels = [
|
panels = [ SnippetChooserPanel('item'), ]
|
||||||
SnippetChooserPanel('item'),
|
|
||||||
FieldPanel('model'),
|
|
||||||
]
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return '{}: {}'.format(self.__class__.__name__, self.title or self.pk)
|
return '{}: {}'.format(self.__class__.__name__, self.title or self.pk)
|
||||||
|
|
||||||
|
|
||||||
class SectionItemMeta(models.base.ModelBase):
|
class SectionItemMeta(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):
|
def __new__(cls, name, bases, attrs):
|
||||||
cl = super().__new__(name, bases, attrs)
|
from django.template.loader import get_template
|
||||||
|
from django.template import TemplateDoesNotExist
|
||||||
|
|
||||||
|
cl = super().__new__(cls, name, bases, attrs)
|
||||||
if not 'template' in attrs:
|
if not 'template' in attrs:
|
||||||
attrs['template'] = 'cms/sections/{}.html' \
|
cl.template = '{}/sections/{}.html'.format(
|
||||||
.format(camelcase_to_underscore(name))
|
cl._meta.app_label,
|
||||||
|
camelcase_to_underscore(name),
|
||||||
|
)
|
||||||
|
if name != 'SectionItem':
|
||||||
|
try:
|
||||||
|
get_template(cl.template)
|
||||||
|
except TemplateDoesNotExist:
|
||||||
|
cl.template = 'cms/sections/section_item.html'
|
||||||
return cl
|
return cl
|
||||||
|
|
||||||
|
|
||||||
@register_snippet
|
@register_snippet
|
||||||
class SectionItem(models.Model):
|
class SectionItem(models.Model,metaclass=SectionItemMeta):
|
||||||
|
"""
|
||||||
|
Base class for a section item.
|
||||||
|
"""
|
||||||
real_type = models.CharField(
|
real_type = models.CharField(
|
||||||
max_length=32,
|
max_length=32,
|
||||||
blank = True, null = True,
|
blank = True, null = True,
|
||||||
@ -453,8 +549,8 @@ class SectionItem(models.Model):
|
|||||||
default = False,
|
default = False,
|
||||||
help_text=_('if set show a title at the head of the section'),
|
help_text=_('if set show a title at the head of the section'),
|
||||||
)
|
)
|
||||||
related = models.BooleanField(
|
is_related = models.BooleanField(
|
||||||
_('related'),
|
_('is related'),
|
||||||
default = False,
|
default = False,
|
||||||
help_text=_('if set, section is related to the current publication. '
|
help_text=_('if set, section is related to the current publication. '
|
||||||
'e.g rendering a list related to it instead of to all '
|
'e.g rendering a list related to it instead of to all '
|
||||||
@ -492,17 +588,34 @@ class SectionItem(models.Model):
|
|||||||
return super().save(*args, **kwargs)
|
return super().save(*args, **kwargs)
|
||||||
|
|
||||||
def get_context(self, request, page, *args, **kwargs):
|
def get_context(self, request, page, *args, **kwargs):
|
||||||
|
"""
|
||||||
|
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
|
||||||
|
"""
|
||||||
return {
|
return {
|
||||||
'self': self,
|
'self': self,
|
||||||
'page': page,
|
'page': page,
|
||||||
|
'request': request,
|
||||||
}
|
}
|
||||||
|
|
||||||
def render(self, request, page, *args, **kwargs):
|
def render(self, request, page, *args, **kwargs):
|
||||||
"""
|
"""
|
||||||
Render the section. Page is the current publication being rendered.
|
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.
|
||||||
"""
|
"""
|
||||||
return render_to_string(
|
return render_to_string(
|
||||||
request, self.template,
|
self.template,
|
||||||
self.get_context(request, *args, page=page, **kwargs)
|
self.get_context(request, *args, page=page, **kwargs)
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -520,22 +633,78 @@ class SectionText(SectionItem):
|
|||||||
FieldPanel('body'),
|
FieldPanel('body'),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
def get_context(self, request, page, *args, **kwargs):
|
||||||
|
from wagtail.wagtailcore.rich_text import expand_db_html
|
||||||
|
context = super().get_context(request, page, *args, **kwargs)
|
||||||
|
context['content'] = expand_db_html(self.body)
|
||||||
|
return context
|
||||||
|
|
||||||
@register_snippet
|
@register_snippet
|
||||||
class SectionImage(SectionItem):
|
class SectionImage(SectionItem):
|
||||||
|
class ResizeMode(IntEnum):
|
||||||
|
max = 0x00
|
||||||
|
min = 0x01
|
||||||
|
crop = 0x02
|
||||||
|
|
||||||
image = models.ForeignKey(
|
image = models.ForeignKey(
|
||||||
'wagtailimages.Image',
|
'wagtailimages.Image',
|
||||||
verbose_name = _('image'),
|
verbose_name = _('image'),
|
||||||
related_name='+',
|
related_name='+',
|
||||||
)
|
)
|
||||||
|
width = models.SmallIntegerField(
|
||||||
|
_('width'),
|
||||||
|
blank=True, null=True,
|
||||||
|
help_text=_('if set and > 0, set a maximum width for the image'),
|
||||||
|
)
|
||||||
|
height = models.SmallIntegerField(
|
||||||
|
_('height'),
|
||||||
|
blank=True, null=True,
|
||||||
|
help_text=_('if set 0 and > 0, set a maximum height for the image'),
|
||||||
|
)
|
||||||
|
resize_mode = models.SmallIntegerField(
|
||||||
|
verbose_name = _('resize mode'),
|
||||||
|
choices = [ (int(y), _(x)) for x,y in ResizeMode.__members__.items() ],
|
||||||
|
default = int(ResizeMode.max),
|
||||||
|
help_text=_('if the image is resized, set the resizing mode'),
|
||||||
|
)
|
||||||
|
|
||||||
panels = SectionItem.panels + [
|
panels = SectionItem.panels + [
|
||||||
ImageChooserPanel('image'),
|
ImageChooserPanel('image'),
|
||||||
|
MultiFieldPanel([
|
||||||
|
FieldPanel('width'),
|
||||||
|
FieldPanel('height'),
|
||||||
|
FieldPanel('resize_mode'),
|
||||||
|
], heading=_('Resizing'))
|
||||||
]
|
]
|
||||||
|
|
||||||
|
def get_context(self, request, page, *args, **kwargs):
|
||||||
|
context = super().get_context(request, page, *args, **kwargs)
|
||||||
|
|
||||||
|
image = self.image
|
||||||
|
if self.width or self.height:
|
||||||
|
filter_spec = \
|
||||||
|
'width-{}'.format(self.width) if not self.height else \
|
||||||
|
'height-{}'.format(self.height) if not self.width else \
|
||||||
|
'{}-{}x{}'.format(
|
||||||
|
self.get_resize_mode_display(),
|
||||||
|
self.width, self.height
|
||||||
|
)
|
||||||
|
filter_spec = (image.id, filter_spec)
|
||||||
|
url = reverse(
|
||||||
|
'wagtailimages_serve',
|
||||||
|
args=(generate_signature(*filter_spec), *filter_spec)
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
url = image.file.url
|
||||||
|
|
||||||
|
context['content'] = '<img src="{}"/>'.format(url)
|
||||||
|
return context
|
||||||
|
|
||||||
|
|
||||||
@register_snippet
|
@register_snippet
|
||||||
class SectionLink(RelatedLinkBase,SectionItem):
|
class SectionLink(RelatedLinkBase, SectionItem):
|
||||||
"""
|
"""
|
||||||
|
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
|
||||||
"""
|
"""
|
||||||
parent = ParentalKey('SectionLinkList', related_name='links',
|
parent = ParentalKey('SectionLinkList', related_name='links',
|
||||||
@ -544,21 +713,83 @@ class SectionLink(RelatedLinkBase,SectionItem):
|
|||||||
|
|
||||||
|
|
||||||
@register_snippet
|
@register_snippet
|
||||||
class SectionLinkList(SectionItem,ClusterableModel):
|
class SectionLinkList(SectionItem, ClusterableModel):
|
||||||
|
"""
|
||||||
|
Render a list of links. If related to the current page, print
|
||||||
|
the page's links otherwise, the assigned link list.
|
||||||
|
|
||||||
|
Note: assign the link's class to the <a> tag if there is some.
|
||||||
|
"""
|
||||||
panels = SectionItem.panels + [
|
panels = SectionItem.panels + [
|
||||||
InlinePanel('links', label=_('links'))
|
InlinePanel('links', label=_('links'))
|
||||||
]
|
]
|
||||||
|
|
||||||
|
def get_context(self, request, page, *args, **kwargs):
|
||||||
|
context = super().get_context(*args, **kwargs)
|
||||||
|
links = \
|
||||||
|
self.links if not self.is_related else \
|
||||||
|
page.related_links if hasattr(page, 'related_links') else \
|
||||||
|
None
|
||||||
|
context['object_list'] = links
|
||||||
|
return context
|
||||||
|
|
||||||
|
|
||||||
@register_snippet
|
@register_snippet
|
||||||
class SectionPublicationList(ListBase,SectionItem):
|
class SectionList(ListBase, SectionItem):
|
||||||
|
"""
|
||||||
|
This one is quite badass, but needed: render a list of pages
|
||||||
|
using given parameters (cf. ListBase).
|
||||||
|
|
||||||
|
If focus_available, the first article in the list will be the last
|
||||||
|
article with a focus, and will be rendered in a bigger size.
|
||||||
|
"""
|
||||||
focus_available = models.BooleanField(
|
focus_available = models.BooleanField(
|
||||||
_('focus available'),
|
_('focus available'),
|
||||||
default = False,
|
default = False,
|
||||||
help_text = _('if true, highlight the first focused article found')
|
help_text = _('if true, highlight the first focused article found')
|
||||||
)
|
)
|
||||||
panels = SectionItem.panels + [ FieldPanel('focus_available') ] +\
|
count = models.SmallIntegerField(
|
||||||
ListBase.panels
|
_('count'),
|
||||||
|
default = 5,
|
||||||
|
help_text = _('number of items to display in the list'),
|
||||||
|
)
|
||||||
|
url_text = models.CharField(
|
||||||
|
_('text of the url'),
|
||||||
|
max_length=32,
|
||||||
|
blank = True, null = True,
|
||||||
|
help_text = _('use this text to display an URL to the complete '
|
||||||
|
'list. If empty, does not print an address'),
|
||||||
|
)
|
||||||
|
|
||||||
|
panels = SectionItem.panels + [
|
||||||
|
MultiFieldPanel([
|
||||||
|
FieldPanel('focus_available'),
|
||||||
|
FieldPanel('count'),
|
||||||
|
FieldPanel('url_text'),
|
||||||
|
], heading=_('Rendering')),
|
||||||
|
] + ListBase.panels
|
||||||
|
|
||||||
|
def get_context(self, request, page, *args, **kwargs):
|
||||||
|
from aircox.cms.models import Publication
|
||||||
|
context = super().get_context(request, page, *args, **kwargs)
|
||||||
|
|
||||||
|
qs = self.get_queryset()
|
||||||
|
if self.focus_available:
|
||||||
|
focus = qs.type(Publication).filter(focus = True).first()
|
||||||
|
if focus:
|
||||||
|
focus.css_class = \
|
||||||
|
focus.css_class + ' focus' if focus.css_class else 'focus'
|
||||||
|
qs = qs.exclude(focus.page)
|
||||||
|
else:
|
||||||
|
focus = None
|
||||||
|
pages = qs[:self.count - (focus != None)]
|
||||||
|
|
||||||
|
context['focus'] = focus
|
||||||
|
context['object_list'] = pages
|
||||||
|
if self.url_text:
|
||||||
|
context['url'] = self.to_url(
|
||||||
|
list_page = self.is_related and page
|
||||||
|
)
|
||||||
|
|
||||||
|
return context
|
||||||
|
|
||||||
|
@ -1,7 +1,11 @@
|
|||||||
{% load staticfiles %}
|
{% load staticfiles %}
|
||||||
{% load i18n %}
|
{% load i18n %}
|
||||||
|
|
||||||
{% load wagtailimages_tags %}
|
{% load wagtailimages_tags %}
|
||||||
{% load wagtailsettings_tags %}
|
{% load wagtailsettings_tags %}
|
||||||
|
|
||||||
|
{% load aircox_cms %}
|
||||||
|
|
||||||
{% get_settings %}
|
{% get_settings %}
|
||||||
|
|
||||||
<html>
|
<html>
|
||||||
@ -26,7 +30,7 @@
|
|||||||
|
|
||||||
<div class="middle">
|
<div class="middle">
|
||||||
<nav class="left">
|
<nav class="left">
|
||||||
<img src="{{ settings.cms.WebsiteSettings.logo.file.url }}" class="logo">
|
{% render_sections position="menu_left" %}
|
||||||
</nav>
|
</nav>
|
||||||
|
|
||||||
<main>
|
<main>
|
||||||
|
8
cms/templates/cms/sections/section_item.html
Normal file
8
cms/templates/cms/sections/section_item.html
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<div class="section_item {{ self.css_class }}">
|
||||||
|
{% block title %}
|
||||||
|
{% if self.show_title %}<h2>{{ self.title }}</h2>{% endif %}
|
||||||
|
{% endblock %}
|
||||||
|
{% block content %}{{ content|safe }}{% endblock %}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
13
cms/templates/cms/sections/section_link.html
Normal file
13
cms/templates/cms/sections/section_link.html
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
{% extends "cms/sections/section_item.html" %}
|
||||||
|
{% load wagtailimages_tags %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
{% with link=self.as_dict %}
|
||||||
|
<a href="{{ link.url }}">
|
||||||
|
{% if link.icon %}{% image link.icon fill-32x32 class="icon" %}{% endif %}
|
||||||
|
{{ link.text }}
|
||||||
|
</a>
|
||||||
|
{% endwith %}
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
|
16
cms/templates/cms/sections/section_link_list.html
Normal file
16
cms/templates/cms/sections/section_link_list.html
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
{% extends "cms/sections/section_item.html" %}
|
||||||
|
{% load wagtailimages_tags %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
{% for item in object_list %}
|
||||||
|
{% with link=item.as_dict %}
|
||||||
|
<a href="{{ link.url }}"
|
||||||
|
{% if item.css_class %}class="{{ item.css_class }}"{% endif %}>
|
||||||
|
{% if link.icon %}{% image link.icon fill-32x32 class="icon" %}{% endif %}
|
||||||
|
{{ link.text }}
|
||||||
|
</a>
|
||||||
|
{% endwith %}
|
||||||
|
{% endfor %}
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
|
17
cms/templates/cms/sections/section_list.html
Normal file
17
cms/templates/cms/sections/section_list.html
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
{% extends "cms/sections/section_item.html" %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
{% if focus %}
|
||||||
|
{% with item=focus item_big_cover=True %}
|
||||||
|
{% include "cms/snippets/list_item.html" %}
|
||||||
|
{% endwith %}
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
{% include "cms/snippets/list.html" %}
|
||||||
|
|
||||||
|
{% if url %}
|
||||||
|
<nav><a href="{{ url }}">{{ self.url_text }}</nav>
|
||||||
|
{% endif %}
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
|
@ -26,7 +26,7 @@
|
|||||||
{% if day == nav_dates.date %}class="today"{% endif %}>
|
{% if day == nav_dates.date %}class="today"{% endif %}>
|
||||||
{# you might like to hide it by default -- this more for sections #}
|
{# you might like to hide it by default -- this more for sections #}
|
||||||
<h2>{{ day|date:'l d F' }}</h2>
|
<h2>{{ day|date:'l d F' }}</h2>
|
||||||
{% with object_list=list list_date_format="H:i" %}
|
{% with object_list=list item_date_format="H:i" %}
|
||||||
{% for item in list %}
|
{% for item in list %}
|
||||||
{% include "cms/snippets/list_item.html" %}
|
{% include "cms/snippets/list_item.html" %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
@ -4,14 +4,19 @@ ListItem instance.
|
|||||||
|
|
||||||
Options:
|
Options:
|
||||||
* item: item to render. Fields: title, summary, cover, url, date, info, css_class
|
* item: item to render. Fields: title, summary, cover, url, date, info, css_class
|
||||||
* list_date_format: format passed to the date filter instead of default one
|
* item_date_format: format passed to the date filter instead of default one
|
||||||
|
* item_big_cover: cover should is big instead of thumbnail (width: 600)
|
||||||
{% endcomment %}
|
{% endcomment %}
|
||||||
|
|
||||||
{% load wagtailimages_tags %}
|
{% load wagtailimages_tags %}
|
||||||
|
|
||||||
<a {% if item.url %}href="{{ item.url }}" {% endif %}
|
<a {% if item.url %}href="{{ item.url }}" {% endif %}
|
||||||
class="item page_item{% if item.css_class %}{{ item.css_class }}{% endif %}">
|
class="item page_item{% if item.css_class %}{{ item.css_class }}{% endif %}">
|
||||||
{% image item.cover fill-64x64 class="cover item_cover" %}
|
{% if item_big_cover %}
|
||||||
|
{% image item.cover max-600 class="cover item_cover" %}
|
||||||
|
{% else %}
|
||||||
|
{% image item.cover fill-64x64 class="cover item_cover" %}
|
||||||
|
{% endif %}
|
||||||
<h3>{{ item.title }}</h3>
|
<h3>{{ item.title }}</h3>
|
||||||
{% if item.summary %}<div class="summary">{{ item.summary }}</div>{% endif %}
|
{% if item.summary %}<div class="summary">{{ item.summary }}</div>{% endif %}
|
||||||
{% if not item.show_in_menus and item.date %}
|
{% if not item.show_in_menus and item.date %}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
from django import template
|
from django import template
|
||||||
from django.db.models import Q
|
from django.utils.safestring import mark_safe
|
||||||
|
|
||||||
import aircox.cms.sections as sections
|
from aircox.cms.sections import Section
|
||||||
|
|
||||||
register = template.Library()
|
register = template.Library()
|
||||||
|
|
||||||
@ -13,21 +13,15 @@ def around(page_num, n):
|
|||||||
return range(page_num-n, page_num+n+1)
|
return range(page_num-n, page_num+n+1)
|
||||||
|
|
||||||
@register.simple_tag(takes_context=True)
|
@register.simple_tag(takes_context=True)
|
||||||
def render_section(position = None, section = None, context = None):
|
def render_sections(context, position = None):
|
||||||
"""
|
"""
|
||||||
If section is not given, render all sections at the given
|
Render all sections at the given position (filter out base on page
|
||||||
position (filter out base on page models' too, cf. Section.model).
|
models' too, cf. Section.model).
|
||||||
"""
|
"""
|
||||||
request = context.get('request')
|
request = context.get('request')
|
||||||
page = context.get('page')
|
page = context.get('page')
|
||||||
|
return mark_safe(''.join(
|
||||||
if section:
|
section.render(request, page=page)
|
||||||
return section.render(request, page=page)
|
for section in Section.get_sections_at(position, page)
|
||||||
|
))
|
||||||
sections = Section.objects \
|
|
||||||
.filter( Q(model__isnull = True) | Q(model = type(page)) ) \
|
|
||||||
.filter(position = position)
|
|
||||||
return '\n'.join(
|
|
||||||
section.render(request, page=page) for section in sections
|
|
||||||
)
|
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ sources that are used to generate the audio stream:
|
|||||||
import os
|
import os
|
||||||
import datetime
|
import datetime
|
||||||
import logging
|
import logging
|
||||||
from enum import Enum, IntEnum
|
from enum import IntEnum
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.contrib.contenttypes.fields import GenericForeignKey
|
from django.contrib.contenttypes.fields import GenericForeignKey
|
||||||
|
13
notes.md
13
notes.md
@ -1,3 +1,16 @@
|
|||||||
|
This file is used as a reminder, can be used as crappy documentation too.
|
||||||
|
|
||||||
|
# conventions
|
||||||
|
## coding style
|
||||||
|
* name of classes relative to a class:
|
||||||
|
- metaclass: `class_name + 'Meta'`
|
||||||
|
- base classes: `class_name + 'Base'`
|
||||||
|
|
||||||
|
## aircox.cms
|
||||||
|
* icons: cropped to 32x32
|
||||||
|
* cover in list items: cropped 64x64
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# TODO:
|
# TODO:
|
||||||
- general:
|
- general:
|
||||||
|
@ -2,7 +2,7 @@ import datetime
|
|||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
import logging
|
import logging
|
||||||
from enum import Enum, IntEnum
|
from enum import IntEnum
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.template.defaultfilters import slugify
|
from django.template.defaultfilters import slugify
|
||||||
|
Reference in New Issue
Block a user