From ae9d1be4d655ea61618a4f1d21f02e9dc9445663 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jordi=20Puiggen=C3=A9?= Date: Fri, 14 Feb 2020 09:48:38 +0100 Subject: [PATCH] Fix sporadical errors when contacts do not have a valid email address --- CHANGES.rst | 1 + bika/lims/content/person.py | 1 + bika/lims/upgrade/v01_03_003.py | 50 +++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index 137a631d51..3d991ddedd 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -52,6 +52,7 @@ Changelog **Fixed** +- #1542 Fix sporadical errors when contacts do not have a valid email address - #1540 Fix flushing CCEmail fields in Sample Add Form - #1533 Fix traceback from log when rendering stickers preview - #1525 Fix error when creating partitions with analyst user diff --git a/bika/lims/content/person.py b/bika/lims/content/person.py index 029d97d5ef..4d2950f153 100644 --- a/bika/lims/content/person.py +++ b/bika/lims/content/person.py @@ -101,6 +101,7 @@ widget=StringWidget( label=_("Email Address"), ), + validators=("isEmail", ) ), StringField( diff --git a/bika/lims/upgrade/v01_03_003.py b/bika/lims/upgrade/v01_03_003.py index bde7cc54d9..6cd860f972 100644 --- a/bika/lims/upgrade/v01_03_003.py +++ b/bika/lims/upgrade/v01_03_003.py @@ -21,9 +21,11 @@ from collections import defaultdict from operator import itemgetter +import re import transaction from bika.lims import api from bika.lims import logger +from bika.lims.api.mail import is_valid_email_address from bika.lims.catalog import CATALOG_ANALYSIS_LISTING from bika.lims.catalog import CATALOG_ANALYSIS_REQUEST_LISTING from bika.lims.catalog.bikasetup_catalog import SETUP_CATALOG @@ -362,6 +364,10 @@ def upgrade(tool): # setup html filtering setup_html_filter(portal) + # Fix email addresses + # https://github.com/senaite/senaite.core/pull/1542 + fix_email_address(portal) + logger.info("{0} upgraded to version {1}".format(product, version)) return True @@ -771,3 +777,47 @@ def update_wf_received_samples(portal): sample.reindexObject() logger.info("Updating workflow mappings for received samples [DONE]") + + +def fix_email_address(portal, portal_types=None, catalog_id="portal_catalog"): + """Validates the email address of portal types that inherit from Person. + The field did not have an email validator, causing some views to fail when + rendering the value while expecting a valid email address format + """ + logger.info("Fixing email addresses ...") + if not portal_types: + portal_types = ["Contact", "LabContact", "SupplierContact"] + query = dict(portal_type=portal_types) + brains = api.search(query, catalog_id) + total = len(brains) + for num, brain in enumerate(brains): + if num and num % 1000 == 0: + logger.info("{}/{} Fixing email addresses ...".format(num, total)) + + obj = api.get_object(brain) + email_address = obj.getEmailAddress() + if not email_address: + continue + + if not is_valid_email_address(email_address): + obj_id = api.get_id(obj) + logger.info("No valid email address for {}: {}" + .format(obj_id, email_address)) + + # Maybe is a list of email addresses + emails = map(lambda x: x.strip(), re.split("[;:, ]", email_address)) + + # Bail out non-valid emails + emails = filter(lambda em: is_valid_email_address(em), emails) + if emails: + email = emails[0] + logger.info("Email address assigned for {}: {}" + .format(api.get_id(obj), email)) + obj.setEmailAddress(email) + obj.reindexObject() + + else: + logger.warn("Cannot resolve email address from '{}'" + .format(email_address)) + + logger.info("Fixing email addresses [DONE]") \ No newline at end of file