def getSelectedTemplate(self): """ Returns the id of the sticker template selected. If no specific template found in the request (parameter template), returns the default template set in Bika Setup > Stickers. If the template doesn't exist, uses the default bika.lims' Code_128_1x48mm.pt template (was sticker_small.pt). If no template selected but size param, get the sticker template set as default in Bika Setup for the size set. """ bs_template = self.context.bika_setup.getAutoStickerTemplate() size = self.request.get('size', '') if size == 'small': bs_template = self.context.bika_setup.getSmallStickerTemplate() elif size == 'large': bs_template = self.context.bika_setup.getLargeStickerTemplate() rq_template = self.request.get('template', bs_template) # Check if the template exists. If not, fallback to default's if rq_template.find(':') >= 0: prefix, rq_template = rq_template.split(':') templates_dir = queryResourceDirectory('stickers', prefix).directory else: this_dir = os.path.dirname(os.path.abspath(__file__)) templates_dir = os.path.join(this_dir, 'templates/stickers/') if not os.path.isfile(os.path.join(templates_dir, rq_template)): rq_template = 'Code_128_1x48mm.pt' return rq_template
def getReportStyle(self): """ Returns the css style to be used for the current template. If the selected template is 'default.pt', this method will return the content from 'default.css'. If no css file found for the current template, returns empty string """ template = self.request.form.get('template', self._DEFAULT_TEMPLATE) content = '' if template.find(':') >= 0: prefix, template = template.split(':') resource = queryResourceDirectory('reports', prefix) css = '{0}.css'.format(template[:-3]) if css in resource.listDirectory(): content = resource.readFile(css) else: this_dir = os.path.dirname(os.path.abspath(__file__)) templates_dir = os.path.join(this_dir, 'templates/reports/') path = '%s/%s.css' % (templates_dir, template[:-3]) with open(path, 'r') as content_file: content = content_file.read() return content
def getReportTemplate(self): """ Returns the html template for the current ar and moves to the next ar to be processed. Uses the selected template specified in the request ('template' parameter) """ templates_dir = 'templates/reports' embedt = self.request.form.get('template', self._DEFAULT_TEMPLATE) if embedt.find(':') >= 0: prefix, template = embedt.split(':') templates_dir = queryResourceDirectory('reports', prefix).directory embedt = template embed = ViewPageTemplateFile(os.path.join(templates_dir, embedt)) reptemplate = "" try: reptemplate = embed(self) except: tbex = traceback.format_exc() arid = self._ars[self._current_ar_index].id reptemplate = "<div class='error-report'>%s - %s '%s':<pre>%s</pre></div>" % (arid, _("Unable to load the template"), embedt, tbex) self._nextAnalysisRequest() return reptemplate
def renderWSTemplate(self): """ Returns the current worksheet rendered with the template specified in the request (param 'template'). Moves the iterator to the next worksheet available. """ templates_dir = self._TEMPLATES_DIR embedt = self.request.get('template', self._DEFAULT_TEMPLATE) if embedt.find(':') >= 0: prefix, embedt = embedt.split(':') templates_dir = queryResourceDirectory(self._TEMPLATES_ADDON_DIR, prefix).directory embed = ViewPageTemplateFile(os.path.join(templates_dir, embedt)) reptemplate = "" try: reptemplate = embed(self) except: tbex = traceback.format_exc() wsid = self._worksheets[self._current_ws_index].id reptemplate = "<div class='error-print'>%s - %s '%s':<pre>%s</pre></div>" % (wsid, _("Unable to load the template"), embedt, tbex) if self._current_ws_index < len(self._worksheets): self._current_ws_index += 1 return reptemplate
def getReportTemplate(self): """ Returns the html template for the current ar and moves to the next ar to be processed. Uses the selected template specified in the request ('template' parameter) """ templates_dir = 'templates/reports' embedt = self.request.form.get('template', self._DEFAULT_TEMPLATE) if embedt.find(':') >= 0: prefix, template = embedt.split(':') templates_dir = queryResourceDirectory('reports', prefix).directory embedt = template embed = ViewPageTemplateFile(os.path.join(templates_dir, embedt)) reptemplate = "" try: reptemplate = embed(self) except: tbex = traceback.format_exc() arid = self._ars[self._current_ar_index].id reptemplate = "<div class='error-report'>%s - %s '%s':<pre>%s</pre></div>" % ( arid, _("Unable to load the template"), embedt, tbex) self._nextAnalysisRequest() return reptemplate
def renderWSTemplate(self): """ Returns the current worksheet rendered with the template specified in the request (param 'template'). Moves the iterator to the next worksheet available. """ templates_dir = self._TEMPLATES_DIR embedt = self.request.get('template', self._DEFAULT_TEMPLATE) if embedt.find(':') >= 0: prefix, embedt = embedt.split(':') templates_dir = queryResourceDirectory(self._TEMPLATES_ADDON_DIR, prefix).directory embed = ViewPageTemplateFile(os.path.join(templates_dir, embedt)) reptemplate = "" try: reptemplate = embed(self) except: tbex = traceback.format_exc() wsid = self._worksheets[self._current_ws_index].id reptemplate = "<div class='error-print'>%s - %s '%s':<pre>%s</pre></div>" % ( wsid, _("Unable to load the template"), embedt, tbex) if self._current_ws_index < len(self._worksheets): self._current_ws_index += 1 return reptemplate
def renderItem(self): """ Renders the next available sticker. Uses the template specified in the request ('template' parameter) by default. If no template defined in the request, uses the default template set by default in Bika Setup > Stickers. If the template specified doesn't exist, uses the default bika.lims' Code_128_1x48mm.pt template (was sticker_small.pt). """ curritem = self.nextItem() templates_dir = 'templates/stickers' embedt = self.getSelectedTemplate() if embedt.find(':') >= 0: prefix, embedt = embedt.split(':') templates_dir = queryResourceDirectory('stickers', prefix).directory fullpath = os.path.join(templates_dir, embedt) try: embed = ViewPageTemplateFile(fullpath) return embed(self) except: tbex = traceback.format_exc() stickerid = curritem[2].id if curritem[2] else curritem[1].id return "<div class='error'>%s - %s '%s':<pre>%s</pre></div>" % \ (stickerid, _("Unable to load the template"), embedt, tbex)
def getSelectedTemplateCSS(self): """ Looks for the CSS file from the selected template and return its contents. If the selected template is default.pt, looks for a file named default.css in the stickers path and return its contents. If no CSS file found, retrns an empty string """ template = self.getSelectedTemplate() # Look for the CSS content = '' if template.find(':') >= 0: # A template from another add-on prefix, template = template.split(':') resource = queryResourceDirectory('stickers', prefix) css = '{0}.css'.format(template[:-3]) if css in resource.listDirectory(): content = resource.readFile(css) else: this_dir = os.path.dirname(os.path.abspath(__file__)) templates_dir = os.path.join(this_dir, 'templates/stickers/') path = '%s/%s.css' % (templates_dir, template[:-3]) if os.path.isfile(path): with open(path, 'r') as content_file: content = content_file.read() return content