From 6246373b1a09e031cc1506e9ca2b759152ac68bf Mon Sep 17 00:00:00 2001 From: "Karl O. Pinc" Date: Sun, 15 Nov 2020 17:08:40 -0600 Subject: [PATCH] Move code out of __init__.py --- src/pgwui_common/__init__.py | 54 +------------- src/pgwui_common/pgwui_common.py | 74 +++++++++++++++++++ ...{test___init__.py => test_pgwui_common.py} | 24 +++--- 3 files changed, 87 insertions(+), 65 deletions(-) create mode 100644 src/pgwui_common/pgwui_common.py rename tests/{test___init__.py => test_pgwui_common.py} (84%) diff --git a/src/pgwui_common/__init__.py b/src/pgwui_common/__init__.py index 9508b90..1d3c7b3 100644 --- a/src/pgwui_common/__init__.py +++ b/src/pgwui_common/__init__.py @@ -19,56 +19,4 @@ # Karl O. Pinc -'''Provide a way to configure PGWUI. -''' - -DEFAULT_HOME_ROUTE = '/' - - -def base_view(wrapped): - '''Decorator for any view which includes base.mk. - ''' - def wrapper(request): - '''Add variables missing but needed by base.mk to the response. - ''' - response = wrapped(request) - pgwui = response.get('pgwui', {}) - url = pgwui.setdefault('url', dict()) - url.setdefault('css', - request.static_url('pgwui_common:static/pgwui.css')) - route = pgwui.setdefault('route', dict()) - route.setdefault('home', - request.route_url('home')) - response['pgwui'] = pgwui - return response - return wrapper - - -def auth_base_view(wrapped): - '''Decorator for any view which includes auth_base.mk. - ''' - def wrapper(request): - '''Add variables needed by auth_base.mk to the response. - ''' - response = base_view(wrapped)(request) - pgwui = response['pgwui'] - try: - logout_route = request.route_url('pgwui_logout') - except KeyError: - pass # A logout route is not required - else: - pgwui['route'].setdefault('pgwui_logout', logout_route) - return response - return wrapper - - -def includeme(config): - '''Pyramid configuration for PGWUI_Common - ''' - config.include('pyramid_mako') - config.include('pyramid_beaker') - config.add_static_view( - 'static', - 'pgwui_common:static/', - cache_max_age=3600) - config.add_route('home', DEFAULT_HOME_ROUTE) +from .pgwui_common import includeme # noqa: F401 diff --git a/src/pgwui_common/pgwui_common.py b/src/pgwui_common/pgwui_common.py new file mode 100644 index 0000000..9508b90 --- /dev/null +++ b/src/pgwui_common/pgwui_common.py @@ -0,0 +1,74 @@ +# Copyright (C) 2018, 2020 The Meme Factory, Inc. http://www.karlpinc.com/ + +# This file is part of PGWUI_Common. +# +# This program is free software: you can redistribute it and/or +# modify it under the terms of the GNU Affero General Public License +# as published by the Free Software Foundation, either version 3 of +# the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public +# License along with this program. If not, see +# . +# + +# Karl O. Pinc + +'''Provide a way to configure PGWUI. +''' + +DEFAULT_HOME_ROUTE = '/' + + +def base_view(wrapped): + '''Decorator for any view which includes base.mk. + ''' + def wrapper(request): + '''Add variables missing but needed by base.mk to the response. + ''' + response = wrapped(request) + pgwui = response.get('pgwui', {}) + url = pgwui.setdefault('url', dict()) + url.setdefault('css', + request.static_url('pgwui_common:static/pgwui.css')) + route = pgwui.setdefault('route', dict()) + route.setdefault('home', + request.route_url('home')) + response['pgwui'] = pgwui + return response + return wrapper + + +def auth_base_view(wrapped): + '''Decorator for any view which includes auth_base.mk. + ''' + def wrapper(request): + '''Add variables needed by auth_base.mk to the response. + ''' + response = base_view(wrapped)(request) + pgwui = response['pgwui'] + try: + logout_route = request.route_url('pgwui_logout') + except KeyError: + pass # A logout route is not required + else: + pgwui['route'].setdefault('pgwui_logout', logout_route) + return response + return wrapper + + +def includeme(config): + '''Pyramid configuration for PGWUI_Common + ''' + config.include('pyramid_mako') + config.include('pyramid_beaker') + config.add_static_view( + 'static', + 'pgwui_common:static/', + cache_max_age=3600) + config.add_route('home', DEFAULT_HOME_ROUTE) diff --git a/tests/test___init__.py b/tests/test_pgwui_common.py similarity index 84% rename from tests/test___init__.py rename to tests/test_pgwui_common.py index e51d3cf..bab7ff3 100644 --- a/tests/test___init__.py +++ b/tests/test_pgwui_common.py @@ -21,7 +21,7 @@ import pyramid.config from pyramid.threadlocal import get_current_request -import pgwui_common.__init__ as pgwui_common_init +import pgwui_common.pgwui_common as pgwui_common # Activiate our pytest plugin pytest_plugins = ("pgwui",) @@ -39,7 +39,7 @@ def mock_view(request): def check_base_view_results(request, pgwui): assert pgwui['url']['css'] == CSS_URL url = (request.application_url - + pgwui_common_init.DEFAULT_HOME_ROUTE) + + pgwui_common.DEFAULT_HOME_ROUTE) assert pgwui['route']['home'] == url @@ -51,16 +51,16 @@ def test_base_view_add(pyramid_request_config): def mock_view(request): return {} - pgwui_common_init.includeme(pyramid_request_config) - wrapper = pgwui_common_init.base_view(mock_view) + pgwui_common.includeme(pyramid_request_config) + wrapper = pgwui_common.base_view(mock_view) response = wrapper(get_current_request()) assert response['pgwui']['url']['css'][0:4] == 'http' def test_base_view_default(pyramid_request_config): '''The response retains the mock view's variables''' - pgwui_common_init.includeme(pyramid_request_config) - wrapper = pgwui_common_init.base_view(mock_view) + pgwui_common.includeme(pyramid_request_config) + wrapper = pgwui_common.base_view(mock_view) request = get_current_request() response = wrapper(request) pgwui = response['pgwui'] @@ -73,11 +73,11 @@ def test_auth_base_view_logout(pyramid_request_config): '''The response contains base_view and auth_base_view variables when there is a logout route ''' - pgwui_common_init.includeme(pyramid_request_config) + pgwui_common.includeme(pyramid_request_config) logout_route = '/logout' pyramid_request_config.add_route('pgwui_logout', logout_route) - wrapper = pgwui_common_init.auth_base_view(mock_view) + wrapper = pgwui_common.auth_base_view(mock_view) request = get_current_request() response = wrapper(request) @@ -91,9 +91,9 @@ def test_auth_base_view_nologout(pyramid_request_config): '''The response contains base_view and auth_base_view variables when there is no logout route ''' - pgwui_common_init.includeme(pyramid_request_config) + pgwui_common.includeme(pyramid_request_config) - wrapper = pgwui_common_init.auth_base_view(mock_view) + wrapper = pgwui_common.auth_base_view(mock_view) request = get_current_request() response = wrapper(request) @@ -122,7 +122,7 @@ def test_includeme_configurecalled(): self.home_route = route config = MockConfig() - pgwui_common_init.includeme(config) + pgwui_common.includeme(config) assert config.include_called assert config.add_static_view_called assert config.home_route == '/' @@ -132,4 +132,4 @@ def test_includeme_configurecalled(): def test_includeme(): config = pyramid.config.Configurator() - pgwui_common_init.includeme(config) + pgwui_common.includeme(config) -- 2.34.1