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

[16.0][IMP] membership_extension & _variable_period: Make sure necessary fields are always truthy #157

Merged
merged 1 commit into from
Oct 9, 2023

Conversation

carmenbianca
Copy link
Member

This is an additional sanity check to make sure that fields that are expected to be available are actually available.

The situation here is a little tricky. Especially as pertains date_from and date_to; these fields are required=True nowhere. Not on the product, not on the membership invoice, and not on the membership line. However, they are set as required in the product and membership line views. If these values are ever empty, it's a sure sign that something went wrong somewhere, because the user should not be able to reach such a state via the UI.

One sure way to reach that state was via the demo data, however, which had no start and end dates. This is now fixed.

The situation for the variable period fields is similar. These fields should never be empty when the membership type is variable, and it's not typically possible to empty these values via the UI. For convenience's sake, I simply set them to required an sich, with a default value.

@carmenbianca carmenbianca force-pushed the 16.0-add-constrains branch 3 times, most recently from d475f3e to 3f8afd4 Compare October 5, 2023 14:08
Copy link
Member

@chienandalu chienandalu left a comment

Choose a reason for hiding this comment

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

Hi @carmenbianca I'm ok with your suggestions. Just a couple of comments:

Comment on lines 39 to 41
for record in self:
if record.membership:
if not record.membership_date_from or not record.membership_date_to:
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
for record in self:
if record.membership:
if not record.membership_date_from or not record.membership_date_to:
if self.filtered(lambda x: x.membership and (not x.membership_date_from or not x.membership_date_to):

Comment on lines 99 to 102
for record in self:
if record.membership:
if record.membership_type == "fixed":
return super(ProductTemplate, record)._check_membership_dates()
Copy link
Member

Choose a reason for hiding this comment

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

You're breaking your loop here. Maybe what you want to do is:

Suggested change
for record in self:
if record.membership:
if record.membership_type == "fixed":
return super(ProductTemplate, record)._check_membership_dates()
return super(ProductTemplate, self.filtered(lambda x: x.membership_type == "fixed"))._check_membership_dates()

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch!

…elds are always truthy

This is an additional sanity check to make sure that fields that are
expected to be available are actually available.

The situation here is a little tricky. Especially as pertains date_from
and date_to; these fields are `required=True` nowhere. Not on the
product, not on the membership invoice, and not on the membership line.
However, they _are_ set as required in the product and membership line
views. If these values are ever empty, it's a sure sign that something
went wrong somewhere, because the user should not be able to reach such
a state via the UI.

One sure way to reach that state was via the demo data, however, which
had no start and end dates. This is now fixed.

The situation for the variable period fields is similar. These fields
should never be empty when the membership type is variable, and it's not
typically possible to empty these values via the UI. For convenience's
sake, I simply set them to required an sich, with a default value.

Signed-off-by: Carmen Bianca BAKKER <[email protected]>
@carmenbianca
Copy link
Member Author

@chienandalu I've implemented and pushed your suggestions. Thank you for the review.

Copy link
Member

@chienandalu chienandalu left a comment

Choose a reason for hiding this comment

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

Thanks :)

@pedrobaeza pedrobaeza added this to the 16.0 milestone Oct 6, 2023
cls.product_1.membership = True
cls.product_1.write(
{
"membership_date_from": "2023-01-01",
Copy link
Member

Choose a reason for hiding this comment

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

Why do you need to change this?

Copy link
Member Author

Choose a reason for hiding this comment

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

@pedrobaeza Because the new constraint won't let a membership product have no start/end dates.

    @api.constrains("membership_date_from", "membership_date_to", "membership")
    def _check_membership_dates(self):
        if self.filtered(
            lambda record: record.membership
            and (not record.membership_date_from or not record.membership_date_to)
        ):
            raise ValidationError(
                _("A membership product must have a start date and an end date.")
            )

(Interestingly, the write function is sensitive to the order of key-value pairs. If "membership": True appears before the other items, the constraint complains. Not sure why that happens.)

Copy link
Member

Choose a reason for hiding this comment

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

It's weird, as in other places, you are removing such fields, but it's not a big deal.

/ocabot merge patch

@pedrobaeza
Copy link
Member

/ocabot merge patch

@OCA-git-bot
Copy link
Contributor

Hey, thanks for contributing! Proceeding to merge this for you.
Prepared branch 16.0-ocabot-merge-pr-157-by-pedrobaeza-bump-patch, awaiting test results.

@OCA-git-bot OCA-git-bot merged commit 322941a into OCA:16.0 Oct 9, 2023
6 checks passed
@OCA-git-bot
Copy link
Contributor

Congratulations, your PR was merged at 3ee9de5. Thanks a lot for contributing to OCA. ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants