Skip to content

Commit fcdd57f

Browse files
Merge pull request #484 from OpenSPP/improve-cycle-date-validations
Improved the validations of the dates in cycle
2 parents 113a3b3 + 9f7a496 commit fcdd57f

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

spp_programs/models/cycle.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Part of OpenSPP. See LICENSE file for full copyright and licensing details.
22

3-
from odoo import fields, models
3+
from odoo import _, api, fields, models
4+
from odoo.exceptions import ValidationError
45

56

67
class G2PCycle(models.Model):
@@ -49,3 +50,11 @@ def get_entitlements(
4950
if count:
5051
return self.env["g2p.cycle.membership"].search_count(domain, limit=limit)
5152
return self.env[entitlement_model].search(domain, offset=offset, limit=limit, order=order)
53+
54+
@api.constrains("start_date", "end_date")
55+
def _check_dates(self):
56+
for record in self:
57+
if record.start_date and record.start_date < fields.Date.today():
58+
raise ValidationError(_('The "Start Date" cannot be earlier than today.'))
59+
if record.end_date and record.start_date and record.end_date < record.start_date:
60+
raise ValidationError(_('The "End Date" cannot be earlier than the "Start Date".'))

spp_programs/tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
from . import test_programs
55
from . import test_registrant
66
from . import test_stock_rule
7+
from . import test_cycle

spp_programs/tests/test_cycle.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from freezegun import freeze_time
2+
3+
from odoo.exceptions import ValidationError
4+
5+
from .common import Common
6+
7+
8+
@freeze_time("2024-07-19")
9+
class TestCycle(Common):
10+
def test_check_dates_constrains(self):
11+
with self.assertRaisesRegex(ValidationError, 'The "End Date" cannot be earlier than the "Start Date".'):
12+
self.cycle.write(
13+
{
14+
"end_date": "2024-07-18",
15+
}
16+
)
17+
18+
with self.assertRaisesRegex(ValidationError, 'The "Start Date" cannot be earlier than today.'):
19+
self.cycle.write(
20+
{
21+
"start_date": "2024-07-18",
22+
}
23+
)

0 commit comments

Comments
 (0)