def action_add_to_ws(self):
        """Form action to add a new attachment in a worksheet
        """

        ws = self.context
        form = self.request.form
        attachment_file = form.get('AttachmentFile_file', None)
        analysis_uid = self.request.get('analysis_uid', None)
        service_uid = self.request.get('Service', None)
        AttachmentType = form.get('AttachmentType', '')
        AttachmentKeys = form.get('AttachmentKeys', '')
        ReportOption = form.get('ReportOption', 'r')

        # nothing to do if the attachment file is missing
        if attachment_file is None:
            logger.warn("AttachmentView.action_add_attachment: Attachment file is missing")
            return

        if analysis_uid:
            rc = api.get_tool("reference_catalog")
            analysis = rc.lookupObject(analysis_uid)

            # create attachment
            attachment = self.create_attachment(
                ws,
                attachment_file,
                AttachmentType=AttachmentType,
                AttachmentKeys=AttachmentKeys,
                ReportOption=ReportOption)

            others = analysis.getAttachment()
            attachments = []
            for other in others:
                attachments.append(other.UID())
            attachments.append(attachment.UID())
            analysis.setAttachment(attachments)

            # The metadata for getAttachmentUIDs need to get updated,
            # otherwise the attachments are not displayed
            # https://github.com/senaite/bika.lims/issues/521
            analysis.reindexObject()

        if service_uid:
            workflow = api.get_tool('portal_workflow')

            # XXX: refactor out dependency to this view.
            view = api.get_view("manage_results", context=self.context, request=self.request)
            analyses = self.context.getAnalyses()
            allowed_states = ["assigned", "unassigned", "to_be_verified"]
            for analysis in analyses:
                if analysis.portal_type not in ('Analysis', 'DuplicateAnalysis'):
                    continue
                if not analysis.getServiceUID() == service_uid:
                    continue
                review_state = workflow.getInfoFor(analysis, 'review_state', '')
                if review_state not in allowed_states:
                    continue

                # create attachment
                attachment = self.create_attachment(
                    ws,
                    attachment_file,
                    AttachmentType=AttachmentType,
                    AttachmentKeys=AttachmentKeys,
                    ReportOption=ReportOption)

                others = analysis.getAttachment()
                attachments = []
                for other in others:
                    attachments.append(other.UID())
                attachments.append(attachment.UID())
                analysis.setAttachment(attachments)

                # The metadata for getAttachmentUIDs need to get updated,
                # otherwise the attachments are not displayed
                # https://github.com/senaite/bika.lims/issues/521
                analysis.reindexObject()

        if self.request['HTTP_REFERER'].endswith('manage_results'):
            self.request.response.redirect('{}/manage_results'.format(
                self.context.absolute_url()))
        else:
            self.request.response.redirect(self.context.absolute_url())
Form action to add a new attachment in a worksheet