From 27a70ed76d30f6a88ca41cabcbf9b45d32061e65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D1=82=D0=BE=D0=BD=20=D0=9A=D0=B0=D1=81=D0=B8?= =?UTF-8?q?=D0=BC=D0=BE=D0=B2?= Date: Sun, 24 Feb 2019 03:16:38 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=BD=D0=BE=D1=81=20re?= =?UTF-8?q?direct=20=D0=B8=20content=5Ftype=20=D0=B2=20view=20[closes=20#2?= =?UTF-8?q?3].=20=D0=9C=D0=BE=D0=B6=D0=B5=D1=82=20=D0=B1=D1=8B=D1=82=D1=8C?= =?UTF-8?q?=20=D0=BF=D0=BE=D0=BB=D0=B5=D0=B7=D0=BD=D0=BE=20=D0=B4=D0=BB?= =?UTF-8?q?=D1=8F=20[#11]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- flatfilecms/views/__init__.py | 9 +++++++++ flatfilecms/views/pages.py | 26 +++++++++++++++++--------- tests/pages/view-with-options.md | 9 +++++++++ tests/test_pages.py | 6 ++++++ tests/views.py | 10 ++++++++-- 5 files changed, 49 insertions(+), 11 deletions(-) create mode 100644 tests/pages/view-with-options.md diff --git a/flatfilecms/views/__init__.py b/flatfilecms/views/__init__.py index e69de29..faec57d 100644 --- a/flatfilecms/views/__init__.py +++ b/flatfilecms/views/__init__.py @@ -0,0 +1,9 @@ +from pyramid.httpexceptions import HTTPFound + + +def redirect(self, url): + return HTTPFound(location=url) + + +def set_content_type(self, content_type): + self.request.response.content_type = content_type diff --git a/flatfilecms/views/pages.py b/flatfilecms/views/pages.py index 1064722..c4daa03 100644 --- a/flatfilecms/views/pages.py +++ b/flatfilecms/views/pages.py @@ -39,17 +39,26 @@ class PagesView: if 'view' in self.context.page: r = DottedNameResolver() if isinstance(self.context.page['view'], str): - r.resolve(self.context.page['view'])(self) + response = r.resolve(self.context.page['view'])(self) + if response: + return response + elif isinstance(self.context.page['view'], dict): + for name, options in self.context.page['view'].items(): + response = r.resolve(name)(self, options) + if response: + return response else: for view in self.context.page['view']: if isinstance(view, str): - r.resolve(view)(self) + response = r.resolve(view)(self) + if response: + return response else: - for name, kw in view.items(): - r.resolve(name)(self, **kw) + for name, options in view.items(): + response = r.resolve(name)(self, options) + if response: + return response post = self.context.page - if 'redirect' in post: - return HTTPFound(location=post['redirect']) if 'menu' not in post: post['menu'] = yaml.load( AssetResolver().resolve( @@ -60,10 +69,9 @@ class PagesView: '{0}.jinja2'.format( post.get('template', 'default')), post, - request=self.request + request=self.request, + response=self.request.response ) - if 'content_type' in post: - response.content_type = post['content_type'] return response @view_config(context=Jinja2) diff --git a/tests/pages/view-with-options.md b/tests/pages/view-with-options.md new file mode 100644 index 0000000..552a509 --- /dev/null +++ b/tests/pages/view-with-options.md @@ -0,0 +1,9 @@ +--- +title: Тест одиночного view c опциями +description: Тесты view +template: tests:templates/test_markdown +view: + tests.views.test4: a +--- + +Hello World! diff --git a/tests/test_pages.py b/tests/test_pages.py index f69a87c..708ea21 100644 --- a/tests/test_pages.py +++ b/tests/test_pages.py @@ -49,6 +49,12 @@ def test_view(pages, fake_request, config): assert view.process_yaml().text == 'Hello World! View1' +def test_view_with_options(pages, fake_request, config): + from flatfilecms.views.pages import PagesView + view = PagesView(pages['view-with-options'], fake_request) + assert view.process_yaml().text == 'Hello World! View4(a)' + + def test_views(pages, fake_request, config): from flatfilecms.views.pages import PagesView view = PagesView(pages['views'], fake_request) diff --git a/tests/views.py b/tests/views.py index e08a7d4..0ee34c9 100644 --- a/tests/views.py +++ b/tests/views.py @@ -6,5 +6,11 @@ def test2(self): self.context.page['content'] += ' View2' -def test3(self, **kwargs): - self.context.page['content'] += f" View3(a:{kwargs['a']}, b:{kwargs['b']})" +def test3(self, options): + self.context.page['content'] += \ + f" View3(a:{options['a']}, b:{options['b']})" + + +def test4(self, options): + self.context.page['content'] += \ + f" View4({options})"