Esempio n. 1
0
    def getFormatedValue(self,
                         field,
                         node,
                         language=None,
                         html=1,
                         template_from_caller=None,
                         mask=None):

        value = node.get(field.getName()).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 system_languages):  # treat as multilingual
                index = 0
                try:
                    index = valuesList.index(language)
                    value = valuesList[index + 1]
                except ValueError as e:
                    logException(e)

                    log = logging.getLogger("errors")
                    msg = "Exception in getFormatedValue 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 += " field.name=%r, field.id=%r, field.type=%r\n" % (
                        field.name, field.id, field.type)
                    msg += " language=%r, mask=%r" % (language, mask)
                    log.error(msg)

                    value = ""
            else:
                # treat as monolingual
                pass
        unescaped_value = value

        if html:
            value = esc(value)

        # replace variables
        # substitute TeX sub/super-scripts with <sub>/<sup> html tags
        value = modify_tex(value, 'html')

        for var in re.findall(r'&lt;(.+?)&gt;', value):
            if var == "att:id":
                value = value.replace("&lt;" + var + "&gt;", node.id)
            elif var.startswith("att:"):
                val = node.get(var[4:])
                if val == "":
                    val = "____"

                value = value.replace("&lt;" + var + "&gt;", val)
        value = value.replace("&lt;", "<").replace("&gt;", ">")

        maskitem = getMaskitemForField(field, language=language, mask=mask)
        if not maskitem:
            return (field.getLabel(), value)

        # use default value from mask if value is empty
        if value == '':
            value = maskitem.getDefault()

        if template_from_caller and template_from_caller[
                0] and maskitem and str(
                    maskitem.id) == template_from_caller[3]:
            value = template_from_caller[0]

        context = {'node': node, 'host': "http://" + config.get("host.name")}

        if (template_from_caller and
                template_from_caller[0]) and (not node.get(field.getName())):
            value = runTALSnippet(value, context)
        else:
            try:
                value = runTALSnippet(value, context)
            except:
                value = runTALSnippet(unescaped_value, context)

        return (field.getLabel(), value)
Esempio n. 2
0
    def getFormatedValue(self, field, node, language=None, html=1, template_from_caller=None, mask=None):

        value = node.get(field.getName()).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 system_languages):  # treat as multilingual
                index = 0
                try:
                    index = valuesList.index(language)
                    value = valuesList[index + 1]
                except ValueError as e:
                    logException(e)

                    log = logging.getLogger("errors")
                    msg = "Exception in getFormatedValue 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 += " field.name=%r, field.id=%r, field.type=%r\n" % (field.name, field.id, field.type)
                    msg += " language=%r, mask=%r" % (language, mask)
                    log.error(msg)

                    value = ""
            else:
                # treat as monolingual
                pass
        unescaped_value = value

        if html:
            value = esc(value)

        # replace variables
        # substitute TeX sub/super-scripts with <sub>/<sup> html tags
        value = modify_tex(value, 'html')

        for var in re.findall(r'&lt;(.+?)&gt;', value):
            if var == "att:id":
                value = value.replace("&lt;" + var + "&gt;", node.id)
            elif var.startswith("att:"):
                val = node.get(var[4:])
                if val == "":
                    val = "____"

                value = value.replace("&lt;" + var + "&gt;", val)
        value = value.replace("&lt;", "<").replace("&gt;", ">")

        maskitem = getMaskitemForField(field, language=language, mask=mask)
        if not maskitem:
            return (field.getLabel(), value)

        # use default value from mask if value is empty
        if value == '':
            value = maskitem.getDefault()

        if template_from_caller and template_from_caller[0] and maskitem and str(maskitem.id) == template_from_caller[3]:
            value = template_from_caller[0]

        context = {'node': node, 'host': "http://" + config.get("host.name")}

        if (template_from_caller and template_from_caller[0]) and (not node.get(field.getName())):
            value = runTALSnippet(value, context)
        else:
            try:
                value = runTALSnippet(value, context)
            except:
                value = runTALSnippet(unescaped_value, context)

        return (field.getLabel(), value)
