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

Ability to select the number of sample sticker copies for printing #618

Merged
merged 24 commits into from
Jan 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
0c02c33
Add number of copies control
Jan 29, 2018
4674a75
Get the number of sticker copies from the template
Jan 29, 2018
056af87
Remove unnecessary variable definition
Jan 29, 2018
ed1c4e1
Correct typo
Jan 29, 2018
421e443
Implement POST action when number of copies changes and bind it to St…
Jan 29, 2018
f1a0697
Add logic for showing multiple copies of stickers, update post action…
Jan 29, 2018
c7b2d3c
Make sticker copies of the same sample go together (group stickers by…
Jan 29, 2018
93470ad
Add field in bika set up to set the default number of copies
Jan 30, 2018
44b2494
Class constructor and get default number of copies when first loading…
Jan 30, 2018
f3e615e
Corrections: 1- Get default number of copies when request is empty, e…
Jan 30, 2018
ccc587a
modify method to get number of copies
Jan 30, 2018
3205da1
Merge branch 'master' of https://github.com/senaite/bika.lims into ch…
Jan 30, 2018
1d986b8
Rename bika with senaite
Jan 30, 2018
c8f7656
Move super class constructor to the begining of the init method
Jan 31, 2018
20baa51
Fix typos and documentation
Jan 31, 2018
0afc800
Refactor logic of creating as many copies as stickers wanted into a n…
Jan 31, 2018
70e22f7
Documentation
Jan 31, 2018
bf21718
Remove update_sticker_copies function since reload function provides …
Jan 31, 2018
ac805ca
Bind a change in the number of copies to the reload function
Jan 31, 2018
6dd8801
Remove blank lines
Jan 31, 2018
10e1e6c
Render stickers only of items that have a valid item_id
Jan 31, 2018
a50cb45
Remove rendered_items variable from code since it is no longer used
Jan 31, 2018
372527d
Remove blank lines
Jan 31, 2018
99cad8a
Update Changes log
Jan 31, 2018
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
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Changelog

**Added**

- #618 When previewing stickers the number of copies to print for each sticker can be modified.
- #618 The default number of sticker copies can be set and edited in the setup Sticker's tab.

**Removed**

Expand Down
54 changes: 47 additions & 7 deletions bika/lims/browser/stickers.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,14 @@ class Sticker(BrowserView):
-- other_worksheet_stickers_...
"""
template = ViewPageTemplateFile("templates/stickers_preview.pt")
item_index = 0
current_item = None
rendered_items = []

def __init__(self, context, request):
super(Sticker, self).__init__(context, request)
self.item_index = 0
self.current_item = None
self.copies_count = None
self.context = context
self.request = request

def __call__(self):
# Need to generate a PDF with the stickers?
Expand All @@ -66,7 +71,8 @@ def __call__(self):
pdfstream = self.pdf_from_post()
return pdfstream

self.rendered_items = []
self.copies_count = self.get_copies_count()

items = self.request.get('items', '')
# If filter by type is given in the request, only the templates under
# the path with the type name will be given as vocabulary.
Expand All @@ -79,6 +85,9 @@ def __call__(self):
# Default fallback, load from context
self.items = [self.context, ]

# before retrieving the required data for each type of object copy
# each object as many times as the number of desired sticker copies
self.items = self._resolve_number_of_copies(self.items)
new_items = []
for i in self.items:
outitems = self._populateItems(i)
Expand Down Expand Up @@ -243,9 +252,7 @@ def nextItem(self):
"""
if self.item_index == len(self.items):
self.item_index = 0
self.rendered_items = []
self.current_item = self.items[self.item_index]
self.rendered_items.append(self.current_item[2].getId())
self.item_index += 1
return self.current_item

Expand Down Expand Up @@ -305,7 +312,7 @@ def _getStickersTemplatesDirectory(self, resource_name):
for the stickers deppending on the filter_by_type.
:param resource_name: The name of the resource folder.
:type resource_name: string
:resturns: a string as a path
:returns: a string as a path
"""
templates_dir =\
queryResourceDirectory('stickers', resource_name).directory
Expand All @@ -324,3 +331,36 @@ def pdf_from_post(self):
pdf_fn = tempfile.mktemp(suffix='.pdf')
pdf_file = createPdf(htmlreport=reporthtml, outfile=pdf_fn)
return pdf_file

def _resolve_number_of_copies(self, items):
"""For the given objects generate as many copies as the desired
number of stickers. The desired number of stickers for each
object is given by copies_count

:param items: list of objects whose stickers are going to be previewed.
:type items: list
:returns: list containing n copies of each object in the items list,
where n is self.copies_count
:rtype: list
"""
copied_items = []
for obj in items:
for copy in range(self.copies_count):
copied_items.append(obj)
return copied_items

def get_copies_count(self):
"""Return the copies_count number request parameter

:returns: the number of copies for each sticker as stated
in the request
:rtype: int
"""
try:
copies_count = int(self.request.form.get("copies_count"))
except (TypeError, ValueError):
# default number of copies is a mandatory integer field in senaite setup
# so theoretically this should never fail
copies_count = self.context.bika_setup.getDefaultNumberOfCopies()

return copies_count
43 changes: 35 additions & 8 deletions bika/lims/browser/templates/stickers_preview.pt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@
background-color: #bbb;
text-shadow: 1px 1px 1px #aaa;
}
#sticker-preview-header #sticker-copies input {
text-align:center;
width:15mm;
margin:0 0 10px 0;
background-color: #aaa;
border-radius: 5px;
color: #fff;
font-family: Helvetica,Arial;
font-size: 9pt;
font-weight: bold;
padding: 4px 2px;
text-decoration: none;
text-shadow: 1px 1px 1px #999;
text-transform: uppercase;
border:none;
cursor:pointer;
}
#sticker-rule {
color: rgb(51, 51, 51);
height: 10mm;
Expand Down Expand Up @@ -142,7 +159,10 @@
window.location = $(this).attr('data-url');
});
$('select#template').change(function(e) {
reload($('select#template').val());
reload($('select#template').val(), $('#copies_count').val());
});
$('#copies_count').change(function(e) {
reload($('select#template').val(), $('#copies_count').val());
});
var stickwidth = $('.sticker').first().width();
$('#sticker-rule').css({'width':stickwidth,'max-width':stickwidth});
Expand All @@ -151,14 +171,14 @@
/**
* Re-loads the stickers preview by using the template specified
*/
function reload(template) {
function reload(template, copies_count) {
var baseurl = $('body').attr('data-itemsurl');
$('#stickers-wrapper').fadeTo('fast', 0.4);
$.ajax({
url: baseurl,
type: 'POST',
async: true,
data: {"template": template}
data: {"template": template, "copies_count": copies_count}
})
.always(function(data) {
var htmldata = data;
Expand Down Expand Up @@ -221,6 +241,13 @@
</tal:formats>
</select>
</div>
<div id='sticker-copies'>
<label for="copies_count" i18n:translate="">Number of copies</label>
<input name="copies_count" id="copies_count"
type="number"
min="1"
tal:attributes="value view/copies_count"/>
</div>
</div>
<div id='sticker-buttons'>
<input type="button" id='cancel-button' value="Go back"
Expand All @@ -241,11 +268,11 @@
</tal:tick>
</div>
<tal:stickers repeat="sticker view/items">
<tal:sticker define="item_id python:sticker[2].getId() if sticker else None;"
condition="python:not item_id or item_id not in view.rendered_items">
<div class='sticker'
tal:content='structure python:view.renderItem()'></div>
</tal:sticker>
<tal:sticker define="item_id python:sticker[2].getId() if sticker else None;">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to figure out about that view.rendered_items. I understand why you need to remove it, cause each time the function nextItem is called, the current item is added in this view.rendered_items list, preventing you to display a label for a given object more than once. The thing is that I am not sure for what view.rendered_items var is used other than this condition... Anyhow, add a condition for at least check if item_id is not None (note that in define attribute there is the possibility to set a none value to item_id, which is something weird again...)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xispa I also checked the use of rendered_items and agree in that it was only used in the check that I removed to be able to print more than one copy. I am then going to remove rendered_items from the code.

<div class='sticker'
tal:condition="python:item_id is not None"
tal:content='structure python:view.renderItem()'></div>
</tal:sticker>
</tal:stickers>
</div>
</div>
Expand Down
10 changes: 10 additions & 0 deletions bika/lims/content/bikasetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,16 @@ def getCounterTypes(self, instance=None):
description=_("Select which sticker should be used as the 'large' sticker by default")
)
),
IntegerField(
'DefaultNumberOfCopies',
schemata="Sticker",
required="1",
default="1",
widget=IntegerWidget(
label=_("Number of copies"),
description=_("Set the default number of copies to be printed for each sticker")
)
),
IDFormattingField(
'IDFormatting',
schemata="ID Server",
Expand Down