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

Hidden settings of analysis services lost on Sample creation #1473

Merged
merged 2 commits into from
Nov 22, 2019
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 @@ -18,6 +18,7 @@ Changelog

**Fixed**

- #1473 Hidden settings of analysis services lost on Sample creation
- #1472 Secondary samples - removal of analysis profile not possible
- #1469 Fix Site Properties Generic Setup Export Step
- #1467 Cannot override behavior of Batch folder when using `before_render`
Expand Down
52 changes: 48 additions & 4 deletions bika/lims/utils/analysisrequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# Copyright 2018-2019 by it's authors.
# Some rights reserved, see README and LICENSE.

import itertools
import os
import tempfile
from email.mime.multipart import MIMEMultipart
Expand All @@ -26,14 +27,19 @@
from Products.CMFCore.utils import getToolByName
from Products.CMFPlone.utils import _createObjectByType
from Products.CMFPlone.utils import safe_unicode
from email.Utils import formataddr
from zope.interface import alsoProvides
from zope.lifecycleevent import modified

from bika.lims import api
from bika.lims import bikaMessageFactory as _
from bika.lims import logger
from bika.lims.idserver import renameAfterCreation
from bika.lims.interfaces import IAnalysisRequest, IReceived
from bika.lims.interfaces import IAnalysisRequest
from bika.lims.interfaces import IAnalysisRequestRetest
from bika.lims.interfaces import IAnalysisRequestSecondary
from bika.lims.interfaces import IAnalysisService
from bika.lims.interfaces import IReceived
from bika.lims.interfaces import IRoutineAnalysis
from bika.lims.utils import attachPdf
from bika.lims.utils import changeWorkflowState
Expand All @@ -47,9 +53,6 @@
from bika.lims.workflow import push_reindex_to_actions_pool
from bika.lims.workflow.analysisrequest import AR_WORKFLOW_ID
from bika.lims.workflow.analysisrequest import do_action_to_analyses
from email.Utils import formataddr
from zope.interface import alsoProvides
from zope.lifecycleevent import modified


def create_analysisrequest(client, request, values, analyses=None,
Expand Down Expand Up @@ -88,6 +91,11 @@ def create_analysisrequest(client, request, values, analyses=None,
values.update({"Analyses": service_uids})
ar.processForm(REQUEST=request, values=values)

# Handle hidden analyses from template and profiles
# https://github.com/senaite/senaite.core/issues/1437
# https://github.com/senaite/senaite.core/issues/1326
apply_hidden_services(ar)

# Handle rejection reasons
rejection_reasons = resolve_rejection_reasons(values)
ar.setRejectionReasons(rejection_reasons)
Expand Down Expand Up @@ -145,6 +153,42 @@ def create_analysisrequest(client, request, values, analyses=None,
return ar


def apply_hidden_services(sample):
"""
Applies the hidden setting to the sample analyses in accordance with the
settings from its template and/or profiles
:param sample: the sample that contains the analyses
"""
hidden = list()

# Get the "hidden" service uids from the template
template = sample.getTemplate()
hidden = get_hidden_service_uids(template)

# Get the "hidden" service uids from profiles
profiles = sample.getProfiles()
hid_profiles = map(get_hidden_service_uids, profiles)
hid_profiles = list(itertools.chain(*hid_profiles))
hidden.extend(hid_profiles)

# Update the sample analyses
analyses = sample.getAnalyses(full_objects=True)
analyses = filter(lambda an: an.getServiceUID() in hidden, analyses)
for analysis in analyses:
analysis.setHidden(True)


def get_hidden_service_uids(profile_or_template):
"""Returns a list of service uids that are set as hidden
:param profile_or_template: ARTemplate or AnalysisProfile object
"""
if not profile_or_template:
return []
settings = profile_or_template.getAnalysisServicesSettings()
hidden = filter(lambda ser: ser.get("hidden", False), settings)
return map(lambda setting: setting["uid"], hidden)


def get_services_uids(context=None, analyses_serv=None, values=None):
"""
This function returns a list of UIDs from analyses services from its
Expand Down