-
-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IMP] membership_[extension,type,custom_date]: Add restraint on date_…
…from and date_to on membership line This was a bit tricky to get right, architecturally, but I think I arrived at the correct design decision. On the whole, the new restriction makes sense. Following from the fact that dates are mandatory on 'fixed dates' (read: default) membership products, and following from the fact that dates are always set on the membership lines of 'variable period' membership products, membership lines should _always_ have values for date_from and date_to. The fact that these fields are not already required is a strange design decision. The constraint in membership_extension _effectively_ sets required=True on these fields. However, there is a use-case for memberships that do not have an end date (using the 'custom' membership product). For those scenarios, we add a toggle on the product (exclusive to 'custom' products) on whether to apply the constraint. Signed-off-by: Carmen Bianca BAKKER <[email protected]>
- Loading branch information
1 parent
9f3eb6c
commit 3078281
Showing
15 changed files
with
250 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
membership_custom_date/models/membership_membership_line.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# SPDX-FileCopyrightText: 2023 Coop IT Easy SC | ||
# | ||
# SPDX-License-Identifier: AGPL-3.0-or-later | ||
|
||
from odoo import api, fields, models | ||
|
||
|
||
class MembershipLine(models.Model): | ||
_inherit = "membership.membership_line" | ||
|
||
dates_mandatory = fields.Boolean(related="membership_id.dates_mandatory") | ||
|
||
@api.constrains( | ||
"membership_id", | ||
"membership_type", | ||
"dates_mandatory", | ||
"date_from", | ||
"date_to", | ||
) | ||
def _check_mandatory_dates(self): | ||
lines = self.filtered( | ||
lambda line: line.membership_type != "custom" or line.dates_mandatory | ||
) | ||
return super(MembershipLine, lines)._check_mandatory_dates() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# SPDX-FileCopyrightText: 2023 Coop IT Easy SC | ||
# | ||
# SPDX-License-Identifier: AGPL-3.0-or-later | ||
|
||
from . import test_membership |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
# SPDX-FileCopyrightText: 2023 Coop IT Easy SC | ||
# | ||
# SPDX-License-Identifier: AGPL-3.0-or-later | ||
|
||
|
||
from odoo import fields | ||
from odoo.exceptions import ValidationError | ||
from odoo.tests.common import Form, TransactionCase | ||
|
||
|
||
class TestMembership(TransactionCase): | ||
@classmethod | ||
def setUpClass(cls): | ||
super().setUpClass() | ||
cls.product = cls.env["product.product"].create( | ||
{ | ||
"type": "service", | ||
"name": "Custom Membership", | ||
"membership": True, | ||
"membership_type": "custom", | ||
"list_price": 100, | ||
"dates_mandatory": True, | ||
} | ||
) | ||
cls.partner = cls.env["res.partner"].create({"name": "Test Partner"}) | ||
|
||
def test_mandatory_dates(self): | ||
with self.assertRaises(ValidationError): | ||
self.env["membership.membership_line"].create( | ||
{ | ||
"membership_id": self.product.id, | ||
"member_price": 100.00, | ||
"date": fields.Date.today(), | ||
# No dates | ||
"partner": self.partner.id, | ||
"state": "waiting", | ||
} | ||
) | ||
self.product.dates_mandatory = False | ||
self.env["membership.membership_line"].create( | ||
{ | ||
"membership_id": self.product.id, | ||
"member_price": 100.00, | ||
"date": fields.Date.today(), | ||
# No dates | ||
"partner": self.partner.id, | ||
"state": "waiting", | ||
} | ||
) | ||
|
||
def test_check_membership_dates_required(self): | ||
invoice_form = Form( | ||
self.env["membership.invoice"].with_context( | ||
default_partner_id=self.partner.id | ||
) | ||
) | ||
invoice_form.product_id = self.product | ||
|
||
message = "is a required field" | ||
|
||
invoice_form.date_from = False | ||
invoice_form.date_to = False | ||
with self.assertRaisesRegex(AssertionError, message): | ||
invoice_form.save() | ||
|
||
invoice_form.date_from = "2023-01-01" | ||
invoice_form.date_to = False | ||
with self.assertRaisesRegex(AssertionError, message): | ||
invoice_form.save() | ||
|
||
invoice_form.date_from = False | ||
invoice_form.date_to = "2023-12-31" | ||
with self.assertRaisesRegex(AssertionError, message): | ||
invoice_form.save() | ||
|
||
invoice_form.date_from = "2023-01-01" | ||
invoice_form.date_to = "2023-12-31" | ||
invoice_form.save() | ||
|
||
def test_check_membership_invoice_check_dates(self): | ||
wizard = self.env["membership.invoice"].create( | ||
{ | ||
"product_id": self.product.id, | ||
"member_price": 100, | ||
} | ||
) | ||
wizard = wizard.with_context( | ||
default_partner_id=self.partner.id, | ||
active_ids=self.partner.id, | ||
) | ||
|
||
self.assertEqual(wizard.product_id, self.product) | ||
|
||
message = "start and end dates are mandatory" | ||
|
||
# already the case, but to be explicit here | ||
wizard.date_from = False | ||
wizard.date_to = False | ||
with self.assertRaisesRegex(ValidationError, message): | ||
wizard.membership_invoice() | ||
|
||
wizard.date_from = "2023-01-01" | ||
wizard.date_to = False | ||
with self.assertRaisesRegex(ValidationError, message): | ||
wizard.membership_invoice() | ||
|
||
wizard.date_from = False | ||
wizard.date_to = "2023-12-31" | ||
with self.assertRaisesRegex(ValidationError, message): | ||
wizard.membership_invoice() | ||
|
||
wizard.date_from = "2023-01-01" | ||
wizard.date_to = "2023-12-31" | ||
wizard.membership_invoice() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<!-- | ||
SPDX-FileCopyrightText: 2023 Coop IT Easy SC | ||
SPDX-License-Identifier: AGPL-3.0-or-later | ||
--> | ||
<odoo> | ||
<record id="membership_products_form_period" model="ir.ui.view"> | ||
<field name="name">Membership Products (custom type)</field> | ||
<field name="model">product.template</field> | ||
<field name="inherit_id" ref="membership_type.membership_products_form" /> | ||
<field name="arch" type="xml"> | ||
<field name="membership_type" position="after"> | ||
<field | ||
name="dates_mandatory" | ||
attrs="{'invisible': [('membership_type', '!=', 'custom')]}" | ||
/> | ||
</field> | ||
</field> | ||
</record> | ||
</odoo> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
# Copyright 2016 Antonio Espinosa <[email protected]> | ||
# Copyright 2017 David Vidal <[email protected]> | ||
# Copyright 2019 Onestein - Andrea Stirpe | ||
# Copyright 2023 Coop IT Easy SC | ||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). | ||
|
||
from datetime import timedelta | ||
|
||
from odoo import _, api, fields, models | ||
from odoo.exceptions import UserError | ||
from odoo.exceptions import UserError, ValidationError | ||
|
||
|
||
class MembershipLine(models.Model): | ||
|
@@ -35,6 +36,20 @@ class MembershipLine(models.Model): | |
), | ||
] | ||
|
||
@api.constrains("date_from", "date_to") | ||
def _check_mandatory_dates(self): | ||
# This is a rather dirty mechanism to skip this check. It can be used | ||
# when creating a membership line and later writing date_from and | ||
# date_to to it in the same workflow. It is not used in this module an | ||
# sich. | ||
if self.env.context.get("skip_mandatory_dates"): | ||
return | ||
for line in self: | ||
if not line.date_from or not line.date_to: | ||
raise ValidationError( | ||
_("Error: the start and end dates are mandatory.") | ||
) | ||
|
||
@api.depends("membership_id") | ||
def _compute_member_price(self): | ||
for partner in self: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# SPDX-FileCopyrightText: 2023 Coop IT Easy SC | ||
# | ||
# SPDX-License-Identifier: AGPL-3.0-or-later | ||
|
||
from odoo import fields, models | ||
|
||
|
||
class MembershipLine(models.Model): | ||
_inherit = "membership.membership_line" | ||
|
||
membership_type = fields.Selection(related="membership_id.membership_type") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters