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

[13.0][MIG] partner_email_check: Migration to 13.0 #989

Merged
merged 16 commits into from
Jul 8, 2021
Merged
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
99 changes: 99 additions & 0 deletions partner_email_check/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
====================
Email Format Checker
====================

.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fpartner--contact-lightgray.png?logo=github
:target: https://github.com/OCA/partner-contact/tree/12.0/partner_email_check
:alt: OCA/partner-contact
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/partner-contact-12-0/partner-contact-12-0-partner_email_check
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png
:target: https://runbot.odoo-community.org/runbot/134/12.0
:alt: Try me on Runbot

|badge1| |badge2| |badge3| |badge4| |badge5|

This module validates and normalizes the field ``email`` in the model
``res.partner``.

As part of the normalization, email addresses are converted to lowercase.

Optionally, multiple partners can not be allowed to have the same address.
This will not work with multiple comma-separated email addresses in the field,
although validation and normalization are still supported in such cases.

**Table of contents**

.. contents::
:local:

Configuration
=============

Install python package email-validator: ``sudo pip install email-validator``.

To not allow multiple partners to have the same email address, use the
"Filter duplicate email addresses"/``partner_email_check_filter_duplicates``
setting.

To validate that email addresses are deliverable (that the hostname exists),
use the "Check deliverability of email addresses"/``partner_email_check_check_deliverability``
setting.

Usage
=====

This module integrate automatically in all of the view ``res.partner``

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/partner-contact/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us smashing it by providing a detailed and welcomed
`feedback <https://github.com/OCA/partner-contact/issues/new?body=module:%20partner_email_check%0Aversion:%2012.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
~~~~~~~

* Komit

Contributors
~~~~~~~~~~~~

* Vo Hoang Dat <[email protected]>
* Jean-Charles Drubay <[email protected]>

Maintainers
~~~~~~~~~~~

This module is maintained by the OCA.

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

This module is part of the `OCA/partner-contact <https://github.com/OCA/partner-contact/tree/12.0/partner_email_check>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
3 changes: 3 additions & 0 deletions partner_email_check/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from . import models
18 changes: 18 additions & 0 deletions partner_email_check/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2019 Komit <https://komit-consulting.com>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

{
"name": "Email Format Checker",
"version": "13.0.1.0.0",
"summary": "Validate email address field",
"author": "Komit, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/partner-contact",
"category": "Tools",
"depends": ["base_setup"],
"installable": True,
"application": False,
"license": "AGPL-3",
"external_dependencies": {"python": ["email-validator"]},
"data": ["views/base_config_view.xml"],
"demo": ["demo/res_company_demo.xml"],
}
9 changes: 9 additions & 0 deletions partner_email_check/demo/res_company_demo.xml
Original file line number Diff line number Diff line change
@@ -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>
84 changes: 84 additions & 0 deletions partner_email_check/i18n/partner_email_check.pot
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * partner_email_check
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 13.0\n"
"Report-Msgid-Bugs-To: \n"
"Last-Translator: <>\n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: \n"
"Plural-Forms: \n"

#. module: partner_email_check
#: code:addons/partner_email_check/models/res_partner.py:65
#, python-format
msgid "%s is an invalid email"
msgstr ""

#. module: partner_email_check
#: code:addons/partner_email_check/models/res_partner.py:69
#, python-format
msgid "Cannot deliver to email address %s"
msgstr ""

#. module: partner_email_check
#: model:ir.model.fields,field_description:partner_email_check.field_res_config_settings__partner_email_check_check_deliverability
msgid "Check deliverability of email addresses"
msgstr ""

#. module: partner_email_check
#: model:ir.model,name:partner_email_check.model_res_config_settings
msgid "Config Settings"
msgstr ""

#. module: partner_email_check
#: model:ir.model,name:partner_email_check.model_res_partner
msgid "Contact"
msgstr ""

#. module: partner_email_check
#: model:ir.model.fields,help:partner_email_check.field_res_config_settings__partner_email_check_check_deliverability
msgid "Don't allow email addresses with providers that don't exist"
msgstr ""

#. module: partner_email_check
#: model:ir.model.fields,help:partner_email_check.field_res_config_settings__partner_email_check_filter_duplicates
msgid "Don't allow multiple partners to have the same email address."
msgstr ""

#. module: partner_email_check
#: code:addons/partner_email_check/models/res_partner.py:48
#, python-format
msgid "Email '%s' is already in use."
msgstr ""

#. module: partner_email_check
#: model_terms:ir.ui.view,arch_db:partner_email_check.view_general_configuration
msgid "Email validation"
msgstr ""

#. module: partner_email_check
#: model_terms:ir.ui.view,arch_db:partner_email_check.view_general_configuration
msgid "Ensure that partner email addresses can be delivered to"
msgstr ""

#. module: partner_email_check
#: code:addons/partner_email_check/models/res_partner.py:40
#, python-format
msgid "Field contains multiple email addresses. This is not supported when duplicate email addresses are not allowed."
msgstr ""

#. module: partner_email_check
#: model:ir.model.fields,field_description:partner_email_check.field_res_config_settings__partner_email_check_filter_duplicates
msgid "Filter duplicate partner email addresses"
msgstr ""

#. module: partner_email_check
#: model_terms:ir.ui.view,arch_db:partner_email_check.view_general_configuration
msgid "Require partner email addresses to be unique"
msgstr ""

24 changes: 24 additions & 0 deletions partner_email_check/migrations/13.0.1.0.0/post-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2021 ACSONE SA/NV
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from openupgradelib import openupgrade


@openupgrade.migrate(use_env=True)
def migrate(env, version):
param_duplicate = env["ir.config_parameter"].search(
[("key", "=", "partner_email_check_filter_duplicates")]
)
if param_duplicate:
env["res.company"].search([]).write(
{"partner_email_check_filter_duplicates": param_duplicate.value}
)
param_duplicate.unlink()
param_deliverability = env["ir.config_parameter"].search(
[("key", "=", "partner_email_check_check_deliverability")]
)
if param_deliverability:
env["res.company"].search([]).write(
{"partner_email_check_check_deliverability": param_deliverability.value}
)
param_deliverability.unlink()
5 changes: 5 additions & 0 deletions partner_email_check/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from . import res_company
from . import res_config_settings
from . import res_partner
23 changes: 23 additions & 0 deletions partner_email_check/models/res_company.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright 2021 ACSONE SA/NV
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import fields, models


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.",
)
partner_email_check_check_deliverability = fields.Boolean(
string="Check deliverability of email addresses",
help="Don't allow email addresses with providers that don't exist",
)
24 changes: 24 additions & 0 deletions partner_email_check/models/res_config_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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,
)

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
98 changes: 98 additions & 0 deletions partner_email_check/models/res_partner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Copyright 2019 Komit <https://komit-consulting.com>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

import logging

from odoo import _, api, models
from odoo.exceptions import UserError, ValidationError

_logger = logging.getLogger(__name__)

try:
from email_validator import (
validate_email,
EmailSyntaxError,
EmailUndeliverableError,
)
except ImportError:
_logger.debug('Cannot import "email_validator".')

validate_email = None


class ResPartner(models.Model):
_inherit = "res.partner"

def copy_data(self, default=None):
res = super(ResPartner, self).copy_data(default=default)
if self._should_filter_duplicates():
for copy_vals in res:
copy_vals.pop("email", None)
return res

@api.model
def email_check(self, emails):
return ",".join(
self._normalize_email(email.strip())
for email in emails.split(",")
if email.strip()
)

@api.constrains("email")
def _check_email_unique(self):
if self._should_filter_duplicates():
for rec in self.filtered("email"):
if "," in rec.email:
raise UserError(
_(
"Field contains multiple email addresses. This is "
"not supported when duplicate email addresses are "
"not allowed."
)
)
if self.search_count([("email", "=", rec.email), ("id", "!=", rec.id)]):
raise UserError(
_("Email '%s' is already in use.") % rec.email.strip()
)

def _normalize_email(self, email):
if not self._should_check_syntax():
return email
if validate_email is None:
_logger.warning(
"Can not validate email, "
'python dependency required "email_validator"'
)
return email

try:
result = validate_email(
email, check_deliverability=self._should_check_deliverability(),
)
except EmailSyntaxError:
raise ValidationError(_("%s is an invalid email") % email.strip())
except EmailUndeliverableError:
raise ValidationError(
_("Cannot deliver to email address %s") % email.strip()
)
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

def _should_check_deliverability(self):
return self.env.company.partner_email_check_check_deliverability

@api.model
def create(self, vals):
if vals.get("email"):
vals["email"] = self.email_check(vals["email"])
return super(ResPartner, self).create(vals)

def write(self, vals):
if vals.get("email"):
vals["email"] = self.email_check(vals["email"])
return super(ResPartner, self).write(vals)
Loading