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 samples w/o active analyses are displayed under "unassigned" filter #2066

Merged
merged 5 commits into from
Jul 26, 2022
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 @@ -5,6 +5,7 @@ Changelog
2.3.0 (unreleased)
------------------

- #2066 Fix samples w/o active analyses are displayed under "unassigned" filter
- #2065 Fix "Create Worksheet" modal visible for samples w/o unassigned analyses
- #2063 Allow to customize email publication template in setup
- #2062 Fix listing not updated after instrument assignment in Worksheet's view
Expand Down
59 changes: 48 additions & 11 deletions src/senaite/core/catalog/indexer/sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,56 @@

@indexer(IAnalysisRequest)
def assigned_state(instance):
"""Returns `assigned` or `unassigned` depending on the state of the
analyses the analysisrequest contains. Return `unassigned` if the Analysis
Request has at least one analysis in `unassigned` state.
Otherwise, returns `assigned`
"""Returns `assigned`, `unassigned` or 'not_applicable' depending on the
state of the analyses the analysisrequest contains. Return `unassigned` if
the Analysis Request has at least one 'active' analysis in `unassigned`
status. Returns 'assigned' if all 'active' analyses of the sample are
assigned to a Worksheet. Returns 'not_applicable' if no 'active' analyses
for the given sample exist
"""
analyses = instance.getAnalyses()
if not analyses:
return "unassigned"
for analysis in analyses:
analysis_object = api.get_object(analysis)
if not analysis_object.getWorksheet():
assigned = False
skip_statuses = ["retracted", "rejected", "cancelled"]

# Retrieve analyses directly from the instance container instead of relying
# on ARAnalysesField getter, that performs a catalog query. Reason is, that
# we never know if the sample is indexed before the analyses or any other
# dependent catalog
for analysis in instance.objectValues(spec="Analysis"):
status = api.get_review_status(analysis)

if status == "unassigned":
# One unassigned found, no need to go further
return "unassigned"
return "assigned"

if status in skip_statuses:
# Skip "inactive" analyses
continue

if analysis.getWorksheetUID():
# At least one analysis with a worksheet assigned
assigned = True

# ARAnalysesField getter returns all the analyses from the sample, those
# from partitions included. Since we do not rely on the getter, we need to
# manually extract the analyses from the partitions
# Pity is, that for the retrieval of partitions we need to rely on
# getBackReferences, that is a query against reference_catalog
for partition in instance.getDescendants():
# Note we call this same index, but for the partition
partition_status = assigned_state(partition)()
if partition_status == "unassigned":
# One of the partitions with unassigned, no need to go further
return "unassigned"

elif partition_status == "assigned":
assigned = True

if assigned:
# All "active" analyses assigned to a worksheet
return "assigned"

# Sample without "active" assigned/unassigned analyses
return "not_applicable"


@indexer(IAnalysisRequest)
Expand Down
28 changes: 28 additions & 0 deletions src/senaite/core/upgrade/v02_03_000.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def upgrade(tool):
fix_worksheets_analyses(portal)
fix_cannot_create_partitions(portal)
fix_interface_interpretation_template(portal)
fix_unassigned_samples(portal)

logger.info("{0} upgraded to version {1}".format(product, version))
return True
Expand Down Expand Up @@ -176,3 +177,30 @@ def fix_interface_interpretation_template(portal):
fti = pt.get("InterpretationTemplate")
fti.schema = "senaite.core.content.interpretationtemplate.IInterpretationTemplateSchema"
logger.info("Fix interface for InterpretationTemplate FTI ...")


def fix_unassigned_samples(portal):
"""Reindex the 'assigned_state' index for samples
"""
logger.info("Fix unassigned samples ...")
indexes = ["assigned_state"]
query = {
"portal_type": "AnalysisRequest",
"assigned_state": "unassigned",
}
cat = api.get_tool(SAMPLE_CATALOG)
samples = api.search(query, SAMPLE_CATALOG)
total = len(samples)
for num, sample in enumerate(samples):

if num and num % 100 == 0:
logger.info("Fix unassigned samples {0}/{1}".format(num, total))

obj = api.get_object(sample)
obj_url = api.get_path(sample)
cat.catalog_object(obj, obj_url, idxs=indexes, update_metadata=1)

# Flush the object from memory
obj._p_deactivate() # noqa

logger.info("Fix unassigned samples ...")