Browse Source

[IMP] Add partner email check syntax flag

14.0
Quentin Groulard 3 years ago
committed by Zar21
parent
commit
8125ed13cc
  1. 1
      partner_email_check/__manifest__.py
  2. 9
      partner_email_check/demo/res_company_demo.xml
  3. 5
      partner_email_check/models/res_company.py
  4. 13
      partner_email_check/models/res_config_settings.py
  5. 10
      partner_email_check/models/res_partner.py
  6. 33
      partner_email_check/tests/test_partner_email_check.py
  7. 18
      partner_email_check/views/base_config_view.xml

1
partner_email_check/__manifest__.py

@ -14,4 +14,5 @@
"license": "AGPL-3",
"external_dependencies": {"python": ["email-validator"]},
"data": ["views/base_config_view.xml"],
"demo": ["demo/res_company_demo.xml"],
}

9
partner_email_check/demo/res_company_demo.xml

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2021 ACSONE SA/NV
License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -->
<odoo>
<function model="res.company" name="write">
<value model="res.company" search="[]" />
<value eval="{'partner_email_check_syntax': False}" />
</function>
</odoo>

5
partner_email_check/models/res_company.py

@ -8,6 +8,11 @@ class ResCompany(models.Model):
_inherit = "res.company"
partner_email_check_syntax = fields.Boolean(
string="Check syntax of email addresses",
help="Don't allow email addresses with wrong syntax",
default=True,
)
partner_email_check_filter_duplicates = fields.Boolean(
string="Filter duplicate partner email addresses",
help="Don't allow multiple partners to have the same email address.",

13
partner_email_check/models/res_config_settings.py

@ -1,9 +1,13 @@
from odoo import fields, models
from odoo import api, fields, models
class ResConfigSettings(models.TransientModel):
_inherit = "res.config.settings"
partner_email_check_syntax = fields.Boolean(
related="company_id.partner_email_check_syntax", readonly=False,
)
partner_email_check_filter_duplicates = fields.Boolean(
related="company_id.partner_email_check_filter_duplicates", readonly=False,
)
@ -11,3 +15,10 @@ class ResConfigSettings(models.TransientModel):
partner_email_check_check_deliverability = fields.Boolean(
related="company_id.partner_email_check_check_deliverability", readonly=False,
)
@api.onchange(
"partner_email_check_syntax", "partner_email_check_check_deliverability"
)
def _onchange_partner_email_check_check_deliverability(self):
if self.partner_email_check_check_deliverability:
self.partner_email_check_syntax = True

10
partner_email_check/models/res_partner.py

@ -5,7 +5,6 @@ import logging
from odoo import _, api, models
from odoo.exceptions import UserError, ValidationError
from odoo.tools import config
_logger = logging.getLogger(__name__)
@ -33,10 +32,6 @@ class ResPartner(models.Model):
@api.model
def email_check(self, emails):
if config["test_enable"] and not self.env.context.get(
"test_partner_email_check"
):
return emails
return ",".join(
self._normalize_email(email.strip())
for email in emails.split(",")
@ -61,6 +56,8 @@ class ResPartner(models.Model):
)
def _normalize_email(self, email):
if not self._should_check_syntax():
return email
if validate_email is None:
_logger.warning(
"Can not validate email, "
@ -80,6 +77,9 @@ class ResPartner(models.Model):
)
return result["local"].lower() + "@" + result["domain_i18n"]
def _should_check_syntax(self):
return self.env.company.partner_email_check_syntax
def _should_filter_duplicates(self):
return self.env.company.partner_email_check_filter_duplicates

33
partner_email_check/tests/test_partner_email_check.py

@ -13,11 +13,8 @@ class TestPartnerEmailCheck(SavepointCase):
def setUpClass(cls):
super().setUpClass()
cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True))
# Checks are disabled during tests unless this key is set
cls.res_partner = cls.env["res.partner"].with_context(
test_partner_email_check=True
)
cls.test_partner = cls.res_partner.create({"name": "test"})
cls.test_partner = cls.env["res.partner"].create({"name": "test"})
cls.env.company.partner_email_check_syntax = True
cls.env.company.partner_email_check_filter_duplicates = False
cls.env.company.partner_email_check_check_deliverability = False
@ -76,11 +73,15 @@ class TestPartnerEmailCheck(SavepointCase):
self.disallow_duplicates()
self.test_partner.write({"email": "email@domain.tld"})
with self.assertRaises(UserError):
self.res_partner.create({"name": "alsotest", "email": "email@domain.tld"})
self.env["res.partner"].create(
{"name": "alsotest", "email": "email@domain.tld"}
)
def test_duplicate_after_normalization_addresses_disallowed(self):
self.disallow_duplicates()
self.res_partner.create({"name": "alsotest", "email": "email@doMAIN.tld"})
self.env["res.partner"].create(
{"name": "alsotest", "email": "email@doMAIN.tld"}
)
with self.assertRaises(UserError):
self.test_partner.email = "email@domain.tld"
@ -96,7 +97,9 @@ class TestPartnerEmailCheck(SavepointCase):
self.assertFalse(partner_copy.email)
def test_duplicate_addresses_allowed_by_default(self):
self.res_partner.create({"name": "alsotest", "email": "email@domain.tld"})
self.env["res.partner"].create(
{"name": "alsotest", "email": "email@domain.tld"}
)
self.test_partner.email = "email@domain.tld"
def check_deliverability(self):
@ -129,13 +132,13 @@ class TestPartnerEmailCheck(SavepointCase):
with patch(
"odoo.addons.partner_email_check.models.res_partner." "validate_email", None
):
self.res_partner.create({"name": "alsotest", "email": "email@domain.tld"})
self.env["res.partner"].create(
{"name": "alsotest", "email": "email@domain.tld"}
)
with self.assertRaises(UserError):
self.test_partner.email = "email@domain.tld"
def test_invalid_email_addresses_allowed_during_tests(self):
# Note: testing without test_partner_email_check in the context
new_partner = self.env["res.partner"].create(
{"name": "invalidly emailed", "email": "invalid"}
)
self.assertEqual("invalid", new_partner.email)
def test_invalid_email_addresses_allowed(self):
self.env.company.partner_email_check_syntax = False
self.test_partner.email = "bad@email@domain..com"
self.assertTrue(self.test_partner.email)

18
partner_email_check/views/base_config_view.xml

@ -8,6 +8,24 @@
<xpath expr="//div[@id='companies']" position='after'>
<h2>Email validation</h2>
<div class="row mt16 o_settings_container" name="partner_email_check">
<div class="col-xs-12 col-md-6 o_setting_box">
<div class="o_setting_left_pane">
<field name="partner_email_check_syntax" />
</div>
<div class="o_setting_right_pane">
<label for="partner_email_check_syntax" />
<span
class="fa fa-lg fa-building-o"
title="Values set here are company-specific."
aria-label="Values set here are company-specific."
groups="base.group_multi_company"
role="img"
/>
<div class="text-muted">
Require partner email addresses to have valid syntax
</div>
</div>
</div>
<div class="col-xs-12 col-md-6 o_setting_box">
<div class="o_setting_left_pane">
<field name="partner_email_check_filter_duplicates" />

Loading…
Cancel
Save