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

Adapter hook for confirmation when creating a Sample #1802

Merged
merged 3 commits into from
May 13, 2021

Conversation

xispa
Copy link
Member

@xispa xispa commented May 13, 2021

Description of the issue/feature this PR addresses

This Pull Request makes possible to add an adapter to force the user to confirm before the samples are created when Sample's add form "Save" button is clicked. For instance, we might want the system to check first for duplicates in the system before a new sample is created based on the data (client sample id, profile, etc.) entered by user.

An adapter that checks for duplicates based on the ClientSampleID and DateSampled, might look as follows:

class AddSampleConfirmation(object):
    """An adapter for the confirmation of sample creation in Add form
    """
    implements(IAddSampleConfirmation)

    def __init__(self, request):
        self.request = request

    def check_confirmation(self, records):
        # Check if a sample for the given date of collection and client sample id exists already
        field_names = ["ClientSampleID", "DateSampled"]
        
        for record in records:
            field_values = map(lambda name: record.get(name), field_names)
            if not all(field_values):
                continue

            date_sampled = api.to_date(field_values[2])
            date_sampled = date_sampled.strftime("%Y-%m-%d")
            date_from = "{} 00:00".format(date_sampled)
            date_to = "{} 23:59".format(date_sampled)
            query = {
                "getClientSampleID": field_values[0],
                "getDateSampled": {
                    "query": [date_from, date_to],
                    "range": "min:max",
                }
            }
            brains = api.search(query, CATALOG_ANALYSIS_REQUEST_LISTING)
            if brains:
                samp_id = api.get_id(brains[0])
                samp_ids.append(samp_id)

        confirmation = None
        if samp_ids:
            samp_ids = ", ".join(samp_ids)
            confirmation = {
                "message": "Potential duplicates found: {}".format(samp_ids)
            }
        return confirmation

Current behavior before PR

Is not possible to prompt the user for confirmation on Sample submission

Desired behavior after PR is merged

Possible to prompt the user for confirmation on Sample submission when some criteria are met

--
I confirm I have tested this PR thoroughly and coded it according to PEP8
and Plone's Python styleguide standards.

@xispa xispa requested a review from ramonski May 13, 2021 15:54
@xispa xispa added the Version 1.3 SENAITE Core Version 1.3.x label May 13, 2021
Copy link
Contributor

@ramonski ramonski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Version 1.3 SENAITE Core Version 1.3.x
Development

Successfully merging this pull request may close these issues.

2 participants