Exemple #1
0
    def workflow_action_schedule_sampling(self):
        """
        This function prevent the transition if the fields "SamplingDate"
        and "ScheduledSamplingSampler" are uncompleted.
        :return: bool
        """
        from bika.lims.utils.workflow import schedulesampling

        message = "Not expected transition."
        # In Samples Folder we have to get each selected item
        if interfaces.ISamplesFolder.providedBy(self.context):
            select_objs = WorkflowAction._get_selected_items(self)
            message = _("Transition done.")
            for key in select_objs.keys():
                sample = select_objs[key]
                # Getting the sampler
                sch_sampl = (
                    self.request.form.get("getScheduledSamplingSampler", None)[0].get(key)
                    if self.request.form.get("getScheduledSamplingSampler", None)
                    else ""
                )
                # Getting the date
                sampl_date = (
                    self.request.form.get("getSamplingDate", None)[0].get(key)
                    if self.request.form.get("getSamplingDate", None)
                    else ""
                )
                # Setting both values
                sample.setScheduledSamplingSampler(sch_sampl)
                sample.setSamplingDate(sampl_date)
                # Transitioning the sample
                success, errmsg = schedulesampling.doTransition(sample)
                if errmsg == "missing":
                    message = _(
                        "'Sampling date' and 'Define the Sampler for the"
                        + " scheduled sampling' must be completed and saved "
                        + "in order to schedule a sampling. Element: %s" % sample.getId()
                    )
                elif errmsg == "cant_trans":
                    message = _("The item %s can't be transitioned." % sample.getId())
                else:
                    message = _("Transition done.")
                self.context.plone_utils.addPortalMessage(message, "info")
        else:
            success, errmsg = schedulesampling.doTransition(self.context)
            if errmsg == "missing":
                message = _(
                    "'Sampling date' and 'Define the Sampler for the"
                    + " scheduled sampling' must be completed and saved in "
                    + "order to schedule a sampling."
                )
            elif errmsg == "cant_trans":
                message = _("The item can't be transitioned.")
            else:
                message = _("Transition done.")
            self.context.plone_utils.addPortalMessage(message, "info")
        # Reload the page in order to show the portal message
        self.request.response.redirect(self.context.absolute_url())
        return success
 def test_sample_workflow_action_schedule_sampling(self):
     """
     This test validates the function
     bika/lims//browser/analysisrequest/workflow.py/workflow_action_schedule_sampling
     """
     from bika.lims.utils.workflow import schedulesampling
     workflow = getToolByName(self.portal, 'portal_workflow')
     pc = getToolByName(self.portal, 'portal_catalog')
     sampler = api.user.get(username='sampler1')
     coordinator = self.createUser('SamplingCoordinator', 'cord1')
     # checking if the user belongs to the coordinators group
     mtool = getToolByName(self.portal, 'portal_membership')
     groups_tool = getToolByName(self.portal, 'portal_groups')
     usr_groups = groups_tool.getGroupsByUserId('cord1')
     self.assertIn(
         'SamplingCoordinators', [group.id for group in usr_groups])
     # Getting the client
     client = self.portal.clients['client-1']
     # Getting a sample type
     sampletype = self.portal.bika_setup.bika_sampletypes['sampletype-1']
     # Creating an AR
     values = {'Client': client.UID(),
               'Contact': client.getContacts()[0].UID(),
               'SamplingDate': '2015-01-01',
               'SampleType': sampletype.UID()}
     request = {}
     services = [s.UID() for s in self.services]
     # creating the analysisrequest
     ar = create_analysisrequest(client, request, values, services)
     self.assertEqual(
         workflow.getInfoFor(ar, 'review_state'), 'to_be_sampled')
     # Changing user to coordinator
     logout()
     login(self.portal, 'cord1')
     # If ScheduledSamplingSampler is empty and ScheduleSamplingEnabled,
     # no workflow_action_schedule_sampling can be done
     schedulesampling.doTransition(ar.getSample())
     self.assertEqual(
         workflow.getInfoFor(ar, 'review_state'), 'to_be_sampled')
     self.assertEqual(
         workflow.getInfoFor(ar.getSample(), 'review_state'),
         'to_be_sampled')
     # set a value in ScheduledSamplingSampler
     ar.setScheduledSamplingSampler(sampler)
     schedulesampling.doTransition(ar.getSample())
     self.assertEqual(
         workflow.getInfoFor(ar, 'review_state'), 'scheduled_sampling')
     self.assertEqual(
         workflow.getInfoFor(ar.getSample(), 'review_state'),
         'scheduled_sampling')