def getFormattedValue(self, metafield, maskitem, mask, node, language, html=True, template_from_caller=None): value = node.get_special(metafield.name) # consider int, long values like filesize if isinstance(value, (int, long)): value = str(value) value = value.replace(";", "; ") # ignore trailing newlines for textfields value = value.rstrip("\r\n") if value.find('\n') != -1: valuesList = value.split('\n') if any(lang in valuesList for lang in config.languages): # treat as multilingual index = 0 try: index = valuesList.index(language) value = valuesList[index + 1] except ValueError as e: msg = "Exception in getFormattedValue for textfield:\n" msg += " valuesList=%r\n" % valuesList msg += " node.name=%r, node.id=%r, node.type=%r\n" % ( node.name, node.id, node.type) msg += " metafield.name=%r, metafield.id=%r, metafield.type=%r\n" % ( metafield.name, metafield.id, metafield.type) msg += " language=%r, mask=%r" % (language, mask) logg.exception(msg) value = u"" else: # treat as monolingual pass if html: value = esc(value) # replace variables # substitute TeX sub/super-scripts with <sub>/<sup> html tags value = modify_tex(value, 'html') value = replace_attribute_variables(value, node.id, node.get, r'<(.+?)>', "<", ">") value = value.replace("<", "<").replace(">", ">") if not maskitem: return (metafield.getLabel(), value) # use default value from mask if value is empty if value == u'': value = maskitem.getDefault() return (metafield.getLabel(), value)
def getFormattedValue(self, metafield, maskitem, mask, node, language, html=True): value = node.get(metafield.getName()).replace(";", "; ") if html: value = esc(value) # replace variables value = replace_attribute_variables(value, node.id, node.get, r'<(.+?)>', "<", ">") return (metafield.getLabel(), value)
def test_replace_attribute_variables_unicode_val(): value = '''test123<att:test_var>blabla;>''' nid = 12345 reg_exp = r"<(.+?)>" left_tag = "<" right_tag = ">" value_getter = lambda attr_name: {'test_var': u'äüß', 'x': '---'}.get(attr_name) res = replace_attribute_variables(value, nid, value_getter, reg_exp, left_tag, right_tag) assert res == u'''test123äüßblabla;>'''
def test_replace_attribute_variables_other_regex(): value = '''test123<att:test_var>blabla;>''' nid = 12345 reg_exp = r"\<(.+?)\>" left_tag = "<" right_tag = ">" value_getter = lambda attr_name: {'test_var': 'OK', 'x': '---'}.get(attr_name) res = replace_attribute_variables(value, nid, value_getter, reg_exp, left_tag, right_tag) assert res == u'test123OKblabla;>'
def test_replace_attribute_variables_id(): value = '''test123<att:id>blabla;>''' nid = 12345 reg_exp = r"<(.+?)>" left_tag = "<" right_tag = ">" value_getter = lambda attr_name: { 'test_var': 'OK', 'x': '---' }.get(attr_name) res = replace_attribute_variables(value, nid, value_getter, reg_exp, left_tag, right_tag) assert res == u'''test12312345blabla;>'''
def test_replace_attribute_variables_other_regex(): value = '''test123<att:test_var>blabla;>''' nid = 12345 reg_exp = r"\<(.+?)\>" left_tag = "<" right_tag = ">" value_getter = lambda attr_name: { 'test_var': 'OK', 'x': '---' }.get(attr_name) res = replace_attribute_variables(value, nid, value_getter, reg_exp, left_tag, right_tag) assert res == u'test123OKblabla;>'
def getFormattedValue(self, metafield, maskitem, mask, node, language, html=True, template_from_caller=None): value = node.get_special(metafield.name) # consider int, long values like filesize if isinstance(value, (int, long)): value = str(value) value = value.replace(";", "; ") # ignore trailing newlines for textfields value = value.rstrip("\r\n") if value.find('\n') != -1: valuesList = value.split('\n') if any(lang in valuesList for lang in config.languages): # treat as multilingual index = 0 try: index = valuesList.index(language) value = valuesList[index + 1] except ValueError as e: msg = "Exception in getFormattedValue for textfield:\n" msg += " valuesList=%r\n" % valuesList msg += " node.name=%r, node.id=%r, node.type=%r\n" % (node.name, node.id, node.type) msg += " metafield.name=%r, metafield.id=%r, metafield.type=%r\n" % (metafield.name, metafield.id, metafield.type) msg += " language=%r, mask=%r" % (language, mask) logg.exception(msg) value = u"" else: # treat as monolingual pass if html: value = esc(value) # replace variables # substitute TeX sub/super-scripts with <sub>/<sup> html tags value = modify_tex(value, 'html') value = replace_attribute_variables(value, node.id, node.get, r'<(.+?)>', "<", ">") value = value.replace("<", "<").replace(">", ">") if not maskitem: return (metafield.getLabel(), value) # use default value from mask if value is empty if value == u'': value = maskitem.getDefault() return (metafield.getLabel(), value)
def _replace_vars(node, s): s = replace_attribute_variables(s, node.id, node.get_special, r'<(.+?)>', "<", ">") s = replace_attribute_variables(s, node.id, node.get_special, r'\[(.+?)\]', "[", "]") return s
def render_mask_template(node, mask, field_descriptors, language, words=None, separator="", skip_empty_fields=True): res = [] for node_attribute, fd in field_descriptors: metafield_type = fd['metafield_type'] maskitem = fd['maskitem'] metafield = fd["metafield"] metatype = fd["metatype"] maskitem_type = fd["maskitem_type"] if metafield_type in ['date', 'url', 'hlist']: value = metatype.getFormattedValue(metafield, maskitem, mask, node, language)[1] elif metafield_type in ['field']: if maskitem_type in ['hgroup', 'vgroup']: _sep = '' if maskitem_type == 'hgroup': fd['unit'] = '' # unit will be taken from definition of the hgroup use_label = False else: use_label = True value = getMetadataType(maskitem_type).getViewHTML( metafield, [node], # nodes 0, # flags language=language, mask=mask, use_label=use_label) else: value = node.get_special(node_attribute) if hasattr(metatype, "language_snipper"): if (metafield.get("type") == "text" and metafield.get("valuelist") == "multilingual") \ or \ (metafield.get("type") in ['memo', 'htmlmemo'] and metafield.get("multilang") == '1'): value = metatype.language_snipper(value, language) if value.find('<') >= 0: # replace variables value = replace_attribute_variables(value, node.id, node.get_special, r'<(.+?)>', "<", ">") value = value.replace("<", "<").replace(">", ">") if value.find('<') >= 0: # replace variables value = replace_attribute_variables(value, node.id, node.get_special, r'\<(.+?)\>', "<", ">") value = value.replace("<", "<").replace(">", ">") default = fd['default'] if not value and default: value = default if skip_empty_fields and not value: continue if fd["unit"]: value = value + " " + fd["unit"] if fd["format"]: value = fd["format"].replace("<value>", value) if words: value = highlight(value, words, '<font class="hilite">', "</font>") res.append(fd["template"] % value) return separator.join(res)