From 3edd1f17bdfe7f1f83967dc44aefd7249e8e7dbc Mon Sep 17 00:00:00 2001 From: Luis Aleixo Date: Mon, 8 May 2023 10:53:30 +0200 Subject: [PATCH 1/9] declared env variable extra_pages --- app-config/calculator-app/app.sh | 2 ++ app-config/docker-compose.yml | 2 ++ app-config/openshift/deploymentconfig.yaml | 2 ++ 3 files changed, 6 insertions(+) diff --git a/app-config/calculator-app/app.sh b/app-config/calculator-app/app.sh index 8555379e..6a808f8c 100755 --- a/app-config/calculator-app/app.sh +++ b/app-config/calculator-app/app.sh @@ -21,6 +21,8 @@ if [[ "$APP_NAME" == "calculator-app" ]]; then export "ARVE_CLIENT_ID"="$ARVE_CLIENT_ID" export "ARVE_CLIENT_SECRET"="$ARVE_CLIENT_SECRET" + export "EXTRA_PAGES"="$EXTRA_PAGES" + echo "Starting the caimira webservice with: python -m caimira.apps.calculator ${args[@]}" python -m caimira.apps.calculator "${args[@]}" elif [[ "$APP_NAME" == "caimira-voila" ]]; then diff --git a/app-config/docker-compose.yml b/app-config/docker-compose.yml index d9ddb34a..ca2f5e3b 100644 --- a/app-config/docker-compose.yml +++ b/app-config/docker-compose.yml @@ -20,6 +20,7 @@ services: - APPLICATION_ROOT=/ - CAIMIRA_CALCULATOR_PREFIX=/calculator-cern - CAIMIRA_THEME=caimira/apps/templates/cern + - EXTRA_PAGES=[{"url":"/about","filename":"about", "is_root":True},{"url":"/user-guide","filename":"userguide","is_root":False}] user: ${CURRENT_UID} calculator-open-app: @@ -29,6 +30,7 @@ services: - APP_NAME=calculator-app - APPLICATION_ROOT=/ - CAIMIRA_CALCULATOR_PREFIX=/calculator-open + - EXTRA_PAGES=[{"url":"/about","filename":"about", "is_root":True},{"url":"/user-guide","filename":"userguide","is_root":False}] user: ${CURRENT_UID} auth-service: diff --git a/app-config/openshift/deploymentconfig.yaml b/app-config/openshift/deploymentconfig.yaml index e96d65a1..a646d7ca 100644 --- a/app-config/openshift/deploymentconfig.yaml +++ b/app-config/openshift/deploymentconfig.yaml @@ -283,6 +283,8 @@ secretKeyRef: key: ARVE_API_KEY name: arve-api + - name: EXTRA_PAGES + value: [{"url":"/about","filename":"about", "is_root":True},{"url":"/user-guide","filename":"userguide","is_root":False}] image: '${PROJECT_NAME}/calculator-app' ports: - containerPort: 8080 From 2d3a925fc5394346546b3529a3bbeacbdcb2d44a Mon Sep 17 00:00:00 2001 From: Luis Aleixo Date: Mon, 8 May 2023 10:53:50 +0200 Subject: [PATCH 2/9] adapted init methods to consider readme and about pages as generic --- caimira/apps/calculator/__init__.py | 71 +++++++++++--------- caimira/tests/apps/calculator/test_webapp.py | 10 --- 2 files changed, 38 insertions(+), 43 deletions(-) diff --git a/caimira/apps/calculator/__init__.py b/caimira/apps/calculator/__init__.py index fef9ceaf..76583dc1 100644 --- a/caimira/apps/calculator/__init__.py +++ b/caimira/apps/calculator/__init__.py @@ -16,6 +16,7 @@ import traceback import typing import uuid import zlib +import ast import jinja2 import loky @@ -201,20 +202,6 @@ class LandingPage(BaseRequestHandler): self.finish(report) -class AboutPage(BaseRequestHandler): - def get(self): - template_environment = self.settings["template_environment"] - template = template_environment.get_template("about.html.j2") - report = template.render( - user=self.current_user, - get_url = template.globals['get_url'], - get_calculator_url = template.globals["get_calculator_url"], - active_page="about", - text_blocks=template_environment.globals["common_text"] - ) - self.finish(report) - - class CalculatorForm(BaseRequestHandler): def get(self): template_environment = self.settings["template_environment"] @@ -242,20 +229,6 @@ class CompressedCalculatorFormInputs(BaseRequestHandler): return self.finish("Invalid calculator data: it seems incomplete. Was there an error copying & pasting the URL?") template_environment = self.settings["template_environment"] self.redirect(f'{template_environment.globals["get_calculator_url"]()}?{args}') - - -class ReadmeHandler(BaseRequestHandler): - def get(self): - template_environment = self.settings["template_environment"] - template = template_environment.get_template("userguide.html.j2") - readme = template.render( - active_page="calculator/user-guide", - user=self.current_user, - get_url = template.globals['get_url'], - get_calculator_url = template.globals["get_calculator_url"], - text_blocks=template_environment.globals["common_text"], - ) - self.finish(readme) class ArveData(BaseRequestHandler): @@ -345,6 +318,25 @@ class CasesData(BaseRequestHandler): return self.finish(str(round(cases.loc[eight_days_ago:current_date]['New_cases'].mean()))) +class GenericExtraPage(BaseRequestHandler): + + def initialize(self, active_page: str, filename: str): + self.active_page = active_page + # The endpoint that will be used as template name + self.filename = filename + + def get(self): + template_environment = self.settings["template_environment"] + template = template_environment.get_template(f"{self.filename}.html.j2") + self.finish(template.render( + user=self.current_user, + get_url = template.globals['get_url'], + get_calculator_url = template.globals["get_calculator_url"], + active_page=self.active_page, + text_blocks=template_environment.globals["common_text"] + )) + + def get_url(app_root: str, relative_path: str = '/'): return app_root.rstrip('/') + relative_path.rstrip('/') @@ -363,21 +355,36 @@ def make_app( get_root_url = functools.partial(get_url, APPLICATION_ROOT) get_root_calculator_url = functools.partial(get_calculator_url, APPLICATION_ROOT, calculator_prefix) - urls: typing.Any = [ + urls: typing.List = [ (get_root_url(r'/?'), LandingPage), (get_root_url(r'/_c/(.*)'), CompressedCalculatorFormInputs), - (get_root_url(r'/about'), AboutPage), (get_root_url(r'/static/(.*)'), StaticFileHandler, {'path': static_dir}), (get_root_calculator_url(r'/?'), CalculatorForm), (get_root_calculator_url(r'/report'), ConcentrationModel), (get_root_calculator_url(r'/report-json'), ConcentrationModelJsonResponse), (get_root_calculator_url(r'/baseline-model/result'), StaticModel), - (get_root_calculator_url(r'/user-guide'), ReadmeHandler), (get_root_calculator_url(r'/api/arve/v1/(.*)/(.*)'), ArveData), (get_root_calculator_url(r'/cases/(.*)'), CasesData), (get_root_calculator_url(r'/static/(.*)'), StaticFileHandler, {'path': calculator_static_dir}), ] + # Any extra generic page must be declared in the env. variable "EXTRA_PAGES" + extra_pages=os.environ.get('EXTRA_PAGES', None) + try: + for extra in ast.literal_eval(extra_pages): # type: ignore + if extra['is_root']: + urls.append((get_root_url(r'%s' % extra['url']), + GenericExtraPage, { + 'active_page': extra['url'].strip('/'), + 'filename': extra['filename'], })) + else: + urls.append((get_root_calculator_url(r'%s' % extra['url']), + GenericExtraPage, { + 'active_page': 'calculator/' + extra['url'].strip('/'), + 'filename': extra['filename'], })) + except (SyntaxError, ValueError): + pass + caimira_templates = Path(__file__).parent.parent / "templates" calculator_templates = Path(__file__).parent / "templates" templates_directories = [caimira_templates, calculator_templates] @@ -401,8 +408,6 @@ def make_app( return Application( urls, debug=debug, - # calculator_prefix=calculator_prefix, - # APPLICATION_ROOT=APPLICATION_ROOT, template_environment=template_environment, default_handler_class=Missing404Handler, report_generator=ReportGenerator(loader, get_root_url, get_root_calculator_url), diff --git a/caimira/tests/apps/calculator/test_webapp.py b/caimira/tests/apps/calculator/test_webapp.py index 6dc7d692..3bd3c362 100644 --- a/caimira/tests/apps/calculator/test_webapp.py +++ b/caimira/tests/apps/calculator/test_webapp.py @@ -29,16 +29,6 @@ async def test_calculator_form(http_server_client): assert response.code == 200 -async def test_user_guide(http_server_client): - resp = await http_server_client.fetch('/calculator/user-guide') - assert resp.code == 200 - - -async def test_about(http_server_client): - resp = await http_server_client.fetch('/about') - assert resp.code == 200 - - async def test_404(http_server_client): resp = await http_server_client.fetch('/doesnt-exist', raise_error=False) assert resp.code == 404 From fb18a2a715cd609c366b525211390967384d57f3 Mon Sep 17 00:00:00 2001 From: Luis Aleixo Date: Mon, 8 May 2023 10:55:45 +0200 Subject: [PATCH 3/9] added tests for generic pages --- app-config/openshift/deploymentconfig.yaml | 2 +- caimira/tests/apps/calculator/test_webapp.py | 26 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/app-config/openshift/deploymentconfig.yaml b/app-config/openshift/deploymentconfig.yaml index a646d7ca..d5a0c966 100644 --- a/app-config/openshift/deploymentconfig.yaml +++ b/app-config/openshift/deploymentconfig.yaml @@ -284,7 +284,7 @@ key: ARVE_API_KEY name: arve-api - name: EXTRA_PAGES - value: [{"url":"/about","filename":"about", "is_root":True},{"url":"/user-guide","filename":"userguide","is_root":False}] + value: [{"url":"/about","filename":"about","is_root":True},{"url":"/user-guide","filename":"userguide","is_root":False}] image: '${PROJECT_NAME}/calculator-app' ports: - containerPort: 8080 diff --git a/caimira/tests/apps/calculator/test_webapp.py b/caimira/tests/apps/calculator/test_webapp.py index 3bd3c362..de29246a 100644 --- a/caimira/tests/apps/calculator/test_webapp.py +++ b/caimira/tests/apps/calculator/test_webapp.py @@ -137,3 +137,29 @@ class TestError500(tornado.testing.AsyncHTTPTestCase): response = self.fetch('/') assert response.code == 500 assert 'Unfortunately an error occurred when processing your request' in response.body.decode() + + +class TestCERNGenericPage(tornado.testing.AsyncHTTPTestCase): + def get_app(self): + cern_theme = Path(caimira.apps.calculator.__file__).parent.parent / 'themes' / 'cern' + app = caimira.apps.calculator.make_app(theme_dir=cern_theme) + pages = [ + (r'/calculator/user-guide', caimira.apps.calculator.GenericExtraPage, {'active_page': 'userguide', 'filename': 'userguide'}), + (r'/about', caimira.apps.calculator.GenericExtraPage, {'active_page': 'about', 'filename': 'about'}), + ] + + return tornado.web.Application(pages, **app.settings) + + @tornado.testing.gen_test(timeout=_TIMEOUT) + def test_user_guide(self): + response = yield self.http_client.fetch(self.get_url('/calculator/user-guide')) + self.assertEqual(response.code, 200) + + @tornado.testing.gen_test(timeout=_TIMEOUT) + def test_about(self): + response = yield self.http_client.fetch(self.get_url('/about')) + self.assertEqual(response.code, 200) + + def test_calculator_404(self): + response = self.fetch('/calculator') + assert response.code == 404 From eac75910461b059db164a24fa64ae90385ec10a4 Mon Sep 17 00:00:00 2001 From: Luis Aleixo Date: Mon, 8 May 2023 11:49:08 +0200 Subject: [PATCH 4/9] modified deployment config --- app-config/openshift/deploymentconfig.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app-config/openshift/deploymentconfig.yaml b/app-config/openshift/deploymentconfig.yaml index d5a0c966..77f48c92 100644 --- a/app-config/openshift/deploymentconfig.yaml +++ b/app-config/openshift/deploymentconfig.yaml @@ -284,7 +284,7 @@ key: ARVE_API_KEY name: arve-api - name: EXTRA_PAGES - value: [{"url":"/about","filename":"about","is_root":True},{"url":"/user-guide","filename":"userguide","is_root":False}] + value: '[{"url":"/about","filename":"about","is_root":True},{"url":"/user-guide","filename":"userguide","is_root":False}]' image: '${PROJECT_NAME}/calculator-app' ports: - containerPort: 8080 From 7fd0e0f8ec278a4df885f6032774929ccc2f97f3 Mon Sep 17 00:00:00 2001 From: Luis Aleixo Date: Tue, 9 May 2023 17:13:46 +0200 Subject: [PATCH 5/9] updated env var EXTRA_PAGES structure --- caimira/apps/calculator/__init__.py | 29 ++++++++++---------- caimira/tests/apps/calculator/test_webapp.py | 4 +-- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/caimira/apps/calculator/__init__.py b/caimira/apps/calculator/__init__.py index 76583dc1..dba1208e 100644 --- a/caimira/apps/calculator/__init__.py +++ b/caimira/apps/calculator/__init__.py @@ -1,6 +1,8 @@ # This module is part of CAiMIRA. Please see the repository at # https://gitlab.cern.ch/caimira/caimira for details of the license and terms of use. +import ast +import logging import asyncio import concurrent.futures import datetime @@ -16,7 +18,7 @@ import traceback import typing import uuid import zlib -import ast + import jinja2 import loky @@ -38,6 +40,8 @@ from .user import AuthenticatedUser, AnonymousUser # increase the overall CAiMIRA version (found at ``caimira.__version__``). __version__ = "4.9" +LOG = logging.getLogger(__name__) + class BaseRequestHandler(RequestHandler): async def prepare(self): @@ -327,7 +331,7 @@ class GenericExtraPage(BaseRequestHandler): def get(self): template_environment = self.settings["template_environment"] - template = template_environment.get_template(f"{self.filename}.html.j2") + template = template_environment.get_template(self.filename) self.finish(template.render( user=self.current_user, get_url = template.globals['get_url'], @@ -369,22 +373,19 @@ def make_app( ] # Any extra generic page must be declared in the env. variable "EXTRA_PAGES" - extra_pages=os.environ.get('EXTRA_PAGES', None) + extra_pages: typing.Union[str, typing.List] = os.environ.get('EXTRA_PAGES', []) try: - for extra in ast.literal_eval(extra_pages): # type: ignore - if extra['is_root']: - urls.append((get_root_url(r'%s' % extra['url']), - GenericExtraPage, { - 'active_page': extra['url'].strip('/'), - 'filename': extra['filename'], })) - else: - urls.append((get_root_calculator_url(r'%s' % extra['url']), - GenericExtraPage, { - 'active_page': 'calculator/' + extra['url'].strip('/'), - 'filename': extra['filename'], })) + pages = ast.literal_eval(extra_pages) # type: ignore except (SyntaxError, ValueError): + LOG.warning('Warning: There was a problem with the extra pages. Is the "EXTRA_PAGES" environment variable correctly defined?') pass + for extra in pages: + urls.append((get_root_url(r'%s' % extra['url_path']), + GenericExtraPage, { + 'active_page': extra['url_path'].strip('/'), + 'filename': extra['filename'], })) + caimira_templates = Path(__file__).parent.parent / "templates" calculator_templates = Path(__file__).parent / "templates" templates_directories = [caimira_templates, calculator_templates] diff --git a/caimira/tests/apps/calculator/test_webapp.py b/caimira/tests/apps/calculator/test_webapp.py index de29246a..32d22352 100644 --- a/caimira/tests/apps/calculator/test_webapp.py +++ b/caimira/tests/apps/calculator/test_webapp.py @@ -144,8 +144,8 @@ class TestCERNGenericPage(tornado.testing.AsyncHTTPTestCase): cern_theme = Path(caimira.apps.calculator.__file__).parent.parent / 'themes' / 'cern' app = caimira.apps.calculator.make_app(theme_dir=cern_theme) pages = [ - (r'/calculator/user-guide', caimira.apps.calculator.GenericExtraPage, {'active_page': 'userguide', 'filename': 'userguide'}), - (r'/about', caimira.apps.calculator.GenericExtraPage, {'active_page': 'about', 'filename': 'about'}), + (r'/calculator/user-guide', caimira.apps.calculator.GenericExtraPage, {'active_page': 'calculator/user-guide', 'filename': 'userguide.html.j2'}), + (r'/about', caimira.apps.calculator.GenericExtraPage, {'active_page': 'about', 'filename': 'about.html.j2'}), ] return tornado.web.Application(pages, **app.settings) From 18978aef2097b2466f5c38441d64dd5441aa5778 Mon Sep 17 00:00:00 2001 From: Luis Aleixo Date: Tue, 9 May 2023 17:17:52 +0200 Subject: [PATCH 6/9] adapted deployment configs --- app-config/docker-compose.yml | 4 ++-- app-config/openshift/deploymentconfig.yaml | 2 +- caimira/apps/calculator/__init__.py | 1 + 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app-config/docker-compose.yml b/app-config/docker-compose.yml index ca2f5e3b..c78cb131 100644 --- a/app-config/docker-compose.yml +++ b/app-config/docker-compose.yml @@ -20,7 +20,7 @@ services: - APPLICATION_ROOT=/ - CAIMIRA_CALCULATOR_PREFIX=/calculator-cern - CAIMIRA_THEME=caimira/apps/templates/cern - - EXTRA_PAGES=[{"url":"/about","filename":"about", "is_root":True},{"url":"/user-guide","filename":"userguide","is_root":False}] + - EXTRA_PAGES=[{"url_path":"/about","filename":"about.html.j2"},{"url_path":"/calculator/user-guide","filename":"userguide.html.j2"}] user: ${CURRENT_UID} calculator-open-app: @@ -30,7 +30,7 @@ services: - APP_NAME=calculator-app - APPLICATION_ROOT=/ - CAIMIRA_CALCULATOR_PREFIX=/calculator-open - - EXTRA_PAGES=[{"url":"/about","filename":"about", "is_root":True},{"url":"/user-guide","filename":"userguide","is_root":False}] + - EXTRA_PAGES=[{"url_path":"/about","filename":"about.html.j2"},{"url_path":"/calculator/user-guide","filename":"userguide.html.j2"}] user: ${CURRENT_UID} auth-service: diff --git a/app-config/openshift/deploymentconfig.yaml b/app-config/openshift/deploymentconfig.yaml index 77f48c92..2a130971 100644 --- a/app-config/openshift/deploymentconfig.yaml +++ b/app-config/openshift/deploymentconfig.yaml @@ -284,7 +284,7 @@ key: ARVE_API_KEY name: arve-api - name: EXTRA_PAGES - value: '[{"url":"/about","filename":"about","is_root":True},{"url":"/user-guide","filename":"userguide","is_root":False}]' + value: '[{"url_path":"/about","filename":"about.html.j2"},{"url_path":"/calculator/user-guide","filename":"userguide.html.j2"}]' image: '${PROJECT_NAME}/calculator-app' ports: - containerPort: 8080 diff --git a/caimira/apps/calculator/__init__.py b/caimira/apps/calculator/__init__.py index dba1208e..3c71ad06 100644 --- a/caimira/apps/calculator/__init__.py +++ b/caimira/apps/calculator/__init__.py @@ -374,6 +374,7 @@ def make_app( # Any extra generic page must be declared in the env. variable "EXTRA_PAGES" extra_pages: typing.Union[str, typing.List] = os.environ.get('EXTRA_PAGES', []) + pages: typing.List = [] try: pages = ast.literal_eval(extra_pages) # type: ignore except (SyntaxError, ValueError): From 0a08b10d0f9c4dc142056c7e0775c4f35da4b41d Mon Sep 17 00:00:00 2001 From: Luis Aleixo Date: Thu, 11 May 2023 11:30:09 +0200 Subject: [PATCH 7/9] updated docker-compose file --- app-config/docker-compose.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app-config/docker-compose.yml b/app-config/docker-compose.yml index c78cb131..a8a0782e 100644 --- a/app-config/docker-compose.yml +++ b/app-config/docker-compose.yml @@ -20,7 +20,7 @@ services: - APPLICATION_ROOT=/ - CAIMIRA_CALCULATOR_PREFIX=/calculator-cern - CAIMIRA_THEME=caimira/apps/templates/cern - - EXTRA_PAGES=[{"url_path":"/about","filename":"about.html.j2"},{"url_path":"/calculator/user-guide","filename":"userguide.html.j2"}] + - EXTRA_PAGES=[{"url_path":"/about","filename":"about.html.j2"},{"url_path":"/calculator-cern/user-guide","filename":"userguide.html.j2"}] user: ${CURRENT_UID} calculator-open-app: @@ -30,7 +30,7 @@ services: - APP_NAME=calculator-app - APPLICATION_ROOT=/ - CAIMIRA_CALCULATOR_PREFIX=/calculator-open - - EXTRA_PAGES=[{"url_path":"/about","filename":"about.html.j2"},{"url_path":"/calculator/user-guide","filename":"userguide.html.j2"}] + - EXTRA_PAGES=[{"url_path":"/about","filename":"about.html.j2"},{"url_path":"/calculator-open/user-guide","filename":"userguide.html.j2"}] user: ${CURRENT_UID} auth-service: From 77774112e424be368ab8ad2143090f76fe463541 Mon Sep 17 00:00:00 2001 From: Luis Aleixo Date: Thu, 11 May 2023 11:55:56 +0200 Subject: [PATCH 8/9] added env variable to deploymentconfig template --- app-config/openshift/deploymentconfig.yaml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app-config/openshift/deploymentconfig.yaml b/app-config/openshift/deploymentconfig.yaml index 2a130971..cda24258 100644 --- a/app-config/openshift/deploymentconfig.yaml +++ b/app-config/openshift/deploymentconfig.yaml @@ -284,7 +284,7 @@ key: ARVE_API_KEY name: arve-api - name: EXTRA_PAGES - value: '[{"url_path":"/about","filename":"about.html.j2"},{"url_path":"/calculator/user-guide","filename":"userguide.html.j2"}]' + value: '[{"url_path":"/about","filename":"about.html.j2"},{"url_path":"/calculator-cern/user-guide","filename":"userguide.html.j2"}]' image: '${PROJECT_NAME}/calculator-app' ports: - containerPort: 8080 @@ -362,6 +362,8 @@ value: / - name: CAIMIRA_CALCULATOR_PREFIX value: /calculator-open + - name: EXTRA_PAGES + value: '[{"url_path":"/about","filename":"about.html.j2"},{"url_path":"/calculator-open/user-guide","filename":"userguide.html.j2"}]' image: '${PROJECT_NAME}/calculator-app' ports: - containerPort: 8080 From 5525df36eb78862af4a06f531aab1e55effa9c1d Mon Sep 17 00:00:00 2001 From: Luis Aleixo Date: Fri, 12 May 2023 09:15:25 +0200 Subject: [PATCH 9/9] version update --- caimira/apps/calculator/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/caimira/apps/calculator/__init__.py b/caimira/apps/calculator/__init__.py index 3c71ad06..5f4ef9bd 100644 --- a/caimira/apps/calculator/__init__.py +++ b/caimira/apps/calculator/__init__.py @@ -38,7 +38,7 @@ from .user import AuthenticatedUser, AnonymousUser # calculator version. If the calculator needs to make breaking changes (e.g. change # form attributes) then it can also increase its MAJOR version without needing to # increase the overall CAiMIRA version (found at ``caimira.__version__``). -__version__ = "4.9" +__version__ = "4.10" LOG = logging.getLogger(__name__)