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

Simplify attachment render in report options to single checkbox #2262

Merged
merged 12 commits into from
Feb 24, 2023
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Changelog
2.4.0 (unreleased)
------------------

- #2262 Simplify attachment render in report options to single checkbox
- #2259 Prevent string results from formatting and number conversion
- #2255 Fix attachments from retracted or rejected analyses are not ignored
- #2201 Allow manual selection of units on results entry
Expand Down
4 changes: 2 additions & 2 deletions src/bika/lims/browser/analysisreport.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def get_attachment_data_by_uid(self, uid):
filename = f.filename
filesize = self.get_filesize(f)
mimetype = f.getContentType()
report_option = attachment.getReportOption()
render_in_report = attachment.getRenderInReport()

return {
"obj": attachment,
Expand All @@ -108,5 +108,5 @@ def get_attachment_data_by_uid(self, uid):
"filesize": filesize,
"filename": filename,
"mimetype": mimetype,
"report_option": report_option,
"render_in_report": render_in_report,
}
2 changes: 1 addition & 1 deletion src/bika/lims/browser/analysisrequest/add2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1098,7 +1098,7 @@ def to_attachment_record(self, fileupload):
return {
"AttachmentFile": fileupload,
"AttachmentType": "",
"ReportOption": "",
"RenderInReport": False,
"AttachmentKeys": "",
"Service": "",
}
Expand Down
55 changes: 30 additions & 25 deletions src/bika/lims/browser/publish/emailview.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,35 +568,21 @@ def add_status_message(self, message, level="info"):
def get_report_data(self, report):
"""Report data to be used in the template
"""
sample = report.getAnalysisRequest()

# ignore attachments from cancelled, retracted and rejected analyses
analyses = []
skip = ["cancelled", "rejected", "retracted"]
for analysis in sample.getAnalyses(full_objects=True):
if api.get_review_status(analysis) in skip:
continue
analyses.append(analysis)

# merge together sample + analyses attachments
attachments = itertools.chain(
sample.getAttachment(),
*map(lambda an: an.getAttachment(), analyses))

primary_sample = report.getAnalysisRequest()
samples = report.getContainedAnalysisRequests() or [primary_sample]
attachments_data = []
for attachment in attachments:
attachment_data = self.get_attachment_data(attachment)
if attachment_data.get("report_option") == "i":
# attachment to be ignored from results report
continue
attachments_data.append(attachment_data)

for sample in samples:
for attachment in self.get_all_sample_attachments(sample):
attachment_data = self.get_attachment_data(sample, attachment)
attachments_data.append(attachment_data)

pdf = self.get_pdf(report)
filesize = "{} Kb".format(self.get_filesize(pdf))
filename = self.get_report_filename(report)

return {
"sample": sample,
"primary_sample": primary_sample,
"attachments": attachments_data,
"pdf": pdf,
"obj": report,
Expand All @@ -605,7 +591,25 @@ def get_report_data(self, report):
"filename": filename,
}

def get_attachment_data(self, attachment):
def get_all_sample_attachments(self, sample):
"""return all valid attachments of the given sample
"""
# ignore attachments from cancelled, retracted and rejected analyses
analyses = []
skip = ["cancelled", "rejected", "retracted"]
for analysis in sample.getAnalyses(full_objects=True):
if api.get_review_status(analysis) in skip:
continue
analyses.append(analysis)

# merge together sample + analyses attachments
attachments = itertools.chain(
sample.getAttachment(),
*map(lambda an: an.getAttachment(), analyses))

return list(attachments)

def get_attachment_data(self, sample, attachment):
"""Attachments data to be used in the template
"""
f = attachment.getAttachmentFile()
Expand All @@ -614,9 +618,10 @@ def get_attachment_data(self, attachment):
filename = f.filename
filesize = self.get_filesize(f)
mimetype = f.getContentType()
report_option = attachment.getReportOption()
render_in_report = attachment.getRenderInReport()

return {
"sample": sample,
"obj": attachment,
"attachment_type": attachment_type,
"attachment_keys": attachment_keys,
Expand All @@ -625,7 +630,7 @@ def get_attachment_data(self, attachment):
"filesize": filesize,
"filename": filename,
"mimetype": mimetype,
"report_option": report_option,
"render_in_report": render_in_report,
}

def get_recipients_data(self, reports):
Expand Down
8 changes: 4 additions & 4 deletions src/bika/lims/browser/publish/templates/email.pt
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
class="attachments well row">
<tal:reports repeat="report view/reports_data">
<input type="hidden" name="uids:list" tal:attributes="value report/uid"/>
<div class="attachment col-xs-12 col-sm-4 col-md-3 col-lg-2">
<div class="attachment d-inline-block pr-4">
<a href="#"
class="report-link"
target="_blank"
Expand All @@ -162,12 +162,12 @@
</div>

