Skip to content

Conversation

@JoeyStk
Copy link
Contributor

@JoeyStk JoeyStk commented Nov 28, 2025

Short description

This PR refactors the post method of page_form_view a little bit by adding some functionality to sub methods.

Proposed changes

  • Move some functionality of the post method into sub methods

Side effects

  • We decided to keep the conditional statement in the main method, because we think it's better to read this way. However with this in mind, we were only able to reduce the cyclomatic complexity marginally from 28 to 26.

Faithfulness to issue description and design

There are no intended deviations from the issue and design.

How to test

Resolved issues

Fixes: None
Hopefully this helps us a little bit with #3837


Pull Request Review Guidelines

@JoeyStk JoeyStk added this to the Next milestone Nov 28, 2025
@MizukiTemma MizukiTemma self-assigned this Dec 1, 2025
Copy link
Contributor

@jonbulz jonbulz left a comment

Choose a reason for hiding this comment

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

Thanks, this is definitely taking us in the right direction! A few suggestions from my side:

  • if not page_form.is_valid() or not page_translation_form.is_valid():
    # Add error messages
    page_form.add_error_messages(request)
    page_translation_form.add_error_messages(request)
    elif not request.user.has_perm(
    "cms.publish_page_object",
    page_form.instance,
    ) and (
    page_translation_form.cleaned_data.get("status")
    in [status.DRAFT, status.PUBLIC]
    ):
    # Raise PermissionDenied if user wants to publish page but doesn't have the permission
    raise PermissionDenied(
    f"{request.user!r} does not have the permission to publish {page_form.instance!r}",
    )
    elif (
    page_translation_form.instance.status == status.AUTO_SAVE
    and not page_form.has_changed()
    and not page_translation_form.has_changed()
    ):
    messages.info(request, _("No changes detected, autosave skipped"))

    could maybe be refactored to a method like check_page_save_valid or something similar? Then what is now in the else block would be a bit more explicit like:
    if self.check_page_save_valid(): Perhaps there is a better name, but you get what I mean.
  • # Add the success message and redirect to the edit page
    if not page_instance:
    if page_translation_form.instance.status == status.REVIEW:
    messages.success(
    request,
    _(
    'Page "{}" was successfully created and submitted for approval',
    ).format(page_translation_form.instance.title),
    )
    else:
    messages.success(
    request,
    _('Page "{}" was successfully created').format(
    page_translation_form.instance.title,
    ),
    )
    elif (
    not page_form.has_changed() and not page_translation_form.has_changed()
    ):
    messages.info(request, _("No changes detected, but date refreshed"))
    else:
    # Add the success message
    page_translation_form.add_success_message(request)

    could perhaps be refactored to self.handle_messages()?

That are the things that came to my mind, but I haven't completely thought them through, so feel free to disagree. :)

@JoeyStk
Copy link
Contributor Author

JoeyStk commented Dec 1, 2025

@jonbulz I really liked your suggestions! I especially loved the first one! It was something that had bothered me too, but I didn't know how it could be different :)

Please find my commits and feel free to re-review! :)

@JoeyStk JoeyStk requested review from jonbulz and removed request for jarlhengstmengel December 1, 2025 13:15
@MizukiTemma
Copy link
Member

@JoeyStk
Thank you for tackling this problem 💪

I'm not against the current implementation, but would prefer an approach like #4045 . Is there a reason you went for another way? (Just asking, as I don't know whether @svenseeberg and you had any exchange about this topic)

@svenseeberg
Copy link
Member

svenseeberg commented Dec 1, 2025

but would prefer an approach like #4045

I suppose you meant to refer to another PR? #4045 is exactly this one.

as I don't know whether @svenseeberg and you had any exchange about this topic

None specifically related to this PR that I can remember.

@JoeyStk
Copy link
Contributor Author

JoeyStk commented Dec 1, 2025

I suppose you meant to refer to another PR? #4045 is exactly this one.

Yes, I think #4046 was meant :)

I'm not against the current implementation, but would prefer an approach like #4045 . Is there a reason you went for another way?

So this PR and #4046 are doing different things. In the end we also want an approach like #4046 for pages, but first Peter and I wanted to refactor pages, so it becomes easier for us to implement the new approach like in #4046. So this is one step before what is done in #4046 :) I hope that clarifies it :)

@MizukiTemma
Copy link
Member

@JoeyStk @svenseeberg

I suppose you meant to refer to another PR? #4045 is exactly this one.

Yes, I think #4046 was meant :)

Ups, yes, I meant #4046 🙈

So this PR and #4046 are doing different things. In the end we also want an approach like #4046 for pages, but first Peter and I wanted to refactor pages, so it becomes easier for us to implement the new approach like in #4046. So this is one step before what is done in #4046 :) I hope that clarifies it :)

Thank you for explanation 👍

Copy link
Member

@MizukiTemma MizukiTemma left a comment

Choose a reason for hiding this comment

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

Very cool refactoring 😻 Thank you so much 🚀

Copy link
Contributor

@jonbulz jonbulz left a comment

Choose a reason for hiding this comment

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

Looks great, thank you very much!

@JoeyStk JoeyStk force-pushed the refactor-page-form-view branch from 1d94247 to 166a335 Compare December 3, 2025 14:09
@JoeyStk JoeyStk merged commit 2a873ab into develop Dec 3, 2025
5 checks passed
@JoeyStk JoeyStk deleted the refactor-page-form-view branch December 3, 2025 16:01
@JoeyStk JoeyStk added the not-testable Issues that are not testable label Dec 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

not-testable Issues that are not testable

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants