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

Fix sporadical errors when contacts do not have a valid email address #1542

Merged
merged 1 commit into from
Feb 14, 2020
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
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions bika/lims/content/person.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
widget=StringWidget(
label=_("Email Address"),
),
validators=("isEmail", )
),

StringField(
Expand Down
50 changes: 50 additions & 0 deletions bika/lims/upgrade/v01_03_003.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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]")