work on schedule; section.as_view()

This commit is contained in:
bkfox
2016-06-07 01:57:06 +02:00
parent 2365cc8b8e
commit b99dec05e3
8 changed files with 184 additions and 120 deletions

View File

@ -103,6 +103,9 @@ class Post (models.Model, Routable):
"""
Base model that can be used as is if wanted. Represent a generic
publication on the website.
You can declare an extra property "info" that can be used to append
info in lists rendering.
"""
# metadata
thread_type = models.ForeignKey(

View File

@ -35,8 +35,6 @@ class Section(View):
* name: set name/id of the section container
* css_class: css classes of the container
* attr: HTML attributes of the container
* hide_empty: if true, section is not rendered when content is empty
* title: title of the section
* header: header of the section
* footer: footer of the section
@ -50,13 +48,28 @@ class Section(View):
name = ''
css_class = ''
attrs = None
# hide_empty = False
title = ''
header = ''
footer = ''
object = None
force_object = None
request = None
object = None
kwargs = None
@classmethod
def as_view (cl, *args, **kwargs):
"""
Similar to View.as_view, but instead, wrap a constructor of the
given class that is used as is.
"""
def func(**kwargs_):
if kwargs_:
kwargs.update(kwargs_)
instance = cl(*args, **kwargs)
return instance
return func
def add_css_class(self, css_class):
if self.css_class:
if css_class not in self.css_class:
@ -80,7 +93,11 @@ class Section(View):
def get_content(self):
return ''
def get_context_data(self):
def get_context_data(self, request = None, object = None, **kwargs):
if request: self.request = request
if object: self.object = object
if kwargs: self.kwargs = kwargs
return {
'view': self,
'tag': self.tag,
@ -93,14 +110,10 @@ class Section(View):
'object': self.object,
}
def get_context(self, request, object=None, **kwargs):
self.object = self.force_object or object
self.request = request
self.kwargs = kwargs
return self.get_context_data()
def get(self, request, object=None, **kwargs):
context = self.get_context(request, object, **kwargs)
def get(self, request, object=None, return_context=False, **kwargs):
context = self.get_context_data(request=request, object=object, **kwargs)
if return_context:
return context
if not context:
return ''
return render_to_string(self.template_name, context, request=request)
@ -131,9 +144,12 @@ class Content(Section):
Attributes:
* content: raw HTML code to render
* rel_attr: name of the attribute of self.object to use
* re_image_attr: if true and there is an image on the current object,
render the object's image
"""
content = None
rel_attr = 'content'
rel_image_attr = 'image'
def get_content(self):
if self.content is None:
@ -142,6 +158,10 @@ class Content(Section):
content = escape(content)
content = re.sub(r'(^|\n\n)((\n?[^\n])+)', r'<p>\2</p>', content)
content = re.sub(r'\n', r'<br>', content)
if self.rel_image_attr and hasattr(self.object, self.rel_image_attr):
image = getattr(self.object, self.rel_image_attr)
content = '<img src="{}">'.format(image.url) + content
return content
return str(self.content)
@ -222,12 +242,13 @@ class List(Section):
def get_object_list(self):
return self.object_list
def get_context_data(self):
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
object_list = self.object_list or self.get_object_list()
if not object_list and not self.message_empty:
return
context = super().get_context_data()
context.update({
'base_template': 'aircox/cms/section.html',
'list': self,
@ -276,14 +297,15 @@ class Comments(List):
})
return ''
def get_context_data(self):
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
comment_form = None
if self.object:
post = self.object
if hasattr(post, 'allow_comments') and post.allow_comments:
comment_form = (self.comment_form or CommentForm())
context = super().get_context_data()
context.update({
'comment_form': comment_form,
})
@ -319,11 +341,15 @@ class Menu(Section):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.css_class += ' menu menu_{}'.format(self.name or self.position)
self.add_css_class('menu')
self.add_css_class('menu_' + str(self.name or self.position))
self.sections = [ section() if callable(section) else section
for section in self.sections ]
if not self.attrs:
self.attrs = {}
def get_context_data(self):
def get_context_data(self, *args, **kwargs):
super().get_context_data(*args, **kwargs)
return {
'tag': self.tag,
'css_class': self.css_class,

View File

@ -7,7 +7,7 @@
{% block content %}
<ul>
<ul class="content">
{% for item in object_list %}
<li {% if item.css_class %}class="{{ item.css_class }}"{% endif %}
{% for k, v in item.attrs.items %}
@ -16,6 +16,25 @@
{% if item.detail_url %}
<a href="{{ item.detail_url }}">
{% endif %}
{% if 'image' in list.fields and item.image %}
<img src="{% thumbnail item.image list.image_size crop %}">
{% endif %}
<div class="content">
{% if 'title' in list.fields and item.title %}
<h2 class="title">{{ item.title }}</h2>
{% endif %}
{% if 'content' in list.fields and item.content %}
<div class="text">
{% if list.truncate %}
{{ item.content|striptags|truncatewords:list.truncate }}
{% else %}
{{ item.content|striptags }}
{% endif %}
</div>
{% endif %}
</div>
<div class="meta">
{% if item.date and 'date' in list.fields or 'time' in list.fields %}
@ -45,25 +64,6 @@
{% endif %}
</div>
{% if 'image' in list.fields and item.image %}
<img src="{% thumbnail item.image list.image_size crop %}">
{% endif %}
<div class="content">
{% if 'title' in list.fields and item.title %}
<h2 class="title">{{ item.title }}</h2>
{% endif %}
{% if 'content' in list.fields and item.content %}
<div class="text">
{% if list.truncate %}
{{ item.content|striptags|truncatewords:list.truncate }}
{% else %}
{{ item.content|striptags }}
{% endif %}
</div>
{% endif %}
</div>
{% if item.detail_url %}
</a>
@ -75,8 +75,8 @@
{% endfor %}
</ul>
{% if object_list %}
{% if page_obj or list.url %}
{% if object_list and not embed %}
{% if list.url or page_obj %}
<nav>
{% if not page_obj or embed %}
{% comment %}link to show more elements of the list{% endcomment %}

View File

@ -72,10 +72,6 @@ class PostListView(PostBaseView, ListView):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if self.list:
self.template_name = self.list.template_name
self.css_class = self.list.css_class
self.add_css_class('list')
def dispatch(self, request, *args, **kwargs):
self.route = self.kwargs.get('route') or self.route
@ -88,9 +84,7 @@ class PostListView(PostBaseView, ListView):
qs = self.route.get_queryset(self.model, self.request,
**self.kwargs)
else:
# FIXME: should neven happen
qs = self.queryset or self.model.objects.all()
qs = qs.filter(published = True)
query = self.request.GET
@ -101,45 +95,47 @@ class PostListView(PostBaseView, ListView):
else:
qs = qs.order_by('date', 'id')
if query.get('fields'):
self.fields = [
field for field in query.get('fields')
if field in self.__class__.fields
]
return qs
def get_context_data(self, **kwargs):
if self.list:
list = self.list
def init_list(self):
if not self.list:
self.list = sections.List(
truncate = 32,
fields = ['date', 'time', 'image', 'title', 'content'],
)
else:
list = sections.List(
truncate = 32,
fields = [ 'date', 'time', 'image', 'title', 'content' ],
)
self.list = self.list()
self.template_name = self.list.template_name
self.css_class = self.list.css_class
context = list.get_context(request = self.request, **self.kwargs) or {}
if self.request.GET.get('fields'):
self.list.fields = [
field for field in self.request.GET.getlist('fields')
if field in self.list.fields
]
def get_context_data(self, **kwargs):
self.init_list()
self.add_css_class('list')
context = self.list.get_context_data(self.request, **self.kwargs) or {}
context.update(super().get_context_data(**kwargs))
context.update(self.get_base_context(**kwargs))
if self.title:
title = self.title
else:
title = self.route and \
self.route.get_title(self.model, self.request,
elif self.route:
title = self.route.get_title(self.model, self.request,
**self.kwargs)
context.update({
'title': title,
'base_template': 'aircox/cms/website.html',
'css_class': self.css_class,
'list': list,
'list': self.list,
})
# FIXME: list.url = if self.route: self.model(self.route, self.kwargs) else ''
return context
def get_url(self):
return ''
class PostDetailView(DetailView, PostBaseView):
"""
@ -156,7 +152,7 @@ class PostDetailView(DetailView, PostBaseView):
def __init__(self, sections = None, *args, **kwargs):
super().__init__(*args, **kwargs)
self.add_css_class('detail')
self.sections = sections or []
self.sections = [ section() for section in (sections or []) ]
def get_queryset(self):
if self.request.GET.get('embed'):