From 7c89f84b34532e0342340e8f8f67b3216401eac9 Mon Sep 17 00:00:00 2001 From: bkfox Date: Thu, 26 May 2016 22:12:29 +0200 Subject: [PATCH] make menu work --- cms/sections.py | 49 +++++++++++++++++++++++---- cms/templates/aircox/cms/section.html | 2 +- cms/views.py | 38 --------------------- 3 files changed, 43 insertions(+), 46 deletions(-) diff --git a/cms/sections.py b/cms/sections.py index a102406..9dc82e5 100644 --- a/cms/sections.py +++ b/cms/sections.py @@ -29,6 +29,7 @@ class Section(View): Attributes: * template_name: template to use for rendering * tag: container's tags + * 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 @@ -41,12 +42,13 @@ class Section(View): * object: (can be persistent) related object """ - template_name = 'aircox/cms/content_object.html' + template_name = 'aircox/cms/section.html' tag = 'div' + name = '' css_class = '' - attrs = '' - hide_empty = False + attrs = None + # hide_empty = False title = '' header = '' footer = '' @@ -54,7 +56,12 @@ class Section(View): def __init__ (self, *args, **kwargs): super().__init__(*args, **kwargs) - self.css_class += ' section' + self.css_class = 'section' if not self.css_class else ' section' + if not self.attrs: + self.attrs = {} + if self.name: + self.attrs['name'] = self.name + self.attrs['id'] = self.name def get_content(self): return '' @@ -75,13 +82,10 @@ class Section(View): def get(self, request, object=None, **kwargs): if not self.object: self.object = object - self.request = request self.kwargs = kwargs context = self.get_context_data() - # if not context['content'] and self.hide_empty: - # return '' return render_to_string(self.template_name, context, request=request) @@ -238,3 +242,34 @@ class Comments(List): }) return context + +class Menu(Section): + template_name = 'aircox/cms/section.html' + tag = 'nav' + classes = '' + attrs = '' + name = '' + enabled = True + position = '' # top, left, bottom, right, header, footer, page_top, page_bottom + sections = None + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.css_class += ' menu menu_{}'.format(self.name or self.position) + if not self.attrs: + self.attrs = {} + + def get_context_data(self): + return { + 'tag': self.tag, + 'css_class': self.css_class, + 'attrs': self.attrs, + 'content': ''.join([ + section.get(request=self.request, object=self.object) + for section in self.sections + ]) + } + + + + diff --git a/cms/templates/aircox/cms/section.html b/cms/templates/aircox/cms/section.html index 59e3b78..3303ead 100644 --- a/cms/templates/aircox/cms/section.html +++ b/cms/templates/aircox/cms/section.html @@ -1,6 +1,6 @@ <{{ tag }} class="{{ css_class }}" - {% for k, v in list.attrs.items %} + {% for k, v in attrs.items %} {{ k }} = "{{ v|addslashes }}" {% endfor %} > diff --git a/cms/views.py b/cms/views.py index ccf92f2..9876f9b 100644 --- a/cms/views.py +++ b/cms/views.py @@ -176,41 +176,3 @@ class PostDetailView(DetailView, PostBaseView): return self.get(request, *args, **kwargs) -class Menu(View): - template_name = 'aircox/cms/section.html' - tag = 'nav' - classes = '' - attrs = '' - name = '' - enabled = True - position = '' # top, left, bottom, right, header, footer, page_top, page_bottom - sections = None - - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - self.classes += ' menu menu_{}'.format(self.name or self.position) - if not self.attrs: - self.attrs = {} - if self.name: - self.attrs['name'] = self.name - self.attrs['id'] = self.name - - def get_context_data(self, request, object = None, **kwargs): - kwargs['object'] = object - - return { - 'tag': self.tag, - 'classes': self.classes, - 'attrs': self.attrs, - 'content': ''.join([ - section.get(request, **kwargs) - for section in self.sections - ]) - } - - def get(self, request, object = None, **kwargs): - self.request = request - context = self.get_context_data(request, object, **kwargs) - return render_to_string(self.template_name, context) - -