Esempio n. 3
0
        def render_mask_template(node,
                                 mfs,
                                 words=None,
                                 separator="",
                                 skip_empty_fields=True):
            """
               mfs: [mask] + list_of_maskfields
            """
            res = []
            exception_count = {}
            mask = mfs[0]
            for node_attribute, fd in mfs[1:]:
                metafield_type = fd['metafield_type']
                field_type = fd['field_type']
                if metafield_type in ['date', 'url', 'hlist']:
                    exception_count[
                        metafield_type] = exception_count.setdefault(
                            metafield_type, 0) + 1
                    value = node.get(node_attribute)
                    try:
                        value = fd['metadatatype'].getFormatedValue(
                            fd['element'], node, language=language,
                            mask=mask)[1]
                    except:
                        value = fd['metadatatype'].getFormatedValue(
                            fd['element'], node, language=language)[1]
                elif metafield_type in ['field']:
                    if field_type in ['hgroup', 'vgroup']:
                        _sep = ''
                        if field_type == 'hgroup':
                            fd['unit'] = ''  # unit will be taken from definition of the hgroup
                            use_label = False
                        else:
                            use_label = True
                        value = getMetadataType(field_type).getViewHTML(
                            fd['field'],  # field
                            [node],  # nodes
                            0,  # flags
                            language=language,
                            mask=mask,
                            use_label=use_label)
                else:
                    value = node.get(node_attribute)
                    metadatatype = fd['metadatatype']

                    if hasattr(metadatatype, "language_snipper"):
                        metafield = fd['element']
                        if (metafield.get("type") == "text" and metafield.get("valuelist") == "multilingual") \
                            or \
                           (metafield.get("type") in ['memo', 'htmlmemo'] and metafield.get("multilang") == '1'):
                            value = metadatatype.language_snipper(
                                value, language)

                    if value.find('&lt;') >= 0:
                        # replace variables
                        for var in re.findall(r'&lt;(.+?)&gt;', value):
                            if var == "att:id":
                                value = value.replace("&lt;" + var + "&gt;",
                                                      node.id)
                            elif var.startswith("att:"):
                                val = node.get(var[4:])
                                if val == "":
                                    val = "____"

                                value = value.replace("&lt;" + var + "&gt;",
                                                      val)
                        value = value.replace("&lt;", "<").replace("&gt;", ">")

                    if value.find('<') >= 0:
                        # replace variables
                        for var in re.findall(r'\<(.+?)\>', value):
                            if var == "att:id":
                                value = value.replace("<" + var + ">", node.id)
                            elif var.startswith("att:"):
                                val = node.get(var[4:])
                                if val == "":
                                    val = "____"

                                value = value.replace("&lt;" + var + "&gt;",
                                                      val)
                        value = value.replace("&lt;", "<").replace("&gt;", ">")

                    if value.find('tal:') >= 0:
                        context['node'] = node
                        value = runTALSnippet(value, context)

                    # don't escape before running TAL
                    if (not value) and fd['default']:
                        default = fd['default']
                        if fd['default_has_tal']:
                            context['node'] = node
                            value = runTALSnippet(default, context)
                        else:
                            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)
            if exception_count and len(exception_count.keys()) > 1:
                pass
            return separator.join(res)
Esempio n. 4
0
        def render_mask_template(node, mfs, words=None, separator="", skip_empty_fields=True):
            """
               mfs: [mask] + list_of_maskfields
            """
            res = []
            exception_count = {}
            mask = mfs[0]
            for node_attribute, fd in mfs[1:]:
                metafield_type = fd['metafield_type']
                field_type = fd['field_type']
                if metafield_type in ['date', 'url', 'hlist']:
                    exception_count[metafield_type] = exception_count.setdefault(metafield_type, 0) + 1
                    value = node.get(node_attribute)
                    try:
                        value = fd['metadatatype'].getFormatedValue(fd['element'], node, language=language, mask=mask)[1]
                    except:
                        value = fd['metadatatype'].getFormatedValue(fd['element'], node, language=language)[1]
                elif metafield_type in ['field']:
                    if field_type in ['hgroup', 'vgroup']:
                        _sep = ''
                        if field_type == 'hgroup':
                            fd['unit'] = ''  # unit will be taken from definition of the hgroup
                            use_label = False
                        else:
                            use_label = True
                        value = getMetadataType(field_type).getViewHTML(
                                                                         fd['field'],  # field
                                                                         [node],  # nodes
                                                                         0,  # flags
                                                                         language=language,
                                                                         mask=mask, use_label=use_label)
                else:
                    value = node.get(node_attribute)
                    metadatatype = fd['metadatatype']

                    if hasattr(metadatatype, "language_snipper"):
                        metafield = fd['element']
                        if (metafield.get("type") == "text" and metafield.get("valuelist") == "multilingual") \
                            or \
                           (metafield.get("type") in ['memo', 'htmlmemo'] and metafield.get("multilang") == '1'):
                            value = metadatatype.language_snipper(value, language)

                    if value.find('&lt;') >= 0:
                        # replace variables
                        for var in re.findall(r'&lt;(.+?)&gt;', value):
                            if var == "att:id":
                                value = value.replace("&lt;" + var + "&gt;", node.id)
                            elif var.startswith("att:"):
                                val = node.get(var[4:])
                                if val == "":
                                    val = "____"

                                value = value.replace("&lt;" + var + "&gt;", val)
                        value = value.replace("&lt;", "<").replace("&gt;", ">")

                    if value.find('<') >= 0:
                        # replace variables
                        for var in re.findall(r'\<(.+?)\>', value):
                            if var == "att:id":
                                value = value.replace("<" + var + ">", node.id)
                            elif var.startswith("att:"):
                                val = node.get(var[4:])
                                if val == "":
                                    val = "____"

                                value = value.replace("&lt;" + var + "&gt;", val)
                        value = value.replace("&lt;", "<").replace("&gt;", ">")

                    if value.find('tal:') >= 0:
                        context['node'] = node
                        value = runTALSnippet(value, context)

                    # don't escape before running TAL
                    if (not value) and fd['default']:
                        default = fd['default']
                        if fd['default_has_tal']:
                            context['node'] = node
                            value = runTALSnippet(default, context)
                        else:
                            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)
            if exception_count and len(exception_count.keys()) > 1:
                pass
            return separator.join(res)