def __call__(self): workflow = getToolByName(self.context, 'portal_workflow') # If there's analyses that exceed panic levels, show an alert message analyses = self.context.getAnalyses() for obj in analyses: obj = obj.getObject() if hasattr(obj, 'getObject') else obj astate = workflow.getInfoFor(obj, 'review_state') if astate == 'retracted': continue panic_alerts = ResultOutOfRange(obj)() if panic_alerts: translate = self.context.translate addPortalMessage = self.context.plone_utils.addPortalMessage message = translate( _('Some results exceeded the ' 'panic levels that may ' 'indicate an imminent ' 'life-threatening condition.')) addPortalMessage(message, 'warning') break return super(ManageResultsView, self).__call__()
def __call__(self): workflow = getToolByName(self.context, 'portal_workflow') translate = self.context.translate plone.protect.CheckAuthenticator(self.request) bc = getToolByName(self.context, 'bika_catalog') ar = bc(UID=self.request.get('uid', None)) if ar: ar = ar[0].getObject() self.ccs = [] self.recipients = [] contact = ar.getContact() if contact: email = contact.getEmailAddress() if email: self.recipients = [{ 'uid': contact.UID(), 'name': contact.Title(), 'email': email }] for cc in ar.getCCContact(): ccemail = cc.getEmailAddress() if ccemail: self.ccs.append({ 'uid': cc.UID(), 'name': cc.Title(), 'email': ccemail }) analyses = ar.getAnalyses() strans = [] for analysis in analyses: analysis = analysis.getObject() astate = workflow.getInfoFor(analysis, 'review_state') if astate == 'retracted': continue panic_alerts = ResultOutOfRange(analysis)() if panic_alerts: serviceTitle = analysis.getServiceTitle() result = analysis.getResult() strans.append("- {0}, {1}: {2}".format( serviceTitle, translate(_("Result")), result)) stran = "\n".join(strans) laboratory = self.context.bika_setup.laboratory lab_address = "\n".join(laboratory.getPrintAddress()) self.body = translate( _( "Some results from the Analysis Request ${ar} " "exceeded the panic levels that may indicate an " "imminent life-threatening condition: \n\n${arlist}\n" "\n\n${lab_address}", mapping={ 'ar': ar.getRequestID(), 'arlist': stran, 'lab_address': lab_address })) self.subject = translate( _("Some results from ${ar} exceeded panic range", mapping={'ar': ar.getRequestID()})) return self.template()
def __call__(self): # Do generic bika.lims stuff _AnalysisRequestWorkflowAction.__call__(self) # Do bika-health specific actions when submit action = _AnalysisRequestWorkflowAction._get_form_workflow_action(self) addPortalMessage = self.context.plone_utils.addPortalMessage if action[0] == 'submit' and isActive(self.context): inpanicanalyses = [] workflow = getToolByName(self.context, 'portal_workflow') translate = self.context.translate rc = getToolByName(self.context, REFERENCE_CATALOG) uc = getToolByName(self.context, 'uid_catalog') # retrieve the results from database and check if # the values are exceeding panic levels alerts = {} for uid in self.request.form.get('Result', [{}])[0].keys(): analysis = rc.lookupObject(uid) analysis = analysis.getObject() if hasattr(analysis, 'getObject') else analysis if not analysis: continue astate = workflow.getInfoFor(analysis, 'review_state') if astate == 'retracted': continue alerts.update(ResultOutOfRange(analysis)()) if alerts: message = translate(_('Some results exceeded the ' 'panic levels that may ' 'indicate an imminent ' 'life-threatening condition' )) addPortalMessage(message, 'warning') self.request.response.redirect(self.context.absolute_url()) # If panic levels alert email enabled, send an email to # labmanagers bs = self.context.bika_setup if hasattr(bs, 'getEnablePanicAlert') \ and bs.getEnablePanicAlert(): laboratory = self.context.bika_setup.laboratory lab_address = "<br/>".join(laboratory.getPrintAddress()) managers = self.context.portal_groups.getGroupMembers('LabManagers') mime_msg = MIMEMultipart('related') mime_msg['Subject'] = _("Panic alert") mime_msg['From'] = formataddr( (encode_header(laboratory.getName()), laboratory.getEmailAddress())) to = [] for manager in managers: user = self.portal.acl_users.getUser(manager) uemail = user.getProperty('email') ufull = user.getProperty('fullname') to.append(formataddr((encode_header(ufull), uemail))) mime_msg['To'] = ','.join(to) strans = [] ars = {} for analysis_uid, alertlist in alerts: analysis = uc(analysis_uid).getObject() for alert in alertlist: ars[analysis.aq_parent.Title()] = 1 strans.append("- {0}, {1}: {2}".format( analysis.Title(), translate(_("Result")), analysis.getResult())) ars = ", ".join(ars.keys()) stran = "<br/>".join(strans) text = translate(_( "Some results from ${items} exceeded the panic levels " "that may indicate an imminent life-threatening " "condition: <br/><br/>{analysisresults}<br/><br/>" "<b>Please, check the Analysis Request if you " "want to re-test the analysis or immediately " "alert the client.</b><br/><br/>{lab_address}", mapping={'items': ars, 'analysisresults': stran, 'lab_address': lab_address})) msg_txt = MIMEText(safe_unicode(text).encode('utf-8'), _subtype='html') mime_msg.preamble = 'This is a multi-part MIME message.' mime_msg.attach(msg_txt) try: host = getToolByName(self.context, 'MailHost') host.send(mime_msg.as_string(), immediate=True) except Exception as msg: ar = inpanicanalyses[0].getRequestID() logger.error("Panic level email %s: %s" % (ar, str(msg))) message = translate( _('Unable to send an email to alert lab ' 'managers that some analyses exceeded the ' 'panic levels') + (": %s" % str(msg))) addPortalMessage(message, 'warning')