Skip to content

Commit 633ac13

Browse files
sakshamarora1zzacharo
authored andcommitted
communities_ui: views: Add RecordPermissionDeniedError Handler
1 parent fbe030e commit 633ac13

File tree

1 file changed

+12
-41
lines changed
  • invenio_app_rdm/communities_ui/views

1 file changed

+12
-41
lines changed

invenio_app_rdm/communities_ui/views/ui.py

Lines changed: 12 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,18 @@
1515
UICommunityJSONSerializer,
1616
)
1717
from invenio_communities.errors import CommunityDeletedError
18+
from invenio_communities.views.ui import (
19+
not_found_error,
20+
record_permission_denied_error,
21+
record_tombstone_error,
22+
)
1823
from invenio_i18n import lazy_gettext as _
1924
from invenio_pidstore.errors import PIDDeletedError, PIDDoesNotExistError
2025
from invenio_rdm_records.collections import search_app_context as c_search_app_context
21-
from invenio_records_resources.services.errors import PermissionDeniedError
26+
from invenio_records_resources.services.errors import (
27+
PermissionDeniedError,
28+
RecordPermissionDeniedError,
29+
)
2230

2331
from ..searchapp import search_app_context
2432
from .communities import (
@@ -30,46 +38,6 @@
3038
)
3139

3240

33-
#
34-
# Error handlers
35-
#
36-
def not_found_error(error):
37-
"""Handler for 'Not Found' errors."""
38-
return render_template(current_app.config["THEME_404_TEMPLATE"]), 404
39-
40-
41-
def record_tombstone_error(error):
42-
"""Tombstone page."""
43-
# the RecordDeletedError will have the following properties,
44-
# while the PIDDeletedError won't
45-
record = getattr(error, "record", None)
46-
if (record_ui := getattr(error, "result_item", None)) is not None:
47-
if record is None:
48-
record = record_ui._record
49-
record_ui = UICommunityJSONSerializer().dump_obj(record_ui.to_dict())
50-
51-
# render a 404 page if the tombstone isn't visible
52-
if not record.tombstone.is_visible:
53-
return not_found_error(error)
54-
55-
# we only render a tombstone page if there is a record with a visible tombstone
56-
return (
57-
render_template(
58-
"invenio_communities/tombstone.html",
59-
record=record_ui,
60-
),
61-
410,
62-
)
63-
64-
65-
def record_permission_denied_error(error):
66-
"""Handle permission denier error on record views."""
67-
if not current_user.is_authenticated:
68-
# trigger the flask-login unauthorized handler
69-
return current_app.login_manager.unauthorized()
70-
return render_template(current_app.config["THEME_403_TEMPLATE"]), 403
71-
72-
7341
def _show_browse_page():
7442
"""Whether the browse page should be visible in the menu."""
7543
return (
@@ -118,6 +86,9 @@ def create_ui_blueprint(app):
11886
blueprint.register_error_handler(
11987
PermissionDeniedError, record_permission_denied_error
12088
)
89+
blueprint.register_error_handler(
90+
RecordPermissionDeniedError, record_permission_denied_error
91+
)
12192
blueprint.register_error_handler(PIDDeletedError, record_tombstone_error)
12293
blueprint.register_error_handler(CommunityDeletedError, record_tombstone_error)
12394
blueprint.register_error_handler(PIDDoesNotExistError, not_found_error)

0 commit comments

Comments
 (0)