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

decorator: updates pass_record_or_draft #2336

Merged
merged 1 commit into from
Aug 9, 2023
Merged
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
28 changes: 24 additions & 4 deletions invenio_app_rdm/records_ui/views/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from functools import wraps

from flask import g, request
from flask import g, redirect, request, url_for
from invenio_communities.communities.resources.serializer import (
UICommunityJSONSerializer,
)
Expand Down Expand Up @@ -128,10 +128,30 @@ def view(**kwargs):
try:
record = service().read_draft(**read_kwargs)
except NoResultFound:
record = service().read(**read_kwargs)
try:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it feels like a lot of duplicated code?

record = None
if is_preview:
                try:
                    record = service().read_draft(**read_kwargs)
                except NoResultFound:
                    record = None

if not record:
    try:
                    record = service().read(**read_kwargs)
                except NoResultFound:
                    # If the parent pid is being used we can get the id of the latest record and redirect
                    latest_version = service().read_latest(**read_kwargs)
                    return redirect(
                        url_for(
                            "invenio_app_rdm_records.record_detail",
                            pid_value=latest_version.id,
                        )
                    )

I also think that it should never happen we reach this decorator, with a parent_pid and is_preview=True, WDYT?

record = service().read(**read_kwargs)
except NoResultFound:
# If the parent pid is being used we can get the id of the latest record and redirect
latest_version = service().read_latest(**read_kwargs)
return redirect(
url_for(
"invenio_app_rdm_records.record_detail",
pid_value=latest_version.id,
preview=1,
)
)
else:
record = service().read(**read_kwargs)

try:
record = service().read(**read_kwargs)
except NoResultFound:
# If the parent pid is being used we can get the id of the latest record and redirect
latest_version = service().read_latest(**read_kwargs)
return redirect(
url_for(
"invenio_app_rdm_records.record_detail",
pid_value=latest_version.id,
)
)
kwargs["record"] = record
return f(**kwargs)

Expand Down
Loading