Skip to content

Commit

Permalink
Adding template page resource
Browse files Browse the repository at this point in the history
  • Loading branch information
mesemus committed Dec 29, 2023
1 parent 15a93ca commit 0743579
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 38 deletions.
2 changes: 1 addition & 1 deletion oarepo_ui/resources/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def _get_component_path(
if name in paths:
return paths[name]

raise ComponentNotFound(f"Unable to find a file named {name}")
raise ComponentNotFound(name)

def list_templates(self):
searchpath = []
Expand Down
4 changes: 4 additions & 0 deletions oarepo_ui/resources/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ def get_template_folder(self):
request_view_args = {}


class TemplatePageUIResourceConfig(UIResourceConfig):
routes = {}


class RecordsUIResourceConfig(UIResourceConfig):
routes = {
"search": "",
Expand Down
61 changes: 49 additions & 12 deletions oarepo_ui/resources/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ def as_blueprint(self, **options):
template_folder = self.config.get_template_folder()
if template_folder:
options["template_folder"] = template_folder
return super().as_blueprint(**options)
blueprint = super().as_blueprint(**options)
blueprint.app_context_processor(lambda: self.fill_jinja_context())
return blueprint

#
# Pluggable components
Expand All @@ -70,6 +72,12 @@ def run_components(self, action, *args, **kwargs):
if hasattr(component, action):
getattr(component, action)(*args, **kwargs)

def fill_jinja_context(self):
"""function providing flask template app context processors"""
ret = {}
self.run_components("fill_jinja_context", context=ret)
return ret


class RecordsUIResource(UIResource):
config: RecordsUIResourceConfig
Expand Down Expand Up @@ -113,17 +121,6 @@ def empty_record(self, resource_requestctx, **kwargs):
)
return empty_data

def as_blueprint(self, **options):
blueprint = super().as_blueprint(**options)
blueprint.app_context_processor(lambda: self.fill_jinja_context())
return blueprint

def fill_jinja_context(self):
"""function providing flask template app context processors"""
ret = {}
self.run_components("fill_jinja_context", context=ret)
return ret

@request_read_args
@request_view_args
def detail(self):
Expand Down Expand Up @@ -430,3 +427,43 @@ def expand_search_links(self, identity, pagination, args):
{"config": self.config, "url_prefix": self.config.url_prefix, "args": args},
)
return tpl.expand(identity, pagination)


class TemplatePageUIResource(UIResource):

def create_url_rules(self):
"""Create the URL rules for the record resource."""
route_config = self.config.routes
routes = []
for route_name, route_path in route_config.items():
handler = getattr(self, route_name, None) or partial(self.render, page=route_name)
if not hasattr(handler, '__name__'):
handler.__name__ = self.render.__name__
if not hasattr(handler, '__self__'):
handler.__self__ = self

routes.append(
route("GET", route_path, handler),
)
return routes

@request_view_args
def render(self, page, *args, **kwargs):
extra_context = dict()

self.run_components(
"before_render",
identity=g.identity,
args=resource_requestctx.args,
view_args=resource_requestctx.view_args,
ui_config=self.config,
extra_context=extra_context,
page=page,
)

return current_oarepo_ui.catalog.render(
page,
ui_config=self.config,
ui_resource=self,
extra_context=extra_context,
)
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = oarepo-ui
version = 5.0.94
version = 5.0.95
description = UI module for invenio 3.5+
long_description = file: README.md
long_description_content_type = text/markdown
Expand Down
11 changes: 10 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from invenio_accounts.testutils import login_user_via_session
from invenio_app.factory import create_app as _create_app

from tests.model import ModelUIResource, ModelUIResourceConfig
from tests.model import ModelUIResource, ModelUIResourceConfig, TitlePageUIResource, TitlePageUIResourceConfig


@pytest.fixture(scope="module")
Expand Down Expand Up @@ -69,6 +69,15 @@ def record_ui_resource(app, record_ui_resource_config, record_service):
return ui_resource


@pytest.fixture(scope="module")
def titlepage_ui_resource(app, ):
ui_resource = TitlePageUIResource(TitlePageUIResourceConfig())
app.register_blueprint(
ui_resource.as_blueprint(template_folder=Path(__file__).parent / "templates")
)
return ui_resource


@pytest.fixture()
def fake_manifest(app):
python_path = Path(sys.executable)
Expand Down
14 changes: 14 additions & 0 deletions tests/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
RecordsUIResourceConfig,
)
from oarepo_ui.resources.components import PermissionsComponent
from oarepo_ui.resources.config import TemplatePageUIResourceConfig
from oarepo_ui.resources.resource import TemplatePageUIResource


class ModelRecordIdProvider(RecordIdProviderV2):
Expand Down Expand Up @@ -109,3 +111,15 @@ def _get_record(self, resource_requestctx, allow_draft=False):
# we are not testing drafts here, so always return published record
# tests for drafts should be in oarepo-model-builder-drafts
return super()._get_record(resource_requestctx, allow_draft=False)


class TitlePageUIResourceConfig(TemplatePageUIResourceConfig):
blueprint_name = 'titlepage'
url_prefix = '/'
routes = {
'TitlePage': ''
}


class TitlePageUIResource(TemplatePageUIResource):
pass
4 changes: 4 additions & 0 deletions tests/templates/TitlePage.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{# def #}
{
"ok": "true"
}
10 changes: 10 additions & 0 deletions tests/test_template_page.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import json


def test_template_page(
app, titlepage_ui_resource, client, fake_manifest
):
with client.get(f"/") as c:
assert c.status_code == 200
data = json.loads(c.text)
assert 'ok' in data
23 changes: 0 additions & 23 deletions tests/test_templating.py

This file was deleted.

0 comments on commit 0743579

Please sign in to comment.