diff --git a/cms/sections.py b/cms/sections.py
index d27bf47..008de08 100644
--- a/cms/sections.py
+++ b/cms/sections.py
@@ -97,6 +97,10 @@ class Section(Viewable, View):
* title: title of the section
* header: header of the section
* footer: footer of the section
+
+ * message_empty: if message_empty is not None, print its value as
+ content of the section instead of hiding it. This works also when
+ its value is an empty string (prints an empty string).
"""
template_name = 'aircox/cms/website.html'
@@ -108,6 +112,8 @@ class Section(Viewable, View):
header = ''
footer = ''
+ message_empty = None
+
request = None
object = None
kwargs = None
@@ -132,8 +138,13 @@ class Section(Viewable, View):
self.attrs['name'] = self.name
self.attrs['id'] = self.name
- def get_content(self):
- return ''
+ def is_empty(self):
+ """
+ Return True if the section content will be empty. This allows to
+ hide the section.
+ This must be implemented by the subclasses.
+ """
+ return False
def get_context_data(self, request = None, object = None, **kwargs):
if request: self.request = request
@@ -149,17 +160,21 @@ class Section(Viewable, View):
'title': self.title,
'header': self.header,
'footer': self.footer,
- 'content': self.get_content(),
+ 'content': '',
'object': self.object,
'embed': True,
}
- def render(self, request, object=None, context_only=False, **kwargs):
+ def render(self, request, object=None, **kwargs):
context = self.get_context_data(request=request, object=object, **kwargs)
- if context_only:
- return context
- if not context:
+
+ is_empty = self.is_empty()
+ if not context or (is_empty and not self.message_empty):
return ''
+
+ if is_empty and self.message_empty:
+ context['content'] = self.message_empty
+
context['embed'] = True
return render_to_string(self.template_name, context, request=request)
@@ -170,45 +185,61 @@ class Image(Section):
Attributes:
* url: relative image url
- * rel_attr: name of the attribute of self.object to use
+ * img_attr: name of the attribute of self.object to use
"""
url = None
- rel_attr = 'image'
+ img_attr = 'image'
- def get_content(self, **kwargs):
- if self.url is None:
- image = getattr(self.object, self.rel_attr)
- return ''.format(image.url) if image else ''
- return '
'.format(static(self.url))
+ def get_image(self):
+ if self.url:
+ return static(self.url)
+ if hasattr(self.object, self.img_attr):
+ image = getattr(self.object, self.img_attr)
+ return (image and image.url) or None
+ def is_empty(self):
+ return not self.get_image()
-class Content(Section):
+ def get_context_data(self, *args, **kwargs):
+ context = super().get_context_data(*args, **kwargs)
+ url = self.get_image()
+ if url:
+ context['content'] += '
'.format(url)
+ return context
+
+class Content(Image):
"""
Render content using the self.content or relative to self.object.
+ Since it is a child of Image, can also render an image.
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_attr: name of the attribute of self.object to use
"""
+ # FIXME: markup language -- coordinate with object's one (post/comment)?
content = None
- rel_attr = 'content'
- rel_image_attr = 'image'
+ content_attr = 'content'
def get_content(self):
- if self.content is None:
- content = getattr(self.object, self.rel_attr)
- content = escape(content)
- content = re.sub(r'(^|\n\n)((\n?[^\n])+)', r'
\2
', content) - content = re.sub(r'\n', r'\2
', content) + content = re.sub(r'\n', r'