def append(value): if isinstance(value, (list, tuple)): map(append, value) elif isinstance(value, (dict)): map(append, value.items()) elif isinstance(value, basestring): # convert unicode to UTF8 if isinstance(value, unicode): value = api.safe_unicode(value).encode("utf8") # skip single short values if len(value) < 2: return # flush non meaningful values if value in skip_values: return # flush ISO dates if re.match(DATE_RX, value): return # fetch the title if re.match(UID_RX, value): if value in uid_title_cache: value = uid_title_cache[value] else: title_or_id = get_title_or_id_from_uid(value) uid_title_cache[value] = title_or_id value = title_or_id catalog_data.add(value)
def _process_value(value): """Convert the value into a human readable diff string """ if not value: value = _("Not set") # handle strings elif isinstance(value, basestring): # XXX: bad data, e.g. in AS Method field if value == "None": value = _("Not set") # 0 is detected as the portal UID elif value == "0": value = "0" # handle physical paths elif value.startswith("/"): # remove the portal path to reduce noise in virtual hostings portal_path = api.get_path(api.get_portal()) value = value.replace(portal_path, "", 1) elif api.is_uid(value): value = _get_title_or_id_from_uid(value) # handle dictionaries elif isinstance(value, (dict)): value = json.dumps(sorted(value.items()), indent=1) # handle lists and tuples elif isinstance(value, (list, tuple)): value = sorted(map(_process_value, value)) value = "; ".join(value) # handle unicodes if isinstance(value, unicode): value = api.safe_unicode(value).encode("utf8") return str(value)
def to_string(obj, key, value, **kw): """to string """ if isinstance(value, six.string_types): value = api.safe_unicode(value).encode("utf-8") if value is None: value = "" return str(value)
def _process_value(value): """Convert the value into a human readable diff string """ if not value: value = _("Not set") # XXX: bad data, e.g. in AS Method field elif value == "None": value = _("Not set") # 0 is detected as the portal UID elif value == "0": pass elif api.is_uid(value): value = _get_title_or_id_from_uid(value) elif isinstance(value, (dict)): value = json.dumps(sorted(value.items()), indent=1) elif isinstance(value, (list, tuple)): value = sorted(map(_process_value, value)) value = "; ".join(value) elif isinstance(value, unicode): value = api.safe_unicode(value).encode("utf8") return str(value)
def download(self): """Generate PDF and send it fot download """ form = self.request.form # This is the html after it was rendered by the client browser and # eventually extended by JavaScript, e.g. Barcodes or Graphs added etc. # NOTE: It might also contain multiple reports! html = form.get("html", "") # convert to unicode # https://github.com/senaite/senaite.impress/pull/93 html = api.safe_unicode(html) # get the selected template template = form.get("template") # get the selected paperformat paperformat = form.get("format") # get the selected orientation orientation = form.get("orientation", "portrait") # get the filename filename = form.get("filename", "{}.pdf".format(template)) # Generate the print CSS with the set format/orientation css = self.get_print_css(paperformat=paperformat, orientation=orientation) logger.info(u"Print CSS: {}".format(css)) # get the publisher instance publisher = self.publisher # add the generated CSS to the publisher publisher.add_inline_css(css) # generate the PDF pdf = publisher.write_pdf(html) self.request.response.setHeader( "Content-Disposition", "attachment; filename=%s.pdf" % filename) self.request.response.setHeader("Content-Type", "application/pdf") self.request.response.setHeader("Content-Length", len(pdf)) self.request.response.setHeader("Cache-Control", "no-store") self.request.response.setHeader("Pragma", "no-cache") self.request.response.write(pdf)
def to_utf8(s): return api.safe_unicode(s).encode("utf8")
def set_lastname(self, value): if not isinstance(value, string_types): self.lastname = "" self.lastname = api.safe_unicode(value)