<div id="additional-attachments-container"
class="attachments well row"
class="attachments row"
tal:condition="python:any(attachments)">
<!-- Additional Attachments -->
<tal:reports repeat="report view/reports_data">
<tal:attachments repeat="attachment report/attachments">
<div class="attachment col-xs-12 col-sm-4 col-md-3 col-lg-2">
<div class="attachment d-inline-block pr-4">
<input type="checkbox" name="attachment_uids:list" tal:attributes="value attachment/uid"/>
<a href="#"
class="attachment-link"
Expand All @@ -183,7 +183,7 @@
<div class="attachment-info small">
<span tal:content="attachment/attachment_keys"/>
<span i18n:translate="">in</span>
<span tal:content="report/sample/getId"/>
<span tal:content="attachment/sample/getId"/>
<div class="fileinfo">
<span class="filetype" tal:content="attachment/mimetype"/>
<span class="filesize" tal:content="string:${attachment/filesize}kB"/>
Expand Down
4 changes: 0 additions & 4 deletions src/bika/lims/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@
('c', _('Control')),
('b', _('Blank')),
))
ATTACHMENT_REPORT_OPTIONS = DisplayList((
('r', _('Render in Report')),
('i', _('Ignore in Report')),
))
GENDERS = DisplayList((
('male', _('Male')),
('female', _('Female')),
Expand Down
2 changes: 1 addition & 1 deletion src/bika/lims/content/analysisrequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2394,7 +2394,7 @@ def process_inline_images(self, html):
# create a new attachment
attachment = self.createAttachment(filedata, filename)
# ignore the attachment in report
attachment.setReportOption("i")
attachment.setRenderInReport(False)
# remove the image data base64 prefix
html = html.replace(data_type, "")
# remove the base64 image data with the attachment link
Expand Down
37 changes: 25 additions & 12 deletions src/bika/lims/content/attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,28 @@
from AccessControl import ClassSecurityInfo
from bika.lims import api
from bika.lims import bikaMessageFactory as _
from bika.lims import deprecated
from bika.lims import logger
from bika.lims.browser.fields import UIDReferenceField
from bika.lims.browser.fields.uidreferencefield import get_backreferences
from bika.lims.browser.widgets import DateTimeWidget
from bika.lims.browser.widgets import ReferenceWidget
from bika.lims.config import ATTACHMENT_REPORT_OPTIONS
from bika.lims.config import PROJECTNAME
from bika.lims.content.bikaschema import BikaSchema
from bika.lims.content.clientawaremixin import ClientAwareMixin
from bika.lims.interfaces.analysis import IRequestAnalysis
from DateTime import DateTime
from plone.app.blob.field import FileField
from Products.Archetypes.atapi import BaseFolder
from Products.Archetypes.atapi import BooleanField
from Products.Archetypes.atapi import BooleanWidget
from Products.Archetypes.atapi import DateTimeField
from Products.Archetypes.atapi import FileWidget
from Products.Archetypes.atapi import Schema
from Products.Archetypes.atapi import SelectionWidget
from Products.Archetypes.atapi import StringField
from Products.Archetypes.atapi import StringWidget
from Products.Archetypes.atapi import registerType


schema = BikaSchema.copy() + Schema((

FileField(
Expand All @@ -61,16 +61,14 @@
),
),

StringField(
"ReportOption",
searchable=True,
vocabulary=ATTACHMENT_REPORT_OPTIONS,
widget=SelectionWidget(
label=_("Report Options"),
checkbox_bound=0,
format="select",
BooleanField(
"RenderInReport",
default=True,
widget=BooleanWidget(
label=_("Render attachment in report"),
description=_(
"Only images can be rendered in the report directly"),
visible=True,
default="r",
),
),

Expand Down Expand Up @@ -238,5 +236,20 @@ def getFilename(self):
att_file = self.getAttachmentFile()
return att_file.filename

@deprecated(comment="Removed in 3.x", replacement="getRenderInReport")
def getReportOption(self):
"""BBB: returns "r" if RenderInReport is True, otherwise "i"
"""
if not self.getRenderInReport():
return "i"
return "r"

@deprecated(comment="Removed in 3.x", replacement="setRenderInReport")
def setReportOption(self, value):
"""BBB: set RenderInReport to True if the passed in value is "r",
and for all other values to False
"""
self.setRenderInReport(value == "r")


registerType(Attachment, PROJECTNAME)
4 changes: 2 additions & 2 deletions src/bika/lims/workflow/analysis/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def after_retract(analysis):

# Ignore attachments of this analysis in results report
for attachment in analysis.getAttachment():
attachment.setReportOption("i")
attachment.setRenderInReport(False)

# Retract our dependents (analyses that depend on this analysis)
cascade_to_dependents(analysis, "retract")
Expand Down Expand Up @@ -192,7 +192,7 @@ def after_reject(analysis):

# Ignore attachments of this analysis in results report
for attachment in analysis.getAttachment():
attachment.setReportOption("i")
attachment.setRenderInReport(False)

# Reject our dependents (analyses that depend on this analysis)
cascade_to_dependents(analysis, "reject")
Expand Down
Loading