Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pages: added has_custom_view flag #90

Merged
merged 4 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions invenio_pages/alembic/b0f93ca4a147_add_has_custom_view_to_pages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#
# This file is part of Invenio.
# Copyright (C) 2016-2018 CERN.
#
# Invenio is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.

"""Add has_custom_view to pages."""

import sqlalchemy as sa
import sqlalchemy_utils
from alembic import op
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = "b0f93ca4a147"
down_revision = "145402e8523a"
branch_labels = ()
depends_on = "98e0a418340d"


def upgrade():
0einstein0 marked this conversation as resolved.
Show resolved Hide resolved
"""Upgrade database."""
# Add the new column
op.add_column(
"pages_page",
sa.Column(
"has_custom_view",
sa.Boolean(),
nullable=False,
server_default=sa.sql.expression.literal(False),
default=False,
),
)
op.add_column(
"pages_page_version",
sa.Column(
"has_custom_view",
sa.Boolean(),
nullable=True,
server_default=sa.sql.expression.literal(False),
default=False,
),
)

# ### end Alembic commands ###


def downgrade():
"""Downgrade database."""
# Drop the column
op.drop_column("pages_page", "has_custom_view")
op.drop_column("pages_page_version", "has_custom_view")
# ### end Alembic commands ###
6 changes: 3 additions & 3 deletions invenio_pages/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ def wrap_errorhandler(app):
existing_handler = None

if existing_handler:
app.error_handler_spec[None][404][
NotFound
] = lambda error: handle_not_found(error, wrapped=existing_handler)
app.error_handler_spec[None][404][NotFound] = (
lambda error: handle_not_found(error, wrapped=existing_handler)
)
else:
app.error_handler_spec.setdefault(None, {}).setdefault(404, {})
app.error_handler_spec[None][404][NotFound] = handle_not_found
Expand Down
6 changes: 5 additions & 1 deletion invenio_pages/records/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ class PageModel(db.Model, Timestamp):
template_name = db.Column(db.String(70), nullable=False)
"""Page template name."""

has_custom_view = db.Column(db.Boolean(), nullable=False, default=False)
lnielsen marked this conversation as resolved.
Show resolved Hide resolved
"""Page custom view flag."""

@classmethod
def create(self, data):
"""Create a new page."""
Expand All @@ -56,6 +59,7 @@ def create(self, data):
content=data.get("content", ""),
description=data.get("description", ""),
template_name=data["template_name"],
has_custom_view=data.get("has_custom_view", False),
)
db.session.add(obj)

Expand Down Expand Up @@ -143,7 +147,7 @@ def __repr__(self):
Used on Page admin view in inline model.
:returns: unambiguous page representation.
"""
return "URL: %s, title: %s" % (self.url, self.title)
return f"URL: {self.url}, title: {self.title}, has_custom_view: {self.has_custom_view}"


class PageList(db.Model):
Expand Down
1 change: 1 addition & 0 deletions invenio_pages/services/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@ class PageSchema(Schema):
content = DynamicSanitizedHTML()
description = fields.String()
template_name = fields.String()
has_custom_view = fields.Boolean()
lnielsen marked this conversation as resolved.
Show resolved Hide resolved
created = TZDateTime(timezone=timezone.utc, format="iso", dump_only=True)
updated = TZDateTime(timezone=timezone.utc, format="iso", dump_only=True)
10 changes: 8 additions & 2 deletions invenio_pages/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,18 @@

@blueprint.before_app_first_request
def register_pages():
"""Register URL rule of all static pages to the application."""
"""Register URL rules for all static pages in the application.

This function iterates over all static pages stored in the database and registers
their URL rules with the application. Pages with custom views are skipped to allow
default handling for pages without customizations.
"""
# We need to set the function view, to be able to directly register the urls in the Flask.url_map
current_app.view_functions["invenio_pages.view"] = view

for page in Page.query.all():
_add_url_rule(page.url)
if not page.has_custom_view: # Skip registration of pages with custom view
_add_url_rule(page.url)


@blueprint.app_template_filter("render_string")
Expand Down
42 changes: 41 additions & 1 deletion tests/records/test_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@
from invenio_pages import PageModel as Page
from invenio_pages.records.errors import PageNotCreatedError, PageNotFoundError
from invenio_pages.services.config import PageServiceConfig
from invenio_pages.views import register_pages


def test_page_repr(module_scoped_pages_fixture, base_app):
dog_page = Page.get_by_url("/dogs/shiba")
assert dog_page.__repr__() == "URL: /dogs/shiba, title: Page for doge!"
assert (
dog_page.__repr__()
== "URL: /dogs/shiba, title: Page for doge!, has_custom_view: False"
0einstein0 marked this conversation as resolved.
Show resolved Hide resolved
)


def test_page_versions(module_scoped_pages_fixture, base_app, db):
Expand Down Expand Up @@ -143,3 +147,39 @@ def test_delete_all(module_scoped_pages_fixture, base_app):
Page.delete_all()
pages = Page.search(map_search_params(PageServiceConfig.search, {}), [])
assert len(pages.items) == 0


def test_register_pages_with_custom_view(module_scoped_pages_fixture, base_app):
"""Test that URL is not registered if has_custom_view is True."""
# Create a page with has_custom_view set to True
data_with_custom_view = {
"url": "/custom-page-1",
"title": "Custom Page 1",
"content": "Content for Custom Page 1",
"template_name": "invenio_pages/default.html",
"has_custom_view": True,
}

# Create a page with has_custom_view set to False
data_without_custom_view = {
"url": "/custom-page-2",
"title": "Custom Page 2",
"content": "Content for Custom Page 2",
"template_name": "invenio_pages/default.html",
"has_custom_view": False,
}

# Create the pages
Page.create(data_with_custom_view)
Page.create(data_without_custom_view)

# Register pages
register_pages()

# Verify that the URL is not registered for the page with custom view set to True
assert not any(
rule.rule == "/custom-page-1" for rule in base_app.url_map.iter_rules()
)

# Verify that the URL is registered for the page with custom view set to False
assert any(rule.rule == "/custom-page-2" for rule in base_app.url_map.iter_rules())
2 changes: 2 additions & 0 deletions tests/resources/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def test_page_content(module_scoped_pages_fixture, base_client):
"content": "Generic dog.",
"id": "1",
"template_name": "invenio_pages/default.html",
"has_custom_view": False,
"links": {"self": "https://127.0.0.1:5000/api/pages/1"},
}
assert json == expected_data
Expand All @@ -47,6 +48,7 @@ def test_html_content(module_scoped_pages_fixture, base_client):
"content": "<h1>HTML aware dog.</h1>.\n" '<p class="test">paragraph<br /></p>',
"id": "4",
"template_name": "invenio_pages/default.html",
"has_custom_view": False,
"links": {"self": "https://127.0.0.1:5000/api/pages/4"},
}
assert json == expected_data
Expand Down
2 changes: 2 additions & 0 deletions tests/services/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def test_page_read(module_scoped_pages_fixture, simple_user_identity):
"content": "Generic dog.",
"id": "1",
"template_name": "invenio_pages/default.html",
"has_custom_view": False,
"links": {"self": "https://127.0.0.1:5000/api/pages/1"},
}
assert page == expected_data
Expand All @@ -50,6 +51,7 @@ def test_page_read_by_url(module_scoped_pages_fixture, simple_user_identity):
"content": "Generic dog.",
"id": "1",
"template_name": "invenio_pages/default.html",
"has_custom_view": False,
"links": {"self": "https://127.0.0.1:5000/api/pages/1"},
}
assert page == expected_data
Expand Down
Loading