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

Stickers to PDF and new sticker 2"x1" (50.8mm x 25.4mm) with barcode 3 of 9 #146

Merged
merged 4 commits into from
Jun 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions bika/lims/browser/stickers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@
# Some rights reserved. See LICENSE.txt, AUTHORS.txt.

from Products.CMFCore.utils import getToolByName
from Products.CMFPlone.utils import safe_unicode
from bika.lims import bikaMessageFactory as _, t
from bika.lims import logger
from bika.lims.browser import BrowserView
from bika.lims.utils import createPdf
from bika.lims.vocabularies import getStickerTemplates
from plone.resource.utils import iterDirectoriesOfType, queryResourceDirectory
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
import glob, os, os.path, sys, traceback

import os
import App
import tempfile


class Sticker(BrowserView):
Expand Down Expand Up @@ -51,6 +55,15 @@ class Sticker(BrowserView):
rendered_items = []

def __call__(self):
# Need to generate a PDF with the stickers?
if self.request.form.get('pdf', '0') == '1':
response = self.request.response
response.setHeader('Content-type', 'application/pdf')
response.setHeader('Content-Disposition', 'inline')
response.setHeader('filename', 'sticker.pdf')
pdfstream = self.pdf_from_post()
return pdfstream

self.rendered_items = []
items = self.request.get('items', '')
# If filter by type is given in the request, only the templates under
Expand Down Expand Up @@ -297,3 +310,15 @@ def _getStickersTemplatesDirectory(self, resource_name):
if self.filter_by_type:
templates_dir = templates_dir + '/' + self.filter_by_type
return templates_dir

def pdf_from_post(self):
"""Returns a pdf stream with the stickers
"""
html = self.request.form.get('html')
style = self.request.form.get('style')
reporthtml = '<html><head>{0}</head><body>{1}</body></html>'
reporthtml = reporthtml.format(style, html)
reporthtml = safe_unicode(reporthtml).encode('utf-8')
pdf_fn = tempfile.mktemp(suffix='.pdf')
pdf_file = createPdf(htmlreport=reporthtml, outfile=pdf_fn)
return pdf_file
51 changes: 51 additions & 0 deletions bika/lims/browser/templates/stickers/Code_39_2ix1i.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Sticker Dimensions: 2 inch x 1 inch = 50.8mm x 25.4mm
Sticker margins: 2mm
*/
.sticker {
margin:0 auto;
padding:2mm;
width: 46.8mm;
height: 21.4mm;
font-family: Helvetica, Arial;
font-size:7pt;
}
.sticker .barcode {
margin: 0 auto;
padding: 0 !important;
text-align:center;
}
.sticker .sample-id {
margin:0;
padding: 0mm 0mm 1mm 0mm;
text-align:center;
}
.sticker .analysisrequest-info table,
.sticker .sampling-date-info table {
border-collapse:collapse;
margin: 1px 1px 1px 1px;
width:100%;
}
.sticker .analysisrequest-info table td,
.sticker .sampling-date-info table td {
border:none;
text-align:center;
}
.barcode {
margin:0 auto;
}
.client-sample-id {
text-align:center;
}

@media print {
@page {
size: 50.8mm 25.4mm !important;
margin: 0mm !important;
}
html, body {
width: 50.8mm !important;
height: 25.4mm !important;
margin: 0mm !important;
}
}
72 changes: 72 additions & 0 deletions bika/lims/browser/templates/stickers/Code_39_2ix1i.pt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<!--
Default template used to render one barcode small sticker

To retrieve the item, use view.current_item, that will return an array with
the following structure:

[analysis_request_object, sample_object, sample_partition_object]

Although sample_object (position 1 in the array) will never be None,
analysis_request_object can be None when
a) The user requested to render stickers for samples (instead of ARs) or
b) The user requested to render stickers for sample partitions or
c) The user requested to render stickers for reference samples.

If c), both analysis_request_object and sample_partition_object will be None
-->
<tal:sticker define="
portal_state context/@@plone_portal_state;
portal_url portal_state/portal_url;
item view/current_item;
ar python:item[0];
ar_id python:ar.getId() if ar else '';
sample python:item[1];
part python:item[2];
partnr python:part.getId().split('-')[1];
sampler python:sample.getSampler();
sample_id python:sample.getId();
sample_point python:sample.getSamplePoint() and sample.getSamplePoint().Title() or '';
sample_type python:sample.getSampleType().Title();
sd python:sample.getSamplingDate();
sampling_date python:sd.Date() if sd else '';
client_sample_id python:sample.getClientSampleID();
preservation python:part.getPreservation() and part.getPreservation().Title() or '';
container python:part.getContainer() and part.getContainer().Title() or '';
date_sampled python:sample.getDateSampled() and sample.getDateSampled().Date();
analyses python:part.getAnalyses();
field_analyses python:[analysis for analysis in analyses if analysis.getPointOfCapture()=='field'];
show_partitions python:context.bika_setup.getShowPartitions();
smart_id python:part.getId() if show_partitions else sample.getId();
hazardous python:sample.getSampleType().getHazardous();">

<!-- Sample ID -->
<div class="sample-id">
<!--span tal:replace="string:${smart_id}"/-->
<img tal:condition="hazardous | nothing"
tal:attributes="src string:${portal_url}/++resource++bika.lims.images/hazardous.png"/>
</div>

<!-- Barcode -->
<div class="barcode"
tal:attributes="data-id smart_id;"
data-code="code39"
data-barHeight="14"
data-addQuietZone="true"
data-showHRI="false">
</div>

<!-- Some additional info about the sample -->
<div class="analysisrequest-info">
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td class="client-sample-id" tal:content="python:'%s: %s' % ('CSID', client_sample_id if client_sample_id else '---')"/>
</tr>
<tr>
<td class="sample-type" tal:content="python: sample_type"></td>
<tr>
<tr>
<td class="sampling-date" tal:content="python: sampling_date"></td>
<tr>
</table>
</div>
</tal:sticker>
30 changes: 26 additions & 4 deletions bika/lims/browser/templates/stickers_preview.pt
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@
}

@media print {
body {background-color:#fff;}
html, body {
margin: 0 !important;
padding: 0 !important;
background-color:#fff !important;
}
#sticker-preview-header {display:none !important;}
#stickers-wrapper { margin:0;}
.sticker {
Expand All @@ -108,7 +112,7 @@
$(function(){
$('#print-button').click(function(e) {
e.preventDefault();
window.print();
printPdf();
});
$('#cancel-button').click(function(e) {
e.preventDefault();
Expand Down Expand Up @@ -145,14 +149,32 @@
$('#sticker-rule').fadeIn();
});
}

function printPdf() {
var url = window.location.href;
// Get the stickers style
var style = $('#stickers-style').clone().wrap('<div>').parent().html();
var stickershtml = '';
// Get all stickers
$('#stickers-wrapper .sticker').each(function(e) {
stickershtml += $(this).clone().wrap('<div>').parent().html();
});
// Generate the form (POST request to generate PDF)
var form = '<form action="'+url+'" name="topdf" method="post" style="display:none">' +
'<textarea name="html"><div style="padding:0px;"></div>' +
stickershtml + '</textarea>' +
'<input type="hidden" name="pdf" value="1" />' +
'<textarea name="style">' + style + '</textarea>' +
'</form>';
$('body').html(form);
document.forms.topdf.submit();
}
});
</script>
</head>
<body tal:attributes="onload python:'this.print()' if view.request.get('autoprint', False) else '';
data-itemsurl python:view.getItemsURL();">
<div id='sticker-preview-wrapper'>
<!-- Header of the preview. This will never be printed.
Container with preview options and buttons -->
<div id="sticker-preview-header">
<div id='options-handler'>
<div class='options-line'>
Expand Down