fix bug for schedules set to "one week or two"; sections.List: avoid to call self.get_object_list if possible + add prepare_object_list

This commit is contained in:
bkfox
2016-06-20 15:43:23 +02:00
parent a3a9beac6d
commit b195dd74a0
6 changed files with 76 additions and 21 deletions

View File

@ -238,7 +238,6 @@ class List(Section):
* truncate: number of words to keep in content (0 = full content)
"""
template_name = 'aircox/cms/list.html'
base_template = 'aircox/cms/section.html'
object_list = None
url = None
@ -265,19 +264,45 @@ class List(Section):
def get_object_list(self):
return self.object_list
def get_context_data(self, request, object=None, *args, **kwargs):
def prepare_object_list(self, object_list):
"""
Prepare objects before context is sent to the template renderer.
Return the object_list that is prepared.
Remember: since we are in a rendering process, the items should
not be saved.
"""
return object_list
def get_context_data(self, request, object=None, object_list=None,
*args, **kwargs):
"""
Return a context that is passed to the template at rendering, with
the following values:
- `list`: a reference to self, that contain values used for rendering
- `object_list`: a list of object that can be rendered, either
instances of Post or ListItem.
If object_list is not given, call `get_object_list` to retrieve it.
Prepare the object_list using `self.prepare_object_list`.
Set `request`, `object`, `object_list` and `kwargs` in self.
"""
if request: self.request = request
if object: self.object = object
if kwargs: self.kwargs = kwargs
object_list = self.object_list or self.get_object_list()
if not object_list and not self.message_empty:
return
if object_list is None:
object_list = self.object_list or self.get_object_list()
if not object_list and not self.message_empty:
return
self.object_list = object_list
if object_list:
object_list = self.prepare_object_list(object_list)
context = super().get_context_data(request, object, *args, **kwargs)
context.update({
'base_template': self.base_template,
'list': self,
'object_list': object_list[:self.paginate_by]
if object_list and self.paginate_by else

View File

@ -42,7 +42,7 @@ class BaseView:
self.sections = sections
super().__init__(*args, **kwargs)
def __is_single(self):
def __section_is_single(self):
return not issubclass(type(self.sections), list)
def add_css_class(self, css_class):
@ -64,10 +64,13 @@ class BaseView:
# update from sections
if self.sections:
if self.__is_single():
if self.__section_is_single():
self.template_name = self.sections.template_name
context.update(self.sections.get_context_data(
self.request, **self.kwargs
self.request,
object_list = hasattr(self, 'object_list') and \
self.object_list,
**self.kwargs
) or {})
else:
if not self.template_name:
@ -143,6 +146,7 @@ class PostListView(BaseView, ListView):
return super().dispatch(request, *args, **kwargs)
def get_queryset(self):
print('get_query_set')
if self.route:
qs = self.route.get_queryset(self.model, self.request,
**self.kwargs)
@ -183,13 +187,13 @@ class PostListView(BaseView, ListView):
self.add_css_class('list')
context = super().get_context_data(**kwargs)
# context.update(BaseView.get_context_data(self, **kwargs))
if not context.get('title') and self.route:
context['title'] = self.route.get_title(
self.model, self.request, **self.kwargs
)
print([post.title for post in context.get('object_list')])
context['list'] = self.list
return context