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][MIG] sale_loyalty_auto_refresh: Migration to 16.0 #208

Merged

Conversation

nguyenminhchien
Copy link
Contributor

Rename from sale_coupon_auto_refresh to sale_loyalty_auto_refresh

@nguyenminhchien nguyenminhchien marked this pull request as ready for review March 21, 2024 03:19
Copy link

@cyrilmanuel cyrilmanuel left a comment

Choose a reason for hiding this comment

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

LGTM

@OCA-git-bot
Copy link
Contributor

This PR has the approved label and has been created more than 5 days ago. It should therefore be ready to merge by a maintainer (or a PSC member if the concerned addon has no declared maintainer). 🤖

@cyrilmanuel
Copy link

Hi @pedrobaeza can you merge this PR please ?

@pedrobaeza
Copy link
Member

/ocabot migration sale_loyalty_auto_refresh

@chienandalu do you see it correct?

@OCA-git-bot OCA-git-bot added this to the 16.0 milestone Mar 27, 2024
@OCA-git-bot OCA-git-bot mentioned this pull request Mar 27, 2024
15 tasks
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 for your work @nguyenminhchien 🙂 Just some comments:

.get_param("sale_coupon_auto_refresh.%s_triggers" % (self._table), "")
.get_param("sale_loyalty_auto_refresh.%s_triggers" % (self._table), "")
Copy link
Member

Choose a reason for hiding this comment

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

chore: there should be a migration script renaming this parameter

Comment on lines 42 to 45
def _auto_refresh_coupons(self):
orders = self.filtered(type(self)._allow_recompute_coupon_lines)
if orders:
orders = orders.with_context(skip_auto_refresh_coupons=True)
orders.recompute_coupon_lines()
orders.action_open_reward_wizard()
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure about this change of behavior. Why not using the new _update_programs_and_rewards method?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i tried calling it, but it didn't work as expected. anw, i made some changes to deal with the case there was more than 1 applicable promotion. please review.

Comment on lines 1 to 6
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo.tests import new_test_user, tagged

from odoo.addons.sale_loyalty.tests.common import TestSaleCouponCommon
Copy link
Member

Choose a reason for hiding this comment

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

It'd be nice to have a diff here. Aside from that, at first glance I see lot of changes:

  • Copyright and licences aren't respected from the original
  • Tests cases are trimmed so you're not testing the same effects as before, so avoiding regression isn't granted or hard to tell...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry about the Copyright part, i may remove it by mistake.
I just add 1 more unit test to check the case of having more than 1 applicable promotion.

@nguyenminhchien nguyenminhchien force-pushed the 16.0-mig-sale_coupon_auto_refresh branch 2 times, most recently from 321bcd8 to 0fb6824 Compare March 27, 2024 10:57
@cyrilmanuel
Copy link

Hello @chienandalu is it good for you ? :)

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 @nguyenminhchien some comments / doubts:

Copy link
Member

Choose a reason for hiding this comment

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

The test cases are completely changed and can't see a clear reason why...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

because the models of coupon and promotion were changed dramatically. so i trimmed the test to make it more focus on testing the applying of the promotion auto, than re-test the promotion flow, which has been done by sale_loyalty. anw, i'm going to roll it back.

Comment on lines 47 to 51
self_ctx = self.with_context(skip_auto_refresh_coupons=True)
for order in self_ctx:
if order._allow_recompute_coupon_lines():
order._update_programs_and_rewards()
order.action_apply_rewards()
Copy link
Member

Choose a reason for hiding this comment

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

There's no real need to change the previous structure. Yo can:

Suggested change
self_ctx = self.with_context(skip_auto_refresh_coupons=True)
for order in self_ctx:
if order._allow_recompute_coupon_lines():
order._update_programs_and_rewards()
order.action_apply_rewards()
orders = self.with_context(skip_auto_refresh_coupons=True).filtered(type(self)._allow_recompute_coupon_lines)
for order in orders:
order._update_programs_and_rewards()
order.action_apply_rewards()

Comment on lines 61 to 62
# Ignore exception errors to unblock the user when creating/writing
logger.warning(e)
Copy link
Member

Choose a reason for hiding this comment

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

This is going to be really noisy. I you need to do it, better just log it in debug mode.

@nguyenminhchien nguyenminhchien force-pushed the 16.0-mig-sale_coupon_auto_refresh branch 2 times, most recently from 7aca846 to f4d66d1 Compare April 4, 2024 11:41
@cyrilmanuel
Copy link

@chienandalu is it good for you ?

Copy link

@simahawk simahawk left a comment

Choose a reason for hiding this comment

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

Non blocking comments, only code review.
@chienandalu ping :)


self_ctx = self.with_context(skip_auto_refresh_coupons=True)
lines = super(SaleOrderLine, self_ctx).create(vals_list)
lines.mapped("order_id")._auto_refresh_coupons()

Choose a reason for hiding this comment

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

Suggested change
lines.mapped("order_id")._auto_refresh_coupons()
lines.order_id._auto_refresh_coupons()

Comment on lines 66 to 67
"""Returns whether reward lines in order ``self`` can be recomputed
automatically.

Choose a reason for hiding this comment

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

Suggested change
"""Returns whether reward lines in order ``self`` can be recomputed
automatically.
"""
Check if reward lines in ``self`` can be recomputed automatically.

return super().write(vals)

old_data = self._read_recs_data()
old_orders = self.mapped("order_id")

Choose a reason for hiding this comment

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

Suggested change
old_orders = self.mapped("order_id")
old_orders = self.order_id

self_ctx = self.with_context(skip_auto_refresh_coupons=True)
res = super(SaleOrderLine, self_ctx).write(vals)
new_data = self._read_recs_data()
new_orders = self.mapped("order_id")

Choose a reason for hiding this comment

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

Suggested change
new_orders = self.mapped("order_id")
new_orders = self.order_id

if self._check_skip_refresh():
return super().unlink()

orders = self.mapped("order_id")

Choose a reason for hiding this comment

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

Suggested change
orders = self.mapped("order_id")
orders = self.order_id

#. In every add the fields seperated by commas that you want to add to the recomputation
triggers.

⚠️ After configuring or removing a trigger a restart of Odoo is recommended so the

Choose a reason for hiding this comment

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

I don't understand this statement. Either is required or is not. I'd remove it if not necessary because it's confusing.
I left a comment on the compute method.

)
return {x for x in additional_triggers if x in self._fields}

@api.depends(lambda self: list(self._get_auto_refresh_coupons_triggers()))
Copy link

@simahawk simahawk May 22, 2024

Choose a reason for hiding this comment

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

Are we sure this complexity w/ the check on new triggers is necessary?
Data:

  • the field you compute is non stored -> will get recomputed at every request and you can invalidate caching if needed
  • any change on the config param won't have any effect on this depends
  • in fact what you are doing is to load the triggers at need (eg: create/write)

Wouldn't be better to have an empty api.depends?

@santostelmo
Copy link

@chienandalu ping

@santostelmo
Copy link

santostelmo commented Jun 28, 2024

@SilvioC2C maybe you can help on the review / merge of this PR as it's been under review for some time.

try:
self._apply_program_reward(reward, coupon)
self._update_programs_and_rewards()
except Exception as e:

Choose a reason for hiding this comment

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

Isn't it a too broad except ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, but here, i want to ignore all exception errors to unblock the user when creating/writing

Copy link
Contributor Author

Choose a reason for hiding this comment

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

anw, i updated to catch UserError, ValidationError only.

Copy link
Contributor

@SilvioC2C SilvioC2C left a comment

Choose a reason for hiding this comment

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

Overall LGTM, a few comments here and there

[("key", "like", "sale_coupon_auto_refresh.")]
)
for parameter in parameters:
parameter.key = (parameter.key).replace(
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
parameter.key = (parameter.key).replace(
parameter.key = parameter.key.replace(

Copy link
Contributor Author

Choose a reason for hiding this comment

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

just curious but what's different or benefit of this suggested change?

Copy link
Contributor

Choose a reason for hiding this comment

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

Better readability 😄 parenthesis are redundant in this case

Comment on lines +51 to +53
order._update_programs_and_rewards()
order.action_apply_rewards()
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't _update_programs_and_rewards() already called by action_apply_rewards() internally? Do we need this double call?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, we do. The first call to updates applied with the current state of the order. Then, after calling _apply_program_reward(), need to call it again.

try:
self._apply_program_reward(reward, coupon)
self._update_programs_and_rewards()
except Exception as e:
Copy link
Contributor

Choose a reason for hiding this comment

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

Please catch any possible psycopg2 exception and let them be raised, it'd be better to not ignore DB-level errors.

)._update_programs_and_rewards()


class SaleOrderLine(models.Model):
Copy link
Contributor

Choose a reason for hiding this comment

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

Could be worth it to move SaleOrderLine to its own file sale_order_line.py.

Choose a reason for hiding this comment

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

Well I've seen different approaches regarding that. Some people prefer to keep it in the parent object file (sale_order.py). I am okay to keep like that. But move SaleOrderLine to his own file it's an option as well

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, usually if I have something like:

from odoo import fields, models


class MyModel(models.Model):
    _name = "my.model"

    name = fields.Char()
    line_ids = fields.One2many("my.model.line", "rec_id")


class MyModel(models.Model):
    _name = "my.model"

    descr = fields.Char()
    rec_id = fields.Many2one("my.model")

then it can make sense to have both classes in the same file.

But in this module's case, file's ~150 LoC already, and both classes contain business logic... might as well separate them for improved readability.

OCA-git-bot and others added 15 commits June 28, 2024 17:08
Currently translated at 6.2% (1 of 16 strings)

Translation: sale-promotion-14.0/sale-promotion-14.0-sale_coupon_auto_refresh
Translate-URL: https://translation.odoo-community.org/projects/sale-promotion-14-0/sale-promotion-14-0-sale_coupon_auto_refresh/it/
This can allow us to work altogether with flexible triggers like the
ones we could need with sale_coupon_criteria_order_based

TT37479
We should not refresh the lines before the proccess of applying the
coupon is finished.

TT38806
Use the new `get_depends()` method as the former `depends` attribute
(now in `_depends`) is no longer reliable.
Updated by "Update PO files to match POT (msgmerge)" hook in Weblate.

Translation: sale-promotion-15.0/sale-promotion-15.0-sale_coupon_auto_refresh
Translate-URL: https://translation.odoo-community.org/projects/sale-promotion-15-0/sale-promotion-15-0-sale_coupon_auto_refresh/
Currently translated at 100.0% (13 of 13 strings)

Translation: sale-promotion-15.0/sale-promotion-15.0-sale_coupon_auto_refresh
Translate-URL: https://translation.odoo-community.org/projects/sale-promotion-15-0/sale-promotion-15-0-sale_coupon_auto_refresh/es/
@nguyenminhchien nguyenminhchien force-pushed the 16.0-mig-sale_coupon_auto_refresh branch from f4d66d1 to e48ae33 Compare June 28, 2024 10:08
@nguyenminhchien nguyenminhchien force-pushed the 16.0-mig-sale_coupon_auto_refresh branch from e48ae33 to fb20963 Compare June 28, 2024 10:15
@cyrilmanuel
Copy link

Hi @rousseldenis is it good for you ?

Copy link

There hasn't been any activity on this pull request in the past 4 months, so it has been marked as stale and it will be closed automatically if no further activity occurs in the next 30 days.
If you want this PR to never become stale, please ask a PSC member to apply the "no stale" label.

@github-actions github-actions bot added the stale PR/Issue without recent activity, it'll be soon closed automatically. label Nov 10, 2024
@santostelmo
Copy link

@simahawk is possible to merge that one ? 4 approvals

@simahawk
Copy link

/ocabot merge nobump

@OCA-git-bot
Copy link
Contributor

On my way to merge this fine PR!
Prepared branch 16.0-ocabot-merge-pr-208-by-simahawk-bump-nobump, awaiting test results.

@OCA-git-bot OCA-git-bot merged commit a0a3dbb into OCA:16.0 Nov 12, 2024
4 of 7 checks passed
@OCA-git-bot
Copy link
Contributor

Congratulations, your PR was merged at d64c8a4. 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
Labels
merged 🎉 stale PR/Issue without recent activity, it'll be soon closed automatically.
Projects
None yet
Development

Successfully merging this pull request may close these issues.