|
15 | 15 | UICommunityJSONSerializer, |
16 | 16 | ) |
17 | 17 | 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 | +) |
18 | 23 | from invenio_i18n import lazy_gettext as _ |
19 | 24 | from invenio_pidstore.errors import PIDDeletedError, PIDDoesNotExistError |
20 | 25 | 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 | +) |
22 | 30 |
|
23 | 31 | from ..searchapp import search_app_context |
24 | 32 | from .communities import ( |
|
30 | 38 | ) |
31 | 39 |
|
32 | 40 |
|
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 | | - |
73 | 41 | def _show_browse_page(): |
74 | 42 | """Whether the browse page should be visible in the menu.""" |
75 | 43 | return ( |
@@ -118,6 +86,9 @@ def create_ui_blueprint(app): |
118 | 86 | blueprint.register_error_handler( |
119 | 87 | PermissionDeniedError, record_permission_denied_error |
120 | 88 | ) |
| 89 | + blueprint.register_error_handler( |
| 90 | + RecordPermissionDeniedError, record_permission_denied_error |
| 91 | + ) |
121 | 92 | blueprint.register_error_handler(PIDDeletedError, record_tombstone_error) |
122 | 93 | blueprint.register_error_handler(CommunityDeletedError, record_tombstone_error) |
123 | 94 | blueprint.register_error_handler(PIDDoesNotExistError, not_found_error) |
|
0 commit comments