Skip to content

Commit

Permalink
Merge pull request #94 from senaite/fix-empty-spaces
Browse files Browse the repository at this point in the history
Fix Patient Medical Record must be unique error when MRN has leading/traling whitespaces
  • Loading branch information
ramonski authored Nov 2, 2023
2 parents 6b9954f + 905fce5 commit f9bacb5
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/senaite/patient/browser/widgets/temporaryidentifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def process_form(self, instance, field, form, empty_marker=None,

value = {
"temporary": temporary,
"value": identifier,
"value": identifier.strip(),
"value_auto": autogenerated,
}
return value, {}
Expand Down
2 changes: 1 addition & 1 deletion src/senaite/patient/profiles/default/metadata.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<metadata>
<version>1415</version>
<version>1416</version>
<dependencies>
<dependency>profile-senaite.lims:default</dependency>
</dependencies>
Expand Down
53 changes: 53 additions & 0 deletions src/senaite/patient/upgrade/v01_04_000.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,3 +552,56 @@ def allow_searches_by_patient_in_samples(tool):
cat = api.get_tool(SAMPLE_CATALOG)
cat.reindexIndex(["listing_searchable_text"], None, ZLogHandler())
logger.info("Allowing searches by patient in samples [DONE]")


def remove_whitespaces_mrn(tool):
"""Resets the date of birth from samples
"""
logger.info("Removing lead and trailing whitespaces from MRN ...")

uc = api.get_tool("uid_catalog")
brains = uc(portal_type="AnalysisRequest")
total = len(brains)

for num, brain in enumerate(brains):
if num and num % 100 == 0:
logger.info("Migrated {0}/{1} fields".format(num, total))

if num and num % 1000 == 0:
# reduce memory size of the transaction
transaction.savepoint()

try:
obj = api.get_object(brain)
except AttributeError:
uncatalog_brain(brain)
continue

if not obj:
continue

mrn = obj.getField("MedicalRecordNumber").get(obj)
if not mrn:
# Flush the object from memory
obj._p_deactivate()
continue

value = mrn.get("value")
if not value:
# Flush the object from memory
obj._p_deactivate()
continue

stripped = value.strip()
if stripped == value:
obj._p_deactivate()
continue

# Reset the value
mrn["value"] = stripped
obj.getField("MedicalRecordNumber").set(obj, mrn)
# reindex so indexes and columns are updated in accordance
obj.reindexObject()
obj._p_deactivate()

logger.info("Removing lead and trailing whitespaces from MRN [DONE]")
14 changes: 14 additions & 0 deletions src/senaite/patient/upgrade/v01_04_000.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@
xmlns="http://namespaces.zope.org/zope"
xmlns:genericsetup="http://namespaces.zope.org/genericsetup">

<!-- 1416: Allow searches by patient in samples -->
<genericsetup:upgradeStep
title="Remove lead and trailing whitespaces from MRN"
description="
This upgrade step walks through all samples and removes trailing and
leading whitespaces from Medical Record Number fields. This is
necessary because when creating a Patient object, the MRN is
automatically stripped in setMRN function, although is left without
changes in AnalysisRequest."
source="1415"
destination="1416"
handler=".v01_04_000.remove_whitespaces_mrn"
profile="senaite.patient:default"/>

<!-- 1415: Allow searches by patient in samples -->
<genericsetup:upgradeStep
title="SENAITE PATIENT 1.4.0: Allow patient searches in samples"
Expand Down

0 comments on commit f9bacb5

Please sign in to comment.