Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crm_date_deadline_required/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
1 change: 1 addition & 0 deletions crm_date_deadline_required/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import crm_lead
23 changes: 23 additions & 0 deletions crm_date_deadline_required/models/crm_lead.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from odoo import _, api, models
from odoo.exceptions import ValidationError


class CrmLead(models.Model):
_inherit = "crm.lead"

@api.constrains("date_deadline", "type")
def _check_date_deadline_required(self):
for record in self:
if record.type == "opportunity" and not record.date_deadline:
raise ValidationError(
_("The expected closing date is required for opportunities.")
)

@api.model_create_multi
def create(self, vals_list):
for vals in vals_list:
if vals.get("type") == "opportunity" and not vals.get("date_deadline"):
raise ValidationError(
_("The expected closing date is required for opportunities.")
)
return super().create(vals_list)
29 changes: 17 additions & 12 deletions crm_date_deadline_required/tests/test_crm_date_deadline_required.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright 2025 Moduon Team S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl-3.0)

from odoo.exceptions import ValidationError
from odoo.tests import Form
from odoo.tests.common import TransactionCase

Expand All @@ -11,30 +12,34 @@ def setUpClass(cls):
super().setUpClass()

def test_crm_date_deadline_required_opportunity(self):
"""Check date_deadline required in opportunity in default form"""
"""Check date_deadline is required in opportunity default form."""
opportunity_form = Form(
self.env["crm.lead"].with_context(default_type="opportunity")
)
opportunity_form.name = "Test Opportunity"
# The Form object raises AssertionError for XML required fields
# before the server-side ValidationError can be triggered.
with self.assertRaises(AssertionError):
opportunity_form.save()
opportunity_form.date_deadline = "2025-01-01"
opportunity_form.save()

def test_crm_date_deadline_required_opportunity_quick_create(self):
"""Check date_deadline required in opportunity in quick create form"""
opportunity_quick_create_form = Form(
self.env["crm.lead"].with_context(default_type="opportunity"),
"crm.quick_create_opportunity_form",
)
opportunity_quick_create_form.name = "Test Opportunity Quick Create"
with self.assertRaises(AssertionError):
opportunity_quick_create_form.save()
opportunity_quick_create_form.date_deadline = "2025-01-01"
opportunity_quick_create_form.save()
"""Check date_deadline is required via server validation for quick create."""
# We test the server constraint directly because the field
# was removed from the quick create view to fix the OWL UI bug.
with self.assertRaises(ValidationError):
self.env["crm.lead"].with_context(default_type="opportunity").create(
{
"name": "Test Opportunity Quick Create",
"type": "opportunity",
# We omit date_deadline to trigger the Python ValidationError
}
)

def test_crm_date_deadline_required_lead(self):
"""Check date_deadline not required in lead in default form"""
"""Check date_deadline is not required for lead types."""
lead_form = Form(self.env["crm.lead"].with_context(default_type="lead"))
lead_form.name = "Test Lead"
# Should save without errors as it's not an opportunity
lead_form.save()
13 changes: 0 additions & 13 deletions crm_date_deadline_required/views/crm_lead_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,4 @@
</xpath>
</field>
</record>
<record id="quick_create_opportunity_form_inherit" model="ir.ui.view">
<field name="name">Date deadline required quick create form</field>
<field name="model">crm.lead</field>
<field name="inherit_id" ref="crm.quick_create_opportunity_form" />
<field name="arch" type="xml">
<xpath expr="//field[@name='phone']" position="after">
<field
name="date_deadline"
attrs="{'required': [('type', '=', 'opportunity')]}"
/>
</xpath>
</field>
</record>
</odoo>
Loading