예제 #1
0
    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'&lt;(.+?)&gt;', "&lt;", "&gt;")
        value = value.replace("&lt;", "<").replace("&gt;", ">")

        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)
예제 #2
0
    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'&lt;(.+?)&gt;', "&lt;", "&gt;")

        return (metafield.getLabel(), value)
예제 #3
0
def test_replace_attribute_variables_unicode_val():
    value = '''test123&lt;att:test_var&gt;blabla;&gt;'''
    nid = 12345
    reg_exp = r"&lt;(.+?)&gt;"
    left_tag = "&lt;"
    right_tag = "&gt;"
    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;&gt;'''
예제 #4
0
def test_replace_attribute_variables_other_regex():
    value = '''test123<att:test_var>blabla;&gt;'''
    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;&gt;'
예제 #5
0
def test_replace_attribute_variables_id():
    value = '''test123&lt;att:id&gt;blabla;&gt;'''
    nid = 12345
    reg_exp = r"&lt;(.+?)&gt;"
    left_tag = "&lt;"
    right_tag = "&gt;"
    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;&gt;'''
예제 #6
0
def test_replace_attribute_variables_other_regex():
    value = '''test123<att:test_var>blabla;&gt;'''
    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;&gt;'
예제 #7
0
파일: text.py 프로젝트: mediatum/mediatum
    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'&lt;(.+?)&gt;', "&lt;", "&gt;")
        value = value.replace("&lt;", "<").replace("&gt;", ">")

        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)
예제 #8
0
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
예제 #9
0
파일: data.py 프로젝트: mediatum/mediatum
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('&lt;') >= 0:
                # replace variables
                value = replace_attribute_variables(value, node.id, node.get_special, r'&lt;(.+?)&gt;', "&lt;", "&gt;")
                value = value.replace("&lt;", "<").replace("&gt;", ">")

            if value.find('<') >= 0:
                # replace variables
                value = replace_attribute_variables(value, node.id, node.get_special, r'\<(.+?)\>', "<", ">")
                value = value.replace("&lt;", "<").replace("&gt;", ">")

            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)
예제 #10
0
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('&lt;') >= 0:
                # replace variables
                value = replace_attribute_variables(value, node.id,
                                                    node.get_special,
                                                    r'&lt;(.+?)&gt;', "&lt;",
                                                    "&gt;")
                value = value.replace("&lt;", "<").replace("&gt;", ">")

            if value.find('<') >= 0:
                # replace variables
                value = replace_attribute_variables(value, node.id,
                                                    node.get_special,
                                                    r'\<(.+?)\>', "<", ">")
                value = value.replace("&lt;", "<").replace("&gt;", ">")

            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)
예제 #11
0
파일: url.py 프로젝트: mediatum/mediatum
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