コード例 #1
0
    def getEditorHTML(self,
                      field,
                      value="",
                      width=400,
                      lock=0,
                      language=None,
                      required=None):

        ns = ""
        if field.get("fieldtype") == "mapping":
            field = tree.getNode(field.get("mappingfield"))
            ns = field.getMapping().getNamespace()
            if ns != "":
                ns += ":"
            format = field.getExportFormat()
            field_value = ns + field.getName()
        else:
            format = field.get("mappingfield")
            field_value = field.getName()
        format = esc(format)
        for var in re.findall(r'\[(.+?)\]', format):
            if var.startswith("att:"):
                format = format.replace("[" + var + "]",
                                        '<i>{attribute:' + var[4:] + '}</i>')
            elif var == "field":
                format = format.replace("[field]", field_value)
            elif var == "value":
                format = format.replace("[value]", '<i>{' + value + '}</i>')
            elif var == "ns":
                format = format.replace("[value]", '<i>{namspaces}</i>')
        format = format.replace("\\t", "")
        return format
コード例 #2
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)
コード例 #3
0
ファイル: mappingfield.py プロジェクト: hibozzy/mediatum
    def getEditorHTML(self, field, value="", width=400, lock=0, language=None, required=None):

        ns = ""
        if field.get("fieldtype") == "mapping":
            field = tree.getNode(field.get("mappingfield"))
            ns = field.getMapping().getNamespace()
            if ns != "":
                ns += ":"
            format = field.getExportFormat()
            field_value = ns + field.getName()
        else:
            format = field.get("mappingfield")
            field_value = field.getName()
        format = esc(format)
        for var in re.findall(r"\[(.+?)\]", format):
            if var.startswith("att:"):
                format = format.replace("[" + var + "]", "<i>{attribute:" + var[4:] + "}</i>")
            elif var == "field":
                format = format.replace("[field]", field_value)
            elif var == "value":
                format = format.replace("[value]", "<i>{" + value + "}</i>")
            elif var == "ns":
                format = format.replace("[value]", "<i>{namspaces}</i>")
        format = format.replace("\\t", "")
        return format
コード例 #4
0
    def addChildren(self, children):
        self.addData(Paragraph('%s:' % t(self.language, "print_view_children"), self.bp))

        _head = 0  # count headers
        for c in children:
            if len(c) > 0 and c[0][3] == "header":
                _head += 1

        items = []
        _c = 1
        for c in children:
            if len(c) > 0 and c[0][3] == "header":
                for item in items:
                    self.addData(Paragraph("[%s/%s]: %s" % (_c, len(children) - _head, "; ".join(item)), self.bv))
                    _c += 1
                self.addData(Paragraph(u(c[0][1]).replace('&', '&amp;'), self.bf))
                items = []
                continue

            values = []
            for item in c:
                if item[1].strip() != "":
                    values.append(item[1])
            items.append(values)

        for item in items:
            try:
                self.addData(Paragraph("[%s/%s]: %s" % (_c, len(children) - _head, ", ".join(item)), self.bv))
                _c += 1
            except:
                self.addData(Paragraph("[%s/%s]: %s" % (_c, len(children) - _head, esc(", ".join(item))), self.bv))
コード例 #5
0
ファイル: dlist.py プロジェクト: schnittstabil/mediatum
    def formatValues(self, context):
        valuelist = []

        items = {}
        try:
            n = context.collection
            if not isinstance(n, Node):
                raise KeyError
            field_name = context.field.getName()
            id_attr_val = n.all_children_by_query(
                q(Node.id, Node.a[field_name]).filter(
                    Node.a[field_name] != None
                    and Node.a[field_name] != '').distinct(Node.a[field_name]))
            items = {pair[0]: pair[1] for pair in id_attr_val}
        except KeyError:
            None

        value = context.value.split(";")

        for val in context.field.getValueList():
            indent = 0
            canbeselected = 0
            while val.startswith("*"):
                val = val[1:]
                indent = indent + 1
            if val.startswith(" "):
                canbeselected = 1
            val = val.strip()
            if not indent:
                canbeselected = 1
            if indent > 0:
                indent = indent - 1
            indentstr = "&nbsp;" * (2 * indent)

            num = 0
            if val in items.keys():
                num = int(items[val])

            try:
                if int(num) < 0:
                    raise u""
                elif int(num) == 0:
                    num = u""
                else:
                    num = u" (" + unicode(num) + u")"
            except:
                logg.exception("exception in formatValues, using empty string")
                num = u""

            val = esc(val)

            if not canbeselected:
                valuelist.append(
                    ("optgroup",
                     "<optgroup label=\"" + indentstr + val + "\">", "", ""))
            elif (val in value):
                valuelist.append(("optionselected", indentstr, val, num))
            else:
                valuelist.append(("option", indentstr, val, num))
        return valuelist
コード例 #6
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)
コード例 #7
0
ファイル: ilist.py プロジェクト: mediatum/mediatum
    def getFormattedValue(self, metafield, maskitem, mask, node, language, html=True):
        value = node.get(metafield.getName())
        with suppress(Exception):
            if value and value[-1] == ";":
                value = value[0:-1]

        value = value.replace(";", "; ")
        if html:
            value = esc(value)
        return (metafield.getLabel(), value)
コード例 #8
0
ファイル: dlist.py プロジェクト: hibozzy/mediatum
    def formatValues(self, context):
        valuelist = []

        items = {}
        try:
            n = context.collection
            if n is None:
                raise tree.NoSuchNodeError()
            items = n.getAllAttributeValues(context.field.getName(),
                                            context.access)
        except tree.NoSuchNodeError:
            None

        value = context.value.split(";")

        for val in context.field.getValueList():
            indent = 0
            canbeselected = 0
            while val.startswith("*"):
                val = val[1:]
                indent = indent + 1
            if val.startswith(" "):
                canbeselected = 1
            val = val.strip()
            if not indent:
                canbeselected = 1
            if indent > 0:
                indent = indent - 1
            indentstr = "&nbsp;" * (2 * indent)

            num = 0
            if val in items.keys():
                num = int(items[val])

            try:
                if int(num) < 0:
                    raise ""
                elif int(num) == 0:
                    num = ""
                else:
                    num = " (" + str(num) + ")"
            except:
                num = ""

            val = esc(val)

            if not canbeselected:
                valuelist.append(
                    ("optgroup",
                     "<optgroup label=\"" + indentstr + val + "\">", "", ""))
            elif (val in value):
                valuelist.append(("optionselected", indentstr, val, num))
            else:
                valuelist.append(("option", indentstr, val, num))
        return valuelist
コード例 #9
0
ファイル: dlist.py プロジェクト: mediatum/mediatum
    def formatValues(self, context):
        valuelist = []

        items = {}
        try:
            n = context.collection
            if not isinstance(n, Node):
                raise KeyError
            field_name = context.field.getName()
            id_attr_val = n.all_children_by_query(q(Node.id, Node.a[field_name]).filter(Node.a[field_name] != None and Node.a[field_name] != '').distinct(Node.a[field_name]))
            items = {pair[0]: pair[1] for pair in id_attr_val}
        except KeyError:
            None

        value = context.value.split(";")

        for val in context.field.getValueList():
            indent = 0
            canbeselected = 0
            while val.startswith("*"):
                val = val[1:]
                indent = indent + 1
            if val.startswith(" "):
                canbeselected = 1
            val = val.strip()
            if not indent:
                canbeselected = 1
            if indent > 0:
                indent = indent - 1
            indentstr = "&nbsp;" * (2 * indent)

            num = 0
            if val in items.keys():
                num = int(items[val])

            try:
                if int(num) < 0:
                    raise u""
                elif int(num) == 0:
                    num = u""
                else:
                    num = u" (" + unicode(num) + u")"
            except:
                logg.exception("exception in formatValues, using empty string")
                num = u""

            val = esc(val)

            if not canbeselected:
                valuelist.append(("optgroup", "<optgroup label=\"" + indentstr + val + "\">", "", ""))
            elif (val in value):
                valuelist.append(("optionselected", indentstr, val, num))
            else:
                valuelist.append(("option", indentstr, val, num))
        return valuelist
コード例 #10
0
ファイル: treeselect.py プロジェクト: hibozzy/mediatum
 def getFormatedValue_(self,
                       field,
                       node,
                       language=None,
                       html=1,
                       template_from_caller=None,
                       mask=None):
     value = node.get(field.getName())
     if html:
         value = esc(value)
     return (field.getLabel(), value)
コード例 #11
0
ファイル: dlist.py プロジェクト: schnittstabil/mediatum
 def getFormattedValue(self,
                       metafield,
                       maskitem,
                       mask,
                       node,
                       language,
                       html=True):
     value = node.get(metafield.getName())
     if html:
         value = esc(value)
     return (metafield.getLabel(), value)
コード例 #12
0
ファイル: ilist.py プロジェクト: hibozzy/mediatum
    def getFormatedValue(self, field, node, language=None, html=1):
        value = node.get(field.getName())
        try:
            if value[-1] == ";":
                value = value[0:-1]
        except:
            pass

        value = value.replace(";", "; ")
        if html:
            value = esc(value)
        return (field.getLabel(), value)
コード例 #13
0
ファイル: ilist.py プロジェクト: agromsl/mediatum
    def getFormatedValue(self, field, node, language=None, html=1):
        value = node.get(field.getName())
        try:
            if value[-1] == ";":
                value = value[0:-1]
        except:
            pass

        value = value.replace(";", "; ")
        if html:
            value = esc(value)
        return (field.getLabel(), value)
コード例 #14
0
ファイル: dlist.py プロジェクト: hibozzy/mediatum
    def formatValues(self, context):
        valuelist = []

        items = {}
        try:
            n = context.collection
            if n is None:
                raise tree.NoSuchNodeError()
            items = n.getAllAttributeValues(context.field.getName(), context.access)
        except tree.NoSuchNodeError:
            None

        value = context.value.split(";")

        for val in context.field.getValueList():
            indent = 0
            canbeselected = 0
            while val.startswith("*"):
                val = val[1:]
                indent = indent + 1
            if val.startswith(" "):
                canbeselected = 1
            val = val.strip()
            if not indent:
                canbeselected = 1
            if indent > 0:
                indent = indent - 1
            indentstr = "&nbsp;" * (2 * indent)

            num = 0
            if val in items.keys():
                num = int(items[val])

            try:
                if int(num) < 0:
                    raise ""
                elif int(num) == 0:
                    num = ""
                else:
                    num = " (" + str(num) + ")"
            except:
                num = ""

            val = esc(val)

            if not canbeselected:
                valuelist.append(("optgroup", '<optgroup label="' + indentstr + val + '">', "", ""))
            elif val in value:
                valuelist.append(("optionselected", indentstr, val, num))
            else:
                valuelist.append(("option", indentstr, val, num))
        return valuelist
コード例 #15
0
    def getFormattedValue(self, metafield, maskitem, mask, node, language, html=True):
        value = node.get(metafield.getName())
        try:
            if value and value[-1] == ";":
                value = value[0:-1]
        except:
            logg.exception("exception in getFormattedValue, ignore")
            pass

        value = value.replace(";", "; ")
        if html:
            value = esc(value)
        return (metafield.getLabel(), value)
コード例 #16
0
ファイル: indexer.py プロジェクト: hibozzy/mediatum
    def nodeToFulltextSearch(self, node):
        # build fulltext index from node

        if not node.getContentType() in ("document", "dissertation"):
            # only build fulltext of document nodes
            # print "object is no document"
            return True
        r = re.compile("[a-zA-Z0-9]+")

        for file in node.getFiles():
            w = ''
            if file.getType() == "fulltext" and os.path.exists(
                    file.retrieveFile()):
                data = {}
                content = ''
                f = open(file.retrieveFile())
                try:
                    for line in f:
                        if FULLTEXT_INDEX_MODE == 0:
                            content += u(line)
                        else:
                            for w in re.findall(r, line):
                                if w not in data.keys():
                                    data[w] = 1
                            try:
                                data[w] += 1
                            except KeyError:
                                data[w] = 1
                finally:
                    f.close()

                if FULLTEXT_INDEX_MODE == 1:
                    for key in data.keys():
                        content += key + " "
                elif FULLTEXT_INDEX_MODE == 2:
                    for key in data.keys():
                        content += key + " [" + str(data[key]) + "] "
                sql = ""
                if len(content) > 0:
                    try:
                        sql = 'INSERT INTO textsearchmeta (id, type, schema, value) VALUES("{}", "{}", "{}", "{}")'.format(
                            node.id, node.getContentType(), node.getSchema(),
                            iso2utf8(esc(content)))
                        self.db.execute(sql)
                    except:
                        print "error", node.id, "\n"
                        return False
                else:
                    print "no Content"
        return True
コード例 #17
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)
コード例 #18
0
ファイル: watermark.py プロジェクト: agromsl/mediatum
    def getFormatedValue(self, field, node, language=None, html=1):
        value = node.get(field.getName()).replace(";", "; ")
        if html:
            value = esc(value)
        # 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)

        return (field.getLabel(), value)
コード例 #19
0
    def getFormattedValue(self,
                          metafield,
                          maskitem,
                          mask,
                          node,
                          language,
                          html=True):
        value = node.get(metafield.getName())
        with suppress(Exception):
            if value and value[-1] == ";":
                value = value[0:-1]

        value = value.replace(";", "; ")
        if html:
            value = esc(value)
        return (metafield.getLabel(), value)
コード例 #20
0
    def getFormatedValue(self, field, node, language=None, html=1):
        value = node.get(field.getName()).replace(";", "; ")
        if html:
            value = esc(value)
        # 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)

        return (field.getLabel(), value)
コード例 #21
0
ファイル: printview.py プロジェクト: agromsl/mediatum
    def addMetaData(self, metadata):
        """ format given metadatalist for pdf output """
        max_width = 0

        for item in metadata:
            l = Paragraph(item[2] + ":", self.bl)

            if max_width < l.minWidth():
                max_width = l.minWidth()

        self.bv.leftIndent = max_width + 10
        self.bv.bulletIndent = max_width + 10

        for item in metadata:
            l = Paragraph(esc(item[2] + ":"), self.bl)
            v = Paragraph(re.sub(r'<[^>]*>', '', item[1]), self.bv)
            self.addData(l)
            self.addData(v)
コード例 #22
0
    def addMetaData(self, metadata):
        """ format given metadatalist for pdf output """
        max_width = 0

        for item in metadata:
            l = Paragraph(item[2] + ":", self.bl)

            if max_width < l.minWidth():
                max_width = l.minWidth()

        self.bv.leftIndent = max_width + 10
        self.bv.bulletIndent = max_width + 10

        for item in metadata:
            l = Paragraph(esc(item[2] + ":"), self.bl)
            v = Paragraph(re.sub(r'<[^>]*>', '', item[1]), self.bv)
            self.addData(l)
            self.addData(v)
コード例 #23
0
ファイル: oaisets.py プロジェクト: agromsl/mediatum
def build_container_group():
    # sets configured with container attributes
    node_list = tree.NodeList(tree.getNodesByAttribute('oai.setname', '*'))
    node_list = node_list.sort_by_fields(field="oai.setname")
    node_list = [node for node in node_list if node.type in ['collection', 'directory']]
    node_list = [node for node in node_list if node.get('oai.setname').strip()]
    node_list = [node for node in node_list if node.get('oai.formats').strip()]

    d_names = OrderedDict()
    for col_node in node_list:
        d_names[str(col_node.id)] = esc(col_node.get('oai.setname'))

    g = OAISetGroup(d_names, descr='group of %d container sets' % len(d_names))

    g.func_getNodesForSetSpec = func_getNodesForSetSpec
    g.func_getSetSpecsForNode = func_getSetSpecsForNode
    g.sortorder = '040'
    g.group_identifier = 'oaigroup_containers'
    return g
コード例 #24
0
ファイル: oai.py プロジェクト: agromsl/mediatum
def writeHead(req, attributes=""):
    request = mklink(req)
    d = ISO8601(date.now())
    try:
        verb = req.params["verb"]
    except KeyError:
        verb = ""
    req.reply_headers['charset'] = 'utf-8'
    req.reply_headers['Content-Type'] = 'text/xml; charset=utf-8'
    req.write("""<?xml version="1.0" encoding="UTF-8"?>
    <OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd">
        <responseDate>%sZ</responseDate>
        <request""" % (ISO8601(date.now())))
    if attributes != "noatt":
        for n in ["verb", "identifier", "metadataprefix", "from", "until", "set"]:
            if n in req.params:
                req.write(' %s="%s"' % (n, esc(req.params[n])))
    req.write('>%s</request>' % (request))
    if DEBUG:
        timetable_update(req, "leaving writeHead")
コード例 #25
0
def writeHead(req, attributes=""):
    request = mklink(req)
    d = ISO8601(date.now())
    try:
        verb = req.params["verb"]
    except KeyError:
        verb = ""
    req.reply_headers['charset'] = 'utf-8'
    req.reply_headers['Content-Type'] = 'text/xml; charset=utf-8'
    req.write("""<?xml version="1.0" encoding="UTF-8"?>
    <OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd">
        <responseDate>%sZ</responseDate>
        <request""" % (ISO8601(date.now())))
    if attributes != "noatt":
        for n in ["verb", "identifier", "metadataprefix", "from", "until", "set"]:
            if n in req.params:
                req.write(' %s="%s"' % (n, esc(req.params[n])))
    req.write('>%s</request>' % (request))
    if DEBUG:
        timetable_update(req, "leaving writeHead")
コード例 #26
0
def build_container_group():
    # sets configured with container attributes
    node_list = tree.NodeList(tree.getNodesByAttribute('oai.setname', '*'))
    node_list = node_list.sort_by_fields(field="oai.setname")
    node_list = [
        node for node in node_list if node.type in ['collection', 'directory']
    ]
    node_list = [node for node in node_list if node.get('oai.setname').strip()]
    node_list = [node for node in node_list if node.get('oai.formats').strip()]

    d_names = OrderedDict()
    for col_node in node_list:
        d_names[str(col_node.id)] = esc(col_node.get('oai.setname'))

    g = OAISetGroup(d_names, descr='group of %d container sets' % len(d_names))

    g.func_getNodesForSetSpec = func_getNodesForSetSpec
    g.func_getSetSpecsForNode = func_getSetSpecsForNode
    g.sortorder = '040'
    g.group_identifier = 'oaigroup_containers'
    return g
コード例 #27
0
ファイル: oaisets.py プロジェクト: mediatum/mediatum
def build_container_group():
    node_list = q(Node).filter(Node.attrs['oai.setname'].isnot(None))
    node_list = node_list.order_by(Node.attrs['oai.setname'])
    node_list = [node for node in node_list if node.type in ['collection', 'directory']]
    node_list = [node for node in node_list if node.get('oai.setname').strip()]
    node_list = [node for node in node_list if node.get('oai.formats').strip()]

    d_names = OrderedDict()
    d_filters = OrderedDict()

    for col_node in node_list:
        d_names[ustr(col_node.id)] = esc(col_node.get('oai.setname'))
        d_filters[ustr(col_node.id)] = None  # todo: fix this

    g = OAISetGroup(d_names, descr='group of %d container sets' % len(d_names))

    g.func_getNodesForSetSpec = func_getNodesForSetSpec
    g.func_getSetSpecsForNode = func_getSetSpecsForNode
    g.func_get_nodes_query_for_setspec = get_nodes_query_for_container_setspec
    g.sortorder = '040'
    g.group_identifier = 'oaigroup_containers'
    return g
コード例 #28
0
def build_container_group():
    node_list = q(Node).filter(Node.attrs['oai.setname'].isnot(None))
    node_list = node_list.order_by(Node.attrs['oai.setname'])
    node_list = [
        node for node in node_list if node.type in ['collection', 'directory']
    ]
    node_list = [node for node in node_list if node.get('oai.setname').strip()]
    node_list = [node for node in node_list if node.get('oai.formats').strip()]

    d_names = OrderedDict()
    d_filters = OrderedDict()

    for col_node in node_list:
        d_names[ustr(col_node.id)] = esc(col_node.get('oai.setname'))
        d_filters[ustr(col_node.id)] = None  # todo: fix this

    g = OAISetGroup(d_names, descr='group of %d container sets' % len(d_names))

    g.func_getNodesForSetSpec = func_getNodesForSetSpec
    g.func_getSetSpecsForNode = func_getSetSpecsForNode
    g.func_get_nodes_query_for_setspec = get_nodes_query_for_container_setspec
    g.sortorder = '040'
    g.group_identifier = 'oaigroup_containers'
    return g
コード例 #29
0
ファイル: union.py プロジェクト: hibozzy/mediatum
 def getFormatedValue(self, field, node, language=None, html=1):
     value = node.get(field.getName()).replace(";", "; ")
     if html:
         value = esc(value)
     return (field.getLabel(), value)
コード例 #30
0
ファイル: metatype.py プロジェクト: mediatum/mediatum
def validate(req, op):
    path = req.path[1:].split("/")

    if len(path) == 3 and path[2] == "overview":
        return showFieldOverview(req)

    if len(path) == 4 and path[3] == "editor":
        res = showEditor(req)
        return res

    if len(path) == 5 and path[3] == "editor" and path[4] == "show_testnodes":
        
        raise NotImplementedError("")

        template = req.params.get('template', '')
        testnodes_list = req.params.get('testnodes', '')
        width = req.params.get('width', '400')
        item_id = req.params.get('item_id', None)

        mdt_name = path[1]
        mask_name = path[2]

        mdt = q(Metadatatypes).one().children.filter_by(name=mdt_name).one()
        mask = mdt.children.filter_by(name=mask_name).one()

        sectionlist = []
        for nid in [x.strip() for x in testnodes_list.split(',') if x.strip()]:
            section_descr = {}
            section_descr['nid'] = nid
            section_descr['error_flag'] = ''  # in case of no error

            node = q(Node).get(nid)
            section_descr['node'] = node
            if node and node.has_data_access():
                try:
                    node_html = mask.getViewHTML([node], VIEW_DEFAULT, template_from_caller=[template, mdt, mask, item_id])
                    section_descr['node_html'] = node_html
                except:
                    logg.exception("exception while evaluating template")
                    error_text = str(sys.exc_info()[1])
                    template_line = 'for node id ' + ustr(nid) + ': ' + error_text
                    with suppress(Exception, warn=False):
                        m = re.match(r".*line (?P<line>\d*), column (?P<column>\d*)", error_text)
                        if m:
                            mdict = m.groupdict()
                            line = int(mdict.get('line', 0))
                            column = int(mdict.get('column', 0))
                            error_text = error_text.replace('line %d' % line, 'template line %d' % (line - 1))
                            template_line = 'for node id ' + ustr(nid) + '<br/>' + error_text + '<br/><code>' + esc(
                                template.split(
                                    "\n")[line - 2][0:column - 1]) + '<span style="color:red">' + esc(
                                template.split("\n")[line - 2][column - 1:]) + '</span></code>'
                    section_descr['error_flag'] = 'Error while evaluating template:'
                    section_descr['node_html'] = template_line
            elif node and not node.has_data_access():
                section_descr['error_flag'] = 'no access'
                section_descr['node_html'] = ''
            if node is None:
                section_descr['node'] = None
                section_descr['error_flag'] = 'NoSuchNodeError'
                section_descr['node_html'] = 'for node id ' + ustr(nid)
            sectionlist.append(section_descr)

        # remark: error messages will be served untranslated in English
        # because messages from the python interpreter (in English) will be added

        return req.getTAL("web/admin/modules/metatype.html", {'sectionlist': sectionlist, 'csrf': req.csrf_token.current_token}, macro="view_testnodes")

    if len(path) == 2 and path[1] == "info":
        return showInfo(req)

    if "file" in req.params and hasattr(req.params["file"], "filesize") and req.params["file"].filesize > 0:
        # import scheme from xml-file
        importfile = req.params.get("file")
        if importfile.tempname != "":
            xmlimport(req, importfile.tempname)

    if req.params.get("acttype", "schema") == "schema":
        # section for schema
        for key in req.params.keys():
            # create new metadatatype
            if key.startswith("new"):
                return MetatypeDetail(req, "")

            # edit metadatatype
            elif key.startswith("edit_"):
                return MetatypeDetail(req, key[5:-2])

            # delete metadata
            elif key.startswith("delete_"):
                deleteMetaType(key[7:-2])
                break

            # show details for given metadatatype
            elif key.startswith("detaillist_"):
                return showDetailList(req, key[11:-2])

            # show masklist for given metadatatype
            elif key.startswith("masks_"):
                return showMaskList(req, key[6:-2])

        # save schema
        if "form_op" in req.params.keys():
            if req.params.get("form_op", "") == "cancel":
                return view(req)

            if req.params.get("mname", "") == "" or req.params.get("mlongname", "") == "" or req.params.get("mdatatypes", "") == "":
                return MetatypeDetail(req, req.params.get("mname_orig", ""), 1)  # no name was given
            elif not checkString(req.params.get("mname", "")):
                return MetatypeDetail(req, req.params.get("mname_orig", ""), 4)  # if the name contains wrong characters
            elif req.params.get("mname_orig", "") != req.params.get("mname", "") and existMetaType(req.params.get("mname")):
                return MetatypeDetail(req, req.params.get("mname_orig", ""), 2)  # metadata still existing

            _active = 0
            if req.params.get("mactive", "") != "":
                _active = 1
            updateMetaType(req.params.get("mname", ""),
                           description=req.params.get("description", ""),
                           longname=req.params.get("mlongname", ""), active=_active,
                           datatypes=req.params.get("mdatatypes", "").replace(";", ", "),
                           bibtexmapping=req.params.get("mbibtex", ""),
                           citeprocmapping=req.params.get("mciteproc", ""),
                           orig_name=req.params.get("mname_orig", ""))
            mtype = q(Metadatatype).filter_by(name=req.params.get("mname")).scalar()
            if mtype:
                new_ruleset_names = set(req.form.getlist("leftread"))
                add_remove_rulesets_from_metadatatype(mtype, new_ruleset_names)

            db.session.commit()

    elif req.params.get("acttype") == "field":
        # section for fields
        for key in req.params.keys():
            # create new meta field
            if key.startswith("newdetail_"):
                return FieldDetail(req, req.params.get("parent"), "")

            # edit meta field
            elif key.startswith("editdetail_"):
                return FieldDetail(req, req.params.get("parent"), key[11:-2])

            # delete metafield: key[13:-2] = pid | n
            elif key.startswith("deletedetail_"):
                deleteMetaField(req.params.get("parent"), key[13:-2])
                return showDetailList(req, req.params.get("parent"))

            # change field order up
            if key.startswith("updetail_"):
                moveMetaField(req.params.get("parent"), key[9:-2], -1)
                return showDetailList(req, req.params.get("parent"))

            # change field order down
            elif key.startswith("downdetail_"):
                moveMetaField(req.params.get("parent"), key[11:-2], 1)
                return showDetailList(req, req.params.get("parent"))

        if "form_op" in req.params.keys():
            if req.params.get("form_op", "") == "cancel":
                return showDetailList(req, req.params.get("parent"))

            if existMetaField(req.params.get("parent"), req.params.get("mname")) and \
                    (req.params.get("form_op", "")  == "save_newdetail" or req.params.get("mname") != req.params.get("mname_orig")):
                return FieldDetail(req, req.params.get("parent"), req.params.get("orig_name", ""), 3)  # field still existing
            elif req.params.get("mname", "") == "" or req.params.get("mlabel", "") == "":
                return FieldDetail(req, req.params.get("parent"), req.params.get("orig_name", ""), 1)
            elif not checkString(req.params.get("mname", "")):
                # if the name contains wrong characters
                return FieldDetail(req, req.params.get("parent"), req.params.get("orig_name", ""), 4)

            _option = ""
            for o in req.params.keys():
                if o.startswith("option_"):
                    _option += o[7]

            _fieldvalue = ""
            if req.params.get("mtype", "") + "_value" in req.params.keys():
                _fieldvalue = req.params.get(req.params.get("mtype") + "_value")

            _filenode = None
            if "valuesfile" in req.params.keys():
                valuesfile = req.params.pop("valuesfile")
                _filenode = importFileToRealname(valuesfile.filename, valuesfile.tempname)

            _attr_dict = {}
            if req.params.get("mtype", "") + "_handle_attrs" in req.params.keys():

                attr_names = [s.strip() for s in req.params.get(req.params.get("mtype", "") + "_handle_attrs").split(",")]
                key_prefix = req.params.get("mtype", "") + "_attr_"

                for attr_name in attr_names:
                    attr_value = req.params.get(key_prefix + attr_name, "")
                    _attr_dict[attr_name] = attr_value

            updateMetaField(req.params.get("parent", ""), req.params.get("mname", ""),
                            req.params.get("mlabel", ""), req.params.get("orderpos", ""),
                            req.params.get("mtype", ""), _option, req.params.get("mdescription", ""),
                            _fieldvalue, fieldid=req.params.get("fieldid", ""),
                            filenode=_filenode,
                            attr_dict=_attr_dict)

        return showDetailList(req, req.params.get("parent"))

    elif req.params.get("acttype") == "mask":

        # section for masks
        for key in req.params.keys():

            # new mask
            if key.startswith("newmask_"):
                return MaskDetails(req, req.params.get("parent"), "")

            # edit metatype masks
            elif key.startswith("editmask_"):
                return MaskDetails(req, req.params.get("parent"), key[9:-2], err=0)

            # delete mask
            elif key.startswith("deletemask_"):
                mtype = getMetaType(req.params.get("parent"))
                mtype.children.remove(q(Node).get(key[11:-2]))
                db.session.commit()
                return showMaskList(req, req.params.get("parent"))

            # create autmatic mask with all fields
            elif key.startswith("automask_"):
                generateMask(getMetaType(req.params.get("parent")))
                return showMaskList(req, req.params.get("parent"))

            # cope selected mask
            if key.startswith("copymask_"):
                k = key[9:-2]
                if k.isdigit():
                    mask = q(Mask).get(k)
                else:
                    mtype = getMetaType(req.params.get("parent"))
                    mask = mtype.getMask(k)
                cloneMask(mask, u"copy_" + mask.name)
                return showMaskList(req, req.params.get("parent"))

        if "form_op" in req.params.keys():
            if req.params.get("form_op", "") == "cancel":
                return showMaskList(req, req.params.get("parent"))

            if req.params.get("mname", "") == "":
                return MaskDetails(req, req.params.get("parent", ""), req.params.get("morig_name", ""), err=1)
            elif not checkString(req.params.get("mname", "")):
                # if the name contains wrong characters
                return MaskDetails(req, req.params.get("parent", ""), req.params.get("morig_name", ""), err=4)

            mtype = q(Metadatatype).filter_by(name=q(Node).get(req.params.get("parent", "")).name).one()
            if req.params.get("form_op") == "save_editmask":
                mask = mtype.get_mask(req.params.get("mname", ""))
                # in case of renaming a mask the mask cannot be detected via the new mname
                # then detect mask via maskid
                if not mask:
                    mtype = getMetaType(req.params.get("parent"))
                    mask = mtype.children.filter_by(id =req.params.get("maskid", "")).scalar()

            elif req.params.get("form_op") == "save_newmask":
                mask = Mask(req.params.get("mname", ""))
                mtype.children.append(mask)
                db.session.commit()
            mask.name = req.params.get("mname")
            mask.setDescription(req.params.get("mdescription"))
            mask.setMasktype(req.params.get("mtype"))
            mask.setSeparator(req.params.get("mseparator"))
            db.session.commit()

            if req.params.get("mtype") == "export":
                mask.setExportMapping(req.params.get("exportmapping") or "")
                mask.setExportHeader(req.params.get("exportheader"))
                mask.setExportFooter(req.params.get("exportfooter"))
                _opt = ""
                if "types" in req.params.keys():
                    _opt += "t"
                if "notlast" in req.params.keys():
                    _opt += "l"
                mask.setExportOptions(_opt)
                db.session.commit()

            mask.setLanguage(req.params.get("mlanguage", ""))
            mask.setDefaultMask("mdefault" in req.params.keys())

            for r in mask.access_ruleset_assocs.filter_by(ruletype=u'read'):
                db.session.delete(r)

            for key in req.params.keys():
                if key.startswith("left"):
                    for r in req.params.get(key).split(';'):
                        mask.access_ruleset_assocs.append(NodeToAccessRuleset(ruleset_name=r, ruletype=key[4:]))
                    break
            db.session.commit()
        return showMaskList(req, ustr(req.params.get("parent", "")))
    return view(req)
コード例 #31
0
ファイル: jsonnode.py プロジェクト: mediatum/mediatum
def buildNodeDescriptor(params, node, indent=None, written=None, children=True, parents=False):
    nd = []
    d = {}
    if written is None:
        written = {}

    nodedict = {}
    nodedict['id'] = node.id

    mask = params.get('mask', 'none').lower()

    attrlist = params.get('attrlist', [])
    if attrlist:
        attrlist = attrlist.split(',')

    attrspec = params.get('attrspec', 'default_mask')
    # 'all': no restriction, send all attributes
    # 'none': to not send any attribute at all
    # 'default_mask' (default): only send attributes that correspond to the default mask fields
    #
    # remark: attributes specified comma separated in 'attrlist' are added to those specified by 'attrspec'
    #

    if mask == 'default':
        nodedict['defaultexport'] = node.show_node_text(labels=1, language=params.get('lang', ''))
        # except:
        #    logg.error('Error: web.services.jsonnode: could not get default mask content')
        #    nodedict['defaultexport'] = []

    elif mask not in ["", "none"]:  # deliver every mask
        try:
            mask_obj = getMetaType(node.getSchema()).getMask(mask)
            if mask_obj:
                nodedict['defaultexport'] = mask_obj.getViewHTML([node], flags=8)
            else:
                nodedict['defaultexport'] = "mask not found"
        except Exception as e:
            logg.exception("exception in buildNodeDescriptor")
            nodedict['defaultexport'] = "error"

    if children:
        nodedict['children'] = []
        for c in node.getChildren().sort_by_orderpos():
            nodedict['children'].append({'id': ustr(c.id), 'type': c.type, 'name': esc(c.name)})

    if parents:
        nodedict['parents'] = []
        for c in node.getParents().sort_by_orderpos():
            nodedict['parents'].append({'id': ustr(c.id), 'type': c.type, 'name': esc(c.name)})

    nd.append(nodedict)

    if(children):
        for c in node.getChildren().sort_by_orderpos():
            if c.id not in written:
                written[c.id] = None
                childnodedict = buildNodeDescriptor(params, c, indent)
                nd.append(childnodedict)

    nodeattributes_dict = {}

    if attrspec == 'none':
        # no attributes should be sent
        pass
    elif attrspec == 'default_mask' or attrspec not in ['none', 'all']:
        from contenttypes.data import make_lookup_key, get_maskcache_entry
        from core.transition.globals import request
        language = params.get('lang', '')
        lookup_key = make_lookup_key(node, language=language, labels=False)
        if 'maskcache' not in request.app_cache or lookup_key not in request.app_cache['maskcache']:
            # fill cache
            node.show_node_text(labels=False, language=language)

        mask_id, field_descriptors = get_maskcache_entry(lookup_key, request.app_cache['maskcache'], request.app_cache['maskcache_accesscount'])

        try:
            for field_descriptor in field_descriptors:
                field_attribute = field_descriptor[0]
                if field_attribute not in attrlist:
                    # attrlist may be an empty list or filled from the request parameters
                    attrlist.append(field_attribute)
        except:
            # no mask for this metadata type
            logg.exception("exception in buildNodeDescriptor, no 'nodesmall' or 'shortview' for node %s", node.id)

    elif attrspec == 'all':
        nodeattributes_dict_all_attributes = node.attributes.copy()
        if nodeattributes_dict_all_attributes:
            for k in filter(attribute_name_filter, nodeattributes_dict_all_attributes.keys()):
                nodeattributes_dict[k] = nodeattributes_dict_all_attributes[k]

    if attrlist:
        for attr in filter(attribute_name_filter, attrlist):
            if attr == "nodename":
                value = node.name
            else:
                value = node.get(attr)
            nodeattributes_dict[attr] = value

    if nodeattributes_dict:
        nodedict['attributes'] = nodeattributes_dict

    if 'files' in params:

        nodedict['files'] = []

        for file in node.getFiles():
            if file.type == "metadata" or file.type == "statistic":
                continue
            mimetype = file.mimetype
            if mimetype is None:
                mimetype = "application/x-download"
            nodedict['files'].append({'filename': esc(file.getName()), 'mime-type': mimetype, 'type': file.type})

    if 'nodename' in params:
        nodedict['name'] = node.name

    if 'nodetype' in params:
        nodedict['type'] = node.type

    if 'nodeschema' in params:
        nodedict['schema'] = node.schema
    return nd
コード例 #32
0
ファイル: metatype.py プロジェクト: hibozzy/mediatum
def validate(req, op):
    path = req.path[1:].split("/")

    if len(path) == 3 and path[2] == "overview":
        return showFieldOverview(req)

    if len(path) == 4 and path[3] == "editor":
        res = showEditor(req)
        # mask may have been edited: flush masks cache
        flush_maskcache(req=req)
        return res

    if len(path) == 5 and path[3] == "editor" and path[4] == "show_testnodes":

        template = req.params.get('template', '')
        testnodes_list = req.params.get('testnodes', '')
        width = req.params.get('width', '400')
        item_id = req.params.get('item_id', None)

        mdt_name = path[1]
        mask_name = path[2]

        mdt = tree.getRoot('metadatatypes').getChild(mdt_name)
        mask = mdt.getChild(mask_name)

        access = AccessData(req)

        sectionlist = []
        for nid in [x.strip() for x in testnodes_list.split(',') if x.strip()]:
            section_descr = {}
            section_descr['nid'] = nid
            section_descr['error_flag'] = ''  # in case of no error
            try:
                node = tree.getNode(nid)
                section_descr['node'] = node
                if access.hasAccess(node, "data"):
                    try:
                        node_html = mask.getViewHTML([node], VIEW_DEFAULT, template_from_caller=[template, mdt, mask, item_id], mask=mask)
                        section_descr['node_html'] = node_html
                    except:
                        error_text = str(sys.exc_info()[1])
                        template_line = 'for node id ' + str(nid) + ': ' + error_text
                        try:
                            m = re.match(r".*line (?P<line>\d*), column (?P<column>\d*)", error_text)
                            if m:
                                mdict = m.groupdict()
                                line = int(mdict.get('line', 0))
                                column = int(mdict.get('column', 0))
                                error_text = error_text.replace('line %d' % line, 'template line %d' % (line - 1))
                                template_line = 'for node id ' + str(nid) + '<br/>' + error_text + '<br/><code>' + esc(template.split(
                                    "\n")[line - 2][0:column - 1]) + '<span style="color:red">' + esc(template.split("\n")[line - 2][column - 1:]) + '</span></code>'
                        except:
                            pass
                        section_descr['error_flag'] = 'Error while evaluating template:'
                        section_descr['node_html'] = template_line
                else:
                    section_descr['error_flag'] = 'no access'
                    section_descr['node_html'] = ''
            except tree.NoSuchNodeError:
                section_descr['node'] = None
                section_descr['error_flag'] = 'NoSuchNodeError'
                section_descr['node_html'] = 'for node id ' + str(nid)
            sectionlist.append(section_descr)

        # remark: error messages will be served untranslated in English
        # because messages from the python interpreter (in English) will be added

        return req.getTAL("web/admin/modules/metatype.html", {'sectionlist': sectionlist}, macro="view_testnodes")

    if len(path) == 2 and path[1] == "info":
        return showInfo(req)

    if "file" in req.params and hasattr(req.params["file"], "filesize") and req.params["file"].filesize > 0:
        # import scheme from xml-file
        importfile = req.params.get("file")
        if importfile.tempname != "":
            xmlimport(req, importfile.tempname)

    if req.params.get("acttype", "schema") == "schema":
        # section for schema
        for key in req.params.keys():
            # create new metadatatype
            if key.startswith("new"):
                return MetatypeDetail(req, "")

            # edit metadatatype
            elif key.startswith("edit_"):
                return MetatypeDetail(req, str(key[5:-2]))

            # delete metadata
            elif key.startswith("delete_"):
                deleteMetaType(key[7:-2])
                break

            # show details for given metadatatype
            elif key.startswith("detaillist_"):
                return showDetailList(req, str(key[11:-2]))

            # show masklist for given metadatatype
            elif key.startswith("masks_"):
                return showMaskList(req, str(key[6:-2]))

            # reindex search index for current schema
            elif key.startswith("indexupdate_") and "cancel" not in req.params.keys():
                schema = tree.getNode(key[12:])
                searcher.reindex(schema.getAllItems())
                break

        # save schema
        if "form_op" in req.params.keys():
            if req.params.get("form_op", "") == "cancel":
                return view(req)

            if req.params.get("mname", "") == "" or req.params.get("mlongname", "") == "" or req.params.get("mdatatypes", "") == "":
                return MetatypeDetail(req, req.params.get("mname_orig", ""), 1)  # no name was given
            elif not checkString(req.params.get("mname", "")):
                return MetatypeDetail(req, req.params.get("mname_orig", ""), 4)  # if the name contains wrong characters
            elif req.params.get("mname_orig", "") != req.params.get("mname", "") and existMetaType(req.params.get("mname")):
                return MetatypeDetail(req, req.params.get("mname_orig", ""), 2)  # metadata still existing

            _active = 0
            if req.params.get("mactive", "") != "":
                _active = 1
            updateMetaType(req.params.get("mname", ""),
                           description=req.params.get("description", ""),
                           longname=req.params.get("mlongname", ""), active=_active,
                           datatypes=req.params.get("mdatatypes", "").replace(";", ", "),
                           bibtexmapping=req.params.get("mbibtex", ""),
                           citeprocmapping=req.params.get("mciteproc", ""),
                           orig_name=req.params.get("mname_orig", ""))
            mtype = getMetaType(req.params.get("mname"))
            if mtype:
                mtype.setAccess("read", "")
                for key in req.params.keys():
                    if key.startswith("left"):
                        mtype.setAccess(key[4:], req.params.get(key).replace(";", ","))
                        break

    elif req.params.get("acttype") == "field":
        # section for fields
        for key in req.params.keys():
            # create new meta field
            if key.startswith("newdetail_"):
                return FieldDetail(req, req.params.get("parent"), "")

            # edit meta field
            elif key.startswith("editdetail_"):
                return FieldDetail(req, req.params.get("parent"), key[11:-2])

            # delete metafield: key[13:-2] = pid | n
            elif key.startswith("deletedetail_"):
                deleteMetaField(req.params.get("parent"), key[13:-2])
                return showDetailList(req, req.params.get("parent"))

            # change field order up
            if key.startswith("updetail_"):
                moveMetaField(req.params.get("parent"), key[9:-2], -1)
                return showDetailList(req, req.params.get("parent"))

            # change field order down
            elif key.startswith("downdetail_"):
                moveMetaField(req.params.get("parent"), key[11:-2], 1)
                return showDetailList(req, req.params.get("parent"))

        if "form_op" in req.params.keys():
            if req.params.get("form_op", "") == "cancel":
                return showDetailList(req, req.params.get("parent"))

            if existMetaField(req.params.get("parent"), req.params.get("mname")) and req.params.get("form_op", "") == "save_newdetail":
                return FieldDetail(req, req.params.get("parent"), req.params.get("orig_name", ""), 3)  # field still existing
            elif req.params.get("mname", "") == "" or req.params.get("mlabel", "") == "":
                return FieldDetail(req, req.params.get("parent"), req.params.get("orig_name", ""), 1)
            elif not checkString(req.params.get("mname", "")):
                # if the name contains wrong characters
                return FieldDetail(req, req.params.get("parent"), req.params.get("orig_name", ""), 4)

            _option = ""
            for o in req.params.keys():
                if o.startswith("option_"):
                    _option += o[7]

            _fieldvalue = ""
            if req.params.get("mtype", "") + "_value" in req.params.keys():
                _fieldvalue = str(req.params.get(req.params.get("mtype") + "_value"))

            _filenode = None
            if "valuesfile" in req.params.keys():
                valuesfile = req.params.pop("valuesfile")
                _filenode = importFileToRealname(valuesfile.filename, valuesfile.tempname)

            _attr_dict = {}
            if req.params.get("mtype", "") + "_handle_attrs" in req.params.keys():

                attr_names = [s.strip() for s in req.params.get(req.params.get("mtype", "") + "_handle_attrs").split(",")]
                key_prefix = req.params.get("mtype", "") + "_attr_"

                for attr_name in attr_names:
                    attr_value = req.params.get(key_prefix + attr_name, "")
                    _attr_dict[attr_name] = attr_value

            updateMetaField(req.params.get("parent", ""), req.params.get("mname", ""),
                            req.params.get("mlabel", ""), req.params.get("orderpos", ""),
                            req.params.get("mtype", ""), _option, req.params.get("mdescription", ""),
                            _fieldvalue, fieldid=req.params.get("fieldid", ""),
                            filenode=_filenode,
                            attr_dict=_attr_dict)

        return showDetailList(req, req.params.get("parent"))

    elif req.params.get("acttype") == "mask":

        # mask may have been edited: flush masks cache
        flush_maskcache(req=req)

        # section for masks
        for key in req.params.keys():

            # new mask
            if key.startswith("newmask_"):
                return MaskDetails(req, req.params.get("parent"), "")

            # edit metatype masks
            elif key.startswith("editmask_"):
                return MaskDetails(req, req.params.get("parent"), key[9:-2], err=0)

            # delete mask
            elif key.startswith("deletemask_"):
                mtype = getMetaType(req.params.get("parent"))
                mtype.removeChild(tree.getNode(key[11:-2]))
                return showMaskList(req, req.params.get("parent"))

            # create autmatic mask with all fields
            elif key.startswith("automask_"):
                generateMask(getMetaType(req.params.get("parent")))
                return showMaskList(req, req.params.get("parent"))

            # cope selected mask
            if key.startswith("copymask_"):
                mtype = getMetaType(req.params.get("parent"))
                mask = mtype.getMask(key[9:-2])
                cloneMask(mask, "copy_" + mask.getName())
                return showMaskList(req, req.params.get("parent"))

        if "form_op" in req.params.keys():
            if req.params.get("form_op", "") == "cancel":
                return showMaskList(req, req.params.get("parent"))

            if req.params.get("mname", "") == "":
                return MaskDetails(req, req.params.get("parent", ""), req.params.get("morig_name", ""), err=1)
            elif not checkString(req.params.get("mname", "")):
                # if the name contains wrong characters
                return MaskDetails(req, req.params.get("parent", ""), req.params.get("morig_name", ""), err=4)

            mtype = getMetaType(req.params.get("parent", ""))
            if req.params.get("form_op") == "save_editmask":
                mask = mtype.getMask(req.params.get("maskid", ""))

            elif req.params.get("form_op") == "save_newmask":
                mask = tree.Node(req.params.get("mname", ""), type="mask")
                mtype.addChild(mask)

            mask.setName(req.params.get("mname"))
            mask.setDescription(req.params.get("mdescription"))
            mask.setMasktype(req.params.get("mtype"))
            mask.setSeparator(req.params.get("mseparator"))

            if req.params.get("mtype") == "export":
                mask.setExportMapping(req.params.get("exportmapping") or "")
                mask.setExportHeader(req.params.get("exportheader"))
                mask.setExportFooter(req.params.get("exportfooter"))
                _opt = ""
                if "types" in req.params.keys():
                    _opt += "t"
                if "notlast" in req.params.keys():
                    _opt += "l"
                mask.setExportOptions(_opt)

            mask.setLanguage(req.params.get("mlanguage", ""))
            mask.setDefaultMask("mdefault" in req.params.keys())
            mask.setAccess("read", "")
            for key in req.params.keys():
                if key.startswith("left"):
                    mask.setAccess(key[4:], req.params.get(key).replace(";", ","))
                    break
        return showMaskList(req, str(req.params.get("parent", "")))
    return view(req)
コード例 #33
0
ファイル: union.py プロジェクト: agromsl/mediatum
 def getFormatedValue(self, field, node, language=None, html=1):
     value = node.get(field.getName()).replace(";", "; ")
     if html:
         value = esc(value)
     return (field.getLabel(), value)
コード例 #34
0
ファイル: metatype.py プロジェクト: schnittstabil/mediatum
def validate(req, op):
    path = req.path[1:].split("/")

    if len(path) == 3 and path[2] == "overview":
        return showFieldOverview(req)

    if len(path) == 4 and path[3] == "editor":
        res = showEditor(req)
        return res

    if len(path) == 5 and path[3] == "editor" and path[4] == "show_testnodes":

        raise NotImplementedError("")

        template = req.params.get('template', '')
        testnodes_list = req.params.get('testnodes', '')
        width = req.params.get('width', '400')
        item_id = req.params.get('item_id', None)

        mdt_name = path[1]
        mask_name = path[2]

        mdt = q(Metadatatypes).one().children.filter_by(name=mdt_name).one()
        mask = mdt.children.filter_by(name=mask_name).one()

        sectionlist = []
        for nid in [x.strip() for x in testnodes_list.split(',') if x.strip()]:
            section_descr = {}
            section_descr['nid'] = nid
            section_descr['error_flag'] = ''  # in case of no error

            node = q(Node).get(nid)
            section_descr['node'] = node
            if node and node.has_data_access():
                try:
                    node_html = mask.getViewHTML(
                        [node],
                        VIEW_DEFAULT,
                        template_from_caller=[template, mdt, mask, item_id])
                    section_descr['node_html'] = node_html
                except:
                    logg.exception("exception while evaluating template")
                    error_text = str(sys.exc_info()[1])
                    template_line = 'for node id ' + ustr(
                        nid) + ': ' + error_text
                    with suppress(Exception, warn=False):
                        m = re.match(
                            r".*line (?P<line>\d*), column (?P<column>\d*)",
                            error_text)
                        if m:
                            mdict = m.groupdict()
                            line = int(mdict.get('line', 0))
                            column = int(mdict.get('column', 0))
                            error_text = error_text.replace(
                                'line %d' % line,
                                'template line %d' % (line - 1))
                            template_line = 'for node id ' + ustr(
                                nid
                            ) + '<br/>' + error_text + '<br/><code>' + esc(
                                template.split("\n")[line - 2][0:column - 1]
                            ) + '<span style="color:red">' + esc(
                                template.split("\n")[line -
                                                     2][column -
                                                        1:]) + '</span></code>'
                    section_descr[
                        'error_flag'] = 'Error while evaluating template:'
                    section_descr['node_html'] = template_line
            elif node and not node.has_data_access():
                section_descr['error_flag'] = 'no access'
                section_descr['node_html'] = ''
            if node is None:
                section_descr['node'] = None
                section_descr['error_flag'] = 'NoSuchNodeError'
                section_descr['node_html'] = 'for node id ' + ustr(nid)
            sectionlist.append(section_descr)

        # remark: error messages will be served untranslated in English
        # because messages from the python interpreter (in English) will be added

        return req.getTAL("web/admin/modules/metatype.html", {
            'sectionlist': sectionlist,
            'csrf': req.csrf_token.current_token
        },
                          macro="view_testnodes")

    if len(path) == 2 and path[1] == "info":
        return showInfo(req)

    if "file" in req.params and hasattr(
            req.params["file"],
            "filesize") and req.params["file"].filesize > 0:
        # import scheme from xml-file
        importfile = req.params.get("file")
        if importfile.tempname != "":
            xmlimport(req, importfile.tempname)

    if req.params.get("acttype", "schema") == "schema":
        # section for schema
        for key in req.params.keys():
            # create new metadatatype
            if key.startswith("new"):
                return MetatypeDetail(req, "")

            # edit metadatatype
            elif key.startswith("edit_"):
                return MetatypeDetail(req, key[5:-2])

            # delete metadata
            elif key.startswith("delete_"):
                deleteMetaType(key[7:-2])
                break

            # show details for given metadatatype
            elif key.startswith("detaillist_"):
                return showDetailList(req, key[11:-2])

            # show masklist for given metadatatype
            elif key.startswith("masks_"):
                return showMaskList(req, key[6:-2])

        # save schema
        if "form_op" in req.params.keys():
            if req.params.get("form_op", "") == "cancel":
                return view(req)

            if req.params.get("mname", "") == "" or req.params.get(
                    "mlongname", "") == "" or req.params.get("mdatatypes",
                                                             "") == "":
                return MetatypeDetail(req, req.params.get("mname_orig", ""),
                                      1)  # no name was given
            elif not checkString(req.params.get("mname", "")):
                return MetatypeDetail(
                    req, req.params.get("mname_orig", ""),
                    4)  # if the name contains wrong characters
            elif req.params.get("mname_orig", "") != req.params.get(
                    "mname", "") and existMetaType(req.params.get("mname")):
                return MetatypeDetail(req, req.params.get("mname_orig", ""),
                                      2)  # metadata still existing

            _active = 0
            if req.params.get("mactive", "") != "":
                _active = 1
            updateMetaType(req.params.get("mname", ""),
                           description=req.params.get("description", ""),
                           longname=req.params.get("mlongname", ""),
                           active=_active,
                           datatypes=req.params.get("mdatatypes",
                                                    "").replace(";", ", "),
                           bibtexmapping=req.params.get("mbibtex", ""),
                           citeprocmapping=req.params.get("mciteproc", ""),
                           orig_name=req.params.get("mname_orig", ""))
            mtype = q(Metadatatype).filter_by(
                name=req.params.get("mname")).scalar()
            if mtype:
                new_ruleset_names = set(req.form.getlist("leftread"))
                add_remove_rulesets_from_metadatatype(mtype, new_ruleset_names)

            db.session.commit()

    elif req.params.get("acttype") == "field":
        # section for fields
        for key in req.params.keys():
            # create new meta field
            if key.startswith("newdetail_"):
                return FieldDetail(req, req.params.get("parent"), "")

            # edit meta field
            elif key.startswith("editdetail_"):
                return FieldDetail(req, req.params.get("parent"), key[11:-2])

            # delete metafield: key[13:-2] = pid | n
            elif key.startswith("deletedetail_"):
                deleteMetaField(req.params.get("parent"), key[13:-2])
                return showDetailList(req, req.params.get("parent"))

            # change field order up
            if key.startswith("updetail_"):
                moveMetaField(req.params.get("parent"), key[9:-2], -1)
                return showDetailList(req, req.params.get("parent"))

            # change field order down
            elif key.startswith("downdetail_"):
                moveMetaField(req.params.get("parent"), key[11:-2], 1)
                return showDetailList(req, req.params.get("parent"))

        if "form_op" in req.params.keys():
            if req.params.get("form_op", "") == "cancel":
                return showDetailList(req, req.params.get("parent"))

            if existMetaField(req.params.get("parent"), req.params.get("mname")) and \
                    (req.params.get("form_op", "")  == "save_newdetail" or req.params.get("mname") != req.params.get("mname_orig")):
                return FieldDetail(req, req.params.get("parent"),
                                   req.params.get("orig_name", ""),
                                   3)  # field still existing
            elif req.params.get("mname", "") == "" or req.params.get(
                    "mlabel", "") == "":
                return FieldDetail(req, req.params.get("parent"),
                                   req.params.get("orig_name", ""), 1)
            elif not checkString(req.params.get("mname", "")):
                # if the name contains wrong characters
                return FieldDetail(req, req.params.get("parent"),
                                   req.params.get("orig_name", ""), 4)

            _option = ""
            for o in req.params.keys():
                if o.startswith("option_"):
                    _option += o[7]

            _fieldvalue = ""
            if req.params.get("mtype", "") + "_value" in req.params.keys():
                _fieldvalue = req.params.get(
                    req.params.get("mtype") + "_value")

            _filenode = None
            if "valuesfile" in req.params.keys():
                valuesfile = req.params.pop("valuesfile")
                _filenode = importFileToRealname(valuesfile.filename,
                                                 valuesfile.tempname)

            _attr_dict = {}
            if req.params.get("mtype",
                              "") + "_handle_attrs" in req.params.keys():

                attr_names = [
                    s.strip() for s in req.params.get(
                        req.params.get("mtype", "") +
                        "_handle_attrs").split(",")
                ]
                key_prefix = req.params.get("mtype", "") + "_attr_"

                for attr_name in attr_names:
                    attr_value = req.params.get(key_prefix + attr_name, "")
                    _attr_dict[attr_name] = attr_value

            updateMetaField(req.params.get("parent", ""),
                            req.params.get("mname", ""),
                            req.params.get("mlabel", ""),
                            req.params.get("orderpos", ""),
                            req.params.get("mtype", ""),
                            _option,
                            req.params.get("mdescription", ""),
                            _fieldvalue,
                            fieldid=req.params.get("fieldid", ""),
                            filenode=_filenode,
                            attr_dict=_attr_dict)

        return showDetailList(req, req.params.get("parent"))

    elif req.params.get("acttype") == "mask":

        # section for masks
        for key in req.params.keys():

            # new mask
            if key.startswith("newmask_"):
                return MaskDetails(req, req.params.get("parent"), "")

            # edit metatype masks
            elif key.startswith("editmask_"):
                return MaskDetails(req,
                                   req.params.get("parent"),
                                   key[9:-2],
                                   err=0)

            # delete mask
            elif key.startswith("deletemask_"):
                mtype = getMetaType(req.params.get("parent"))
                mtype.children.remove(q(Node).get(key[11:-2]))
                db.session.commit()
                return showMaskList(req, req.params.get("parent"))

            # create autmatic mask with all fields
            elif key.startswith("automask_"):
                generateMask(getMetaType(req.params.get("parent")))
                return showMaskList(req, req.params.get("parent"))

            # cope selected mask
            if key.startswith("copymask_"):
                k = key[9:-2]
                if k.isdigit():
                    mask = q(Mask).get(k)
                else:
                    mtype = getMetaType(req.params.get("parent"))
                    mask = mtype.getMask(k)
                cloneMask(mask, u"copy_" + mask.name)
                return showMaskList(req, req.params.get("parent"))

        if "form_op" in req.params.keys():
            if req.params.get("form_op", "") == "cancel":
                return showMaskList(req, req.params.get("parent"))

            if req.params.get("mname", "") == "":
                return MaskDetails(req,
                                   req.params.get("parent", ""),
                                   req.params.get("morig_name", ""),
                                   err=1)
            elif not checkString(req.params.get("mname", "")):
                # if the name contains wrong characters
                return MaskDetails(req,
                                   req.params.get("parent", ""),
                                   req.params.get("morig_name", ""),
                                   err=4)

            mtype = q(Metadatatype).filter_by(
                name=q(Node).get(req.params.get("parent", "")).name).one()
            if req.params.get("form_op") == "save_editmask":
                mask = mtype.get_mask(req.params.get("mname", ""))
                # in case of renaming a mask the mask cannot be detected via the new mname
                # then detect mask via maskid
                if not mask:
                    mtype = getMetaType(req.params.get("parent"))
                    mask = mtype.children.filter_by(
                        id=req.params.get("maskid", "")).scalar()

            elif req.params.get("form_op") == "save_newmask":
                mask = Mask(req.params.get("mname", ""))
                mtype.children.append(mask)
                db.session.commit()
            mask.name = req.params.get("mname")
            mask.setDescription(req.params.get("mdescription"))
            mask.setMasktype(req.params.get("mtype"))
            mask.setSeparator(req.params.get("mseparator"))
            db.session.commit()

            if req.params.get("mtype") == "export":
                mask.setExportMapping(req.params.get("exportmapping") or "")
                mask.setExportHeader(req.params.get("exportheader"))
                mask.setExportFooter(req.params.get("exportfooter"))
                _opt = ""
                if "types" in req.params.keys():
                    _opt += "t"
                if "notlast" in req.params.keys():
                    _opt += "l"
                mask.setExportOptions(_opt)
                db.session.commit()

            mask.setLanguage(req.params.get("mlanguage", ""))
            mask.setDefaultMask("mdefault" in req.params.keys())

            for r in mask.access_ruleset_assocs.filter_by(ruletype=u'read'):
                db.session.delete(r)

            for key in req.params.keys():
                if key.startswith("left"):
                    for r in req.params.get(key).split(';'):
                        mask.access_ruleset_assocs.append(
                            NodeToAccessRuleset(ruleset_name=r,
                                                ruletype=key[4:]))
                    break
            db.session.commit()
        return showMaskList(req, ustr(req.params.get("parent", "")))
    return view(req)
コード例 #35
0
ファイル: text.py プロジェクト: agromsl/mediatum
    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)
コード例 #36
0
ファイル: list.py プロジェクト: mediatum/mediatum
    def formatValues(self, context):
        valuelist = []
        items = {}
        try:
            n = context.collection
            if not isinstance(n, Node):
                raise KeyError
            field_name = context.field.getName()
            id_attr_val = count_list_values_for_all_content_children(n.id, field_name)
            items = {pair[0]: pair[1] for pair in id_attr_val}
        except KeyError:
            None

        tempvalues = context.field.getValueList()
        valuesfiles = context.field.files.all()

        if len(valuesfiles):  # a text file with list values was uploaded
            if os.path.isfile(valuesfiles[0].abspath):
                with codecs.open(valuesfiles[0].abspath, 'r', encoding='utf8') as valuesfile:
                    tempvalues = valuesfile.readlines()

        if len(tempvalues):  # Has the user entered any values?
            if tempvalues[0].find('|') > 0:  # there are values in different languages available
                languages = [x.strip() for x in tempvalues[0].split('|')]  # find out the languages
                valuesdict = dict((lang, []) for lang in languages)        # create a dictionary with languages as keys,
                # and list of respective values as dictionary values
                for i in range(len(tempvalues)):
                    if i:  # if i not 0 - the language names itself shouldn't be included to the values
                        tmp = tempvalues[i].split('|')
                        for j in range(len(tmp)):
                            valuesdict[languages[j]].append(tmp[j])

                lang = context.language
                # if there is no default language, the first language-value will be used
                if (not lang) or (lang not in valuesdict.keys()):
                    lang = languages[0]

                tempvalues = valuesdict[lang]

        for val in tempvalues:
            indent = 0
            canbeselected = 0
            while val.startswith("*"):
                val = val[1:]
                indent = indent + 1
            if val.startswith(" "):
                canbeselected = 1
            val = val.strip()
            if not indent:
                canbeselected = 1
            if indent > 0:
                indent = indent - 1
            indentstr = "&nbsp;" * (2 * indent)

            num = 0
            if val in items.keys():
                num = int(items[val])

            try:
                if int(num) < 0:
                    raise ""
                elif int(num) == 0:
                    num = ""
                else:
                    num = " (" + ustr(num) + ")"
            except:
                logg.exception("exception in getMaskEditorHTML, using empty string")
                num = ""

            val = esc(val)
            if not canbeselected:
                valuelist.append(("optgroup", "<optgroup label=\"" + indentstr + val + "\">", "", ""))
            elif (val in context.value.split(";")):
                valuelist.append(("optionselected", indentstr, val, num))
            else:
                valuelist.append(("option", indentstr, val, num))

        return valuelist
コード例 #37
0
ファイル: jsonnode.py プロジェクト: agromsl/mediatum
def buildNodeDescriptor(req, node, indent=None, written=None, children=True, children_access=None, parents=False):
    nd = []
    d = {}
    if written is None:
        written = {}

    nodedict = {}
    nodedict['id'] = node.id

    mask = req.params.get('mask', 'none').lower()

    attrlist = req.params.get('attrlist', [])
    if attrlist:
        attrlist = attrlist.split(',')

    attrspec = req.params.get('attrspec', 'default_mask')
    # 'all': no restriction, send all attributes
    # 'none': to not send any attribute at all
    # 'default_mask' (default): only send attributes that correspond to the default mask fields
    #
    # remark: attributes specified comma separated in 'attrlist' are added to those specified by 'attrspec'
    #

    if mask == 'default':
        maskcachetype = req.params.get('maskcache', 'deep')  # 'deep', 'shallow', 'none'
        nodedict['defaultexport'] = node.show_node_text(labels=1, language=req.params.get('lang', ''), cachetype=maskcachetype)
        # except:
        #    logging.getLogger('services').error('Error: web.services.jsonnode: could not get default mask content')
        #    nodedict['defaultexport'] = []

    elif mask not in ["", "none"]:  # deliver every mask
        try:
            mask_obj = getMetaType(node.getSchema()).getMask(mask)
            if mask_obj:
                nodedict['defaultexport'] = mask_obj.getViewHTML([node], flags=8)
            else:
                nodedict['defaultexport'] = "mask not found"
        except:
            nodedict['defaultexport'] = "error"

    if children:
        nodedict['children'] = []
        for c in node.getChildren().sort_by_orderpos():
            if (not children_access) or (children_access and children_access.hasAccess(c, 'read')):
                nodedict['children'].append({'id': str(c.id), 'type': c.type, 'name': esc(c.name)})

    if parents:
        nodedict['parents'] = []
        for c in node.getParents().sort_by_orderpos():
            if (not children_access) or (children_access and children_access.hasAccess(c, 'read')):
                nodedict['parents'].append({'id': str(c.id), 'type': c.type, 'name': esc(c.name)})

    nd.append(nodedict)

    if(children):
        for c in node.getChildren().sort_by_orderpos():
            if (not children_access) or (children_access and children_access.hasAccess(c, 'read')):
                if c.id not in written:
                    written[c.id] = None
                    childnodedict = buildNodeDescriptor(req, c, indent, children_access=children_access)
                    nd.append(childnodedict)

    # if node.read_access:
    #    nodedict['read'] = esc(node.read_access)
    # if node.write_access:
    #    nodedict['write'] = esc(node.write_access)
    # if node.data_access:
    #    nodedict['data'] = esc(node.data_access)

    nodeattributes_dict = {}

    if attrspec == 'none':
        # no attributes should be sent
        pass
    elif attrspec == 'default_mask' or attrspec not in ['none', 'all']:
        from contenttypes.default import make_lookup_key, get_maskcache_entry, maskcache
        language = req.params.get('lang', '')
        lookup_key = make_lookup_key(node, language=language, labels=False)
        if lookup_key not in maskcache:
            # fill cache
            node.show_node_text(labels=False, language=language, cachetype='deep')

        field_descriptors = get_maskcache_entry(lookup_key)

        try:
            mask = field_descriptors[0]
            for field_descriptor in field_descriptors[1:]:
                field_attribute = field_descriptor[0]
                #fd = field_descriptor[1]
                if field_attribute not in attrlist:
                    # attrlist may be an empty list or filled from the request parameters
                    attrlist.append(field_attribute)
        except:
            # no mask for this metadata type
            msg = "no 'nodesmall' or 'shortview' for node %s" % str(node.id)
            logging.getLogger("services").warning(msg)

    elif attrspec == 'all':
        nodeattributes_dict_all_attributes = node.attributes.copy()
        if nodeattributes_dict_all_attributes:
            for k in filter(attribute_name_filter, nodeattributes_dict_all_attributes.keys()):
                nodeattributes_dict[k] = u2((nodeattributes_dict_all_attributes[k]))

    if attrlist:
        for attr in filter(attribute_name_filter, attrlist):
            nodeattributes_dict[attr] = u2(node.get(attr))

    if nodeattributes_dict:
        nodedict['attributes'] = nodeattributes_dict

    if 'files' in req.params:

        nodedict['files'] = []

        for file in node.getFiles():
            if file.type == "metadata" or file.type == "statistic":
                continue
            mimetype = file.mimetype
            if mimetype is None:
                mimetype = "application/x-download"
            nodedict['files'].append({'filename': esc(file.getName()), 'mime-type': mimetype, 'type': file.type})

    if 'nodename' in req.params:
        nodedict['name'] = node.name

    if 'nodetype' in req.params:
        if node.type is None:
            nodedict['type'] = 'node'
        else:
            nodedict['type'] = node.type
    return nd
コード例 #38
0
ファイル: xmlnode.py プロジェクト: hibozzy/mediatum
def writexml(node, fi, indent=None, written=None, children=True, children_access=None,
             exclude_filetypes=[], exclude_children_types=[], attribute_name_filter=None):
    if written is None:
        written = {}
    if indent is None:
        indent = 0
    # there are a lot of nodes without name ...
    nodename_copy = node.name
    if nodename_copy is None:
        nodename_copy = ""
    #fi.write('%s<node name="%s" id="%s" ' % ((" " * indent), esc(nodename_copy), str(node.id)))
    # non-utf8 encoded umlauts etc. may cause invalid xml
    fi.write('%s<node name="%s" id="%s" ' % ((" " * indent), u2(esc(nodename_copy)), str(node.id)))
    if node.type is None:
        node.type = "node"
    fi.write('type="%s" ' % node.type)
    if node.read_access:
        fi.write('read="%s" ' % esc(node.read_access))
    if node.write_access:
        fi.write('write="%s" ' % esc(node.write_access))
    if node.data_access:
        fi.write('data="%s" ' % esc(node.data_access))
    fi.write(">\n")

    indent += 4

    for name, value in node.items():
        u_esc_name = u(esc(name))
        if attribute_name_filter and not attribute_name_filter(u_esc_name):
            continue
        fi.write('%s<attribute name="%s"><![CDATA[%s]]></attribute>\n' % ((" " * indent), u_esc_name, u2(value)))

    for file in node.getFiles():
        if file.type == "metadata" or file.type in exclude_filetypes:
            continue
        mimetype = file.mimetype
        if mimetype is None:
            mimetype = "application/x-download"
        fi.write('%s<file filename="%s" mime-type="%s" type="%s"/>\n' %
                 ((" " * indent), esc(file.getName()), mimetype, (file.type is not None and file.type or "image")))
    if children:
        for c in node.getChildren().sort_by_orderpos():
            if (not children_access) or (children_access and children_access.hasAccess(c, 'read')):
                if c.type not in exclude_children_types:
                    fi.write('%s<child id="%s" type="%s"/>\n' % ((" " * indent), str(c.id), c.type))

    indent -= 4
    fi.write("%s</node>\n" % (" " * indent))
    if(children):
        for c in node.getChildren().sort_by_orderpos():
            if (not children_access) or (children_access and children_access.hasAccess(c, 'read')):
                if c.type not in exclude_children_types:
                    if c.id not in written:
                        written[c.id] = None
                        c.writexml(fi, indent=indent,
                                   written=written,
                                   children=children,
                                   children_access=children_access,
                                   exclude_filetypes=exclude_filetypes,
                                   exclude_children_types=exclude_children_types,
                                   attribute_name_filter=attribute_name_filter
                                   )

    if node.type in ["mask"]:
        try:
            exportmapping_id = node.get("exportmapping").strip()
            if exportmapping_id and exportmapping_id not in written:
                try:
                    exportmapping = tree.getNode(exportmapping_id)
                    written[exportmapping_id] = None
                    exportmapping.writexml(fi, indent=indent,
                                           written=written,
                                           children=children,
                                           children_access=children_access,
                                           exclude_filetypes=exclude_filetypes,
                                           exclude_children_types=exclude_children_types,
                                           attribute_name_filter=attribute_name_filter
                                           )
                except:
                    msg = "ERROR: node xml export error node.id='%s', node.name='%s', node.type='%s', exportmapping:'%s'" % (
                        str(node.id), node.name, node.type, str(exportmapping_id))
                    logging.getLogger("backend").error(msg)
            else:
                pass
        except:
            msg = "ERROR: node xml export error node.id='%s', node.name='%s', node.type='%s', exportmapping:'%s'" % (
                str(node.id), node.name, node.type, str(exportmapping_id))
            logging.getLogger("backend").error(msg)
コード例 #39
0
    def formatValues(self, context):
        valuelist = []

        items = {}
        try:
            n = context.collection
            if n is not None:
                field_name = context.field.getName()
                id_attr_val = n.all_children_by_query(
                    q(Node.id, Node.a[field_name]).filter(
                        Node.a[field_name] != None
                        and Node.a[field_name] != '').distinct(
                            Node.a[field_name]))
                items = {pair[0]: pair[1] for pair in id_attr_val}
        except:
            None

        tempvalues = context.field.getValueList()
        valuesfiles = context.field.getFiles()

        if len(valuesfiles) > 0:  # a text file with list values was uploaded
            if os.path.isfile(valuesfiles[0].retrieveFile()):
                with codecs.open(valuesfiles[0].retrieveFile(),
                                 'r',
                                 encoding='utf8') as valuesfile:
                    tempvalues = valuesfile.readlines()

        if tempvalues[0].find(
                '|') > 0:  # there are values in different languages available
            languages = [x.strip() for x in tempvalues[0].split('|')
                         ]  # find out the languages
            valuesdict = dict(
                (lang, []) for lang in
                languages)  # create a dictionary with languages as keys,
            # and list of respective values as dictionary values
            for i in range(len(tempvalues)):
                if i:  # if i not 0 - the language names itself shouldn't be included to the values
                    tmp = tempvalues[i].split('|')
                    for j in range(len(tmp)):
                        valuesdict[languages[j]].append(tmp[j])

            if not context.language:  # if there is no default language, the first language-value will be used
                context.language = languages[0]

            tempvalues = valuesdict[context.language]

        for val in tempvalues:
            indent = 0
            canbeselected = 0
            while val.startswith("*"):
                val = val[1:]
                indent = indent + 1
            if val.startswith(" "):
                canbeselected = 1
            val = val.strip()
            if not indent:
                canbeselected = 1
            if indent > 0:
                indent = indent - 1
            indentstr = "&nbsp;" * (2 * indent)

            if val in items.keys():
                num = int(items[val])

            try:
                if int(num) < 0:
                    raise u""
                elif int(num) == 0:
                    num = u""
                else:
                    num = u" (" + unicode(num) + u")"
            except:
                num = u""

            val = esc(val)

            if not canbeselected:
                valuelist.append(
                    ("optgroup",
                     "<optgroup label=\"" + indentstr + val + "\">", "", ""))
            elif (val in context.value.split(";")):
                valuelist.append(("optionselected", indentstr, val, num))
            else:
                valuelist.append(("option", indentstr, val, num))

        return valuelist
コード例 #40
0
ファイル: xmlnode.py プロジェクト: hibozzy/mediatum
def writexml(
    node,
    fi,
    indent=None,
    written=None,
    children=True,
    children_access=None,
    exclude_filetypes=[],
    exclude_children_types=[],
    attribute_name_filter=None,
):
    if written is None:
        written = {}
    if indent is None:
        indent = 0
    # there are a lot of nodes without name ...
    nodename_copy = node.name
    if nodename_copy is None:
        nodename_copy = ""
    # fi.write('%s<node name="%s" id="%s" ' % ((" " * indent), esc(nodename_copy), str(node.id)))
    # non-utf8 encoded umlauts etc. may cause invalid xml
    fi.write('%s<node name="%s" id="%s" ' % ((" " * indent), u2(esc(nodename_copy)), str(node.id)))
    if node.type is None:
        node.type = "node"
    fi.write('type="%s" ' % node.type)
    if node.read_access:
        fi.write('read="%s" ' % esc(node.read_access))
    if node.write_access:
        fi.write('write="%s" ' % esc(node.write_access))
    if node.data_access:
        fi.write('data="%s" ' % esc(node.data_access))
    fi.write(">\n")

    indent += 4

    for name, value in node.items():
        u_esc_name = u(esc(name))
        if attribute_name_filter and not attribute_name_filter(u_esc_name):
            continue
        fi.write('%s<attribute name="%s"><![CDATA[%s]]></attribute>\n' % ((" " * indent), u_esc_name, u2(value)))

    for file in node.getFiles():
        if file.type == "metadata" or file.type in exclude_filetypes:
            continue
        mimetype = file.mimetype
        if mimetype is None:
            mimetype = "application/x-download"
        fi.write(
            '%s<file filename="%s" mime-type="%s" type="%s"/>\n'
            % ((" " * indent), esc(file.getName()), mimetype, (file.type is not None and file.type or "image"))
        )
    if children:
        for c in node.getChildren().sort_by_orderpos():
            if (not children_access) or (children_access and children_access.hasAccess(c, "read")):
                if c.type not in exclude_children_types:
                    fi.write('%s<child id="%s" type="%s"/>\n' % ((" " * indent), str(c.id), c.type))

    indent -= 4
    fi.write("%s</node>\n" % (" " * indent))
    if children:
        for c in node.getChildren().sort_by_orderpos():
            if (not children_access) or (children_access and children_access.hasAccess(c, "read")):
                if c.type not in exclude_children_types:
                    if c.id not in written:
                        written[c.id] = None
                        c.writexml(
                            fi,
                            indent=indent,
                            written=written,
                            children=children,
                            children_access=children_access,
                            exclude_filetypes=exclude_filetypes,
                            exclude_children_types=exclude_children_types,
                            attribute_name_filter=attribute_name_filter,
                        )

    if node.type in ["mask"]:
        try:
            exportmapping_id = node.get("exportmapping").strip()
            if exportmapping_id and exportmapping_id not in written:
                try:
                    exportmapping = tree.getNode(exportmapping_id)
                    written[exportmapping_id] = None
                    exportmapping.writexml(
                        fi,
                        indent=indent,
                        written=written,
                        children=children,
                        children_access=children_access,
                        exclude_filetypes=exclude_filetypes,
                        exclude_children_types=exclude_children_types,
                        attribute_name_filter=attribute_name_filter,
                    )
                except:
                    msg = (
                        "ERROR: node xml export error node.id='%s', node.name='%s', node.type='%s', exportmapping:'%s'"
                        % (str(node.id), node.name, node.type, str(exportmapping_id))
                    )
                    logging.getLogger("backend").error(msg)
            else:
                pass
        except:
            msg = "ERROR: node xml export error node.id='%s', node.name='%s', node.type='%s', exportmapping:'%s'" % (
                str(node.id),
                node.name,
                node.type,
                str(exportmapping_id),
            )
            logging.getLogger("backend").error(msg)
コード例 #41
0
def prss(s):
    '''protect rss item elements'''
    return esc(no_html(esc(no_html(esc(no_html(esc(u(s))))))))
コード例 #42
0
ファイル: indexer.py プロジェクト: agromsl/mediatum
    def nodeToFulltextSearch(self, node):
        # build fulltext index from node

        if not node.getContentType() in ("document", "dissertation"):
            # only build fulltext of document nodes
            # print "object is no document"
            return True
        r = re.compile("[a-zA-Z0-9]+")

        for file in node.getFiles():
            w = ''
            if file.getType() == "fulltext" and os.path.exists(file.retrieveFile()):
                data = {}
                content = ''
                f = open(file.retrieveFile())
                try:
                    for line in f:
                        if FULLTEXT_INDEX_MODE == 0:
                            content += u(line)
                        else:
                            for w in re.findall(r, line):
                                if w not in data.keys():
                                    data[w] = 1
                            try:
                                data[w] += 1
                            except KeyError:
                                data[w] = 1
                finally:
                    f.close()

                if FULLTEXT_INDEX_MODE == 1:
                    for key in data.keys():
                        content += key + " "
                elif FULLTEXT_INDEX_MODE == 2:
                    for key in data.keys():
                        content += key + " [" + str(data[key]) + "] "
                sql = ""
                if len(content) > 0:
                    try:
                        sql = 'INSERT INTO textsearchmeta (id, type, schema, value) VALUES("{}", "{}", "{}", "{}")'.format(node.id,
                                                                                                                           node.getContentType(),
                                                                                                                           node.getSchema(),
                                                                                                                           iso2utf8(esc(content)))
                        self.db.execute(sql)
                    except:
                        print "error", node.id, "\n"
                        return False
                else:
                    print "no Content"
        return True
コード例 #43
0
ファイル: treeselect.py プロジェクト: agromsl/mediatum
 def getFormatedValue_(self, field, node, language=None, html=1, template_from_caller=None, mask=None):
     value = node.get(field.getName())
     if html:
         value = esc(value)
     return (field.getLabel(), value)
コード例 #44
0
ファイル: label.py プロジェクト: mediatum/mediatum
 def getFormattedValue(self, metafield, maskitem, mask, node, language, html=True):
     value = node.get(metafield.getName())
     if html:
         value = esc(value)
     return (metafield.getLabel(), value)
コード例 #45
0
def struct2rss(req,
               path,
               params,
               data,
               struct,
               debug=False,
               singlenode=False,
               send_children=False):
    nodelist = struct['nodelist']
    language = params.get('lang', 'en')
    items_list = []

    host = u"http://" + unicode(_get_header(req, "HOST") or configured_host)
    collections = get_collections_node()
    user = get_guest_user()

    for n in nodelist:
        nodename = n.name
        nodeid = str(n.id)
        updatetime = utime = try_node_date(n)

        # categories to be included in all items - mask generated or not
        default_categories = u'<category>node type: ' + n.type + '/' + n.schema + u'</category>\r\n'

        # check for export mask for this node
        try:
            try:
                mdt = n.metadatatype
            except:
                mdt = None
            mask = mdt.getMask('rss')
            if mask.get('masktype') != 'export':
                mask = None
        except:
            mask = None

        if mask:
            item_xml = u'<item>\r\n' + mask.getViewHTML(
                [n], flags=8) + default_categories + u'\r\n</item>\r\n'
            items_list = items_list + [(updatetime, nodename, nodeid, item_xml)
                                       ]
            continue

        # no rss export mask: build default item from nodesmall mask
        item_d = {}

        browsingPathList = getBrowsingPathList(n)
        browsingPathList = [
            x for x in browsingPathList if x[-1].has_read_access(
                user=user) and x[-1].is_descendant_of(collections)
        ]
        browsingPathList_names = [
            map(lambda x: x.name, browsingPath)
            for browsingPath in browsingPathList
        ]

        # assumption: longest path is most detailled and illustrative for being used in the title
        x = sorted([[len(p), i, p]
                    for i, p in enumerate(browsingPathList_names)])
        x.reverse()
        try:
            most_detailed_path = x[0][2]
        except:  # browsing path list may be empty (for directories, collections, ...)
            most_detailed_path = ''

        item_d['title'] = esc(u"{} ({}, {}/{}) {}".format(
            nodename or u'-unnamed-node-', nodeid, n.type, n.schema,
            u"/".join(most_detailed_path)))
        item_d['item_pubDate'] = utime
        item_d['guid'] = host + u'/node?id=%s' % nodeid
        item_d['link'] = host + u'/node?id=%s' % nodeid

        if mdt:
            lang_mask = mdt.masks.filter(
                Node.name.startswith(u"nodesmall")).filter(
                    Node.a.language == language).first()
            if lang_mask is not None:
                mask = lang_mask
            else:
                mask = mdt.get_mask('nodesmall')
        else:
            mask = None

        if mask is not None:
            attr_list = mask.getViewHTML(
                [n], VIEW_DATA_ONLY,
                language)  # [[attr_name, value, label, type], ...]
        else:
            attr_list = [
                ['', n.id, 'node id', ''],
                ['', n.name, 'node name', ''],
                ['', n.type + "/" + n.schema, 'node type', ''],
            ]

        description = u''
        for x in attr_list:
            description = description + (u'''<b>%s: </b>%s<br/>\r\n''' %
                                         (x[2], x[1]))

        item_d['description'] = description
        categories = default_categories

        for x in browsingPathList_names:
            categories = categories + u'<category>' + esc(
                u'/'.join(x)) + u'</category>\r\n'

        ddcs = n.get('ddc').strip()
        if ddcs.strip():
            ddcs = ddcs.split(';')
            for ddc in ddcs:
                categories = categories + u'<category>' + esc(
                    ddc) + u'</category>\r\n'

        subjects = n.get('subject').strip()
        if subjects:
            subjects = subjects.split(';')
            for subject in subjects:
                categories = categories + u'<category>' + esc(
                    subject) + u'</category>\r\n'

        item_d['categories'] = categories

        for k, v in item_d.items():
            item_d[k] = v

        items_list = items_list + [(updatetime, nodename, nodeid,
                                    (template_rss_item % item_d))]

    if items_list:
        items_list.sort()
        items_list.reverse()

    items = ''
    for x in items_list:
        items += (x[3] + u'\r\n')

    pubDate = lastBuildDate = format_date(format='rfc822')

    struct['dataready'] = (u"%.3f" %
                           (time.time() - struct['build_response_start']))

    fcd = feed_channel_dict.copy()
    fcd['lang'] = u'de'
    fcd['pubdate'] = pubDate
    fcd['lastbuild'] = lastBuildDate
    fcd['link'] = host
    fcd['atom_link'] = host + req.fullpath
    fcd['image_title'] = 'testlogo'
    fcd['image_link'] = host + u'/img/testlogo.png'
    fcd['image_url'] = host + u'/img/testlogo.png'

    if 'feed_info' in params:
        for k, v in params['feed_info'].items():
            fcd[k] = v
    else:
        fcd['title'] = host + req.fullpath + req.query
    fcd['items'] = items
    s = template_rss_channel % fcd  # params['feed_info']

    return s.encode("utf8")
コード例 #46
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)
コード例 #47
0
ファイル: mlist.py プロジェクト: hibozzy/mediatum
    def formatValues(self, context):
        valuelist = []

        items = {}
        try:
            n = context.collection
            if n is None:
                raise tree.NoSuchNodeError()
            items = n.getAllAttributeValues(context.field.getName(),
                                            context.access)
        except tree.NoSuchNodeError:
            None

        tempvalues = context.field.getValueList()
        valuesfiles = context.field.getFiles()

        if len(valuesfiles) > 0:  # a text file with list values was uploaded
            if os.path.isfile(valuesfiles[0].retrieveFile()):
                valuesfile = open(valuesfiles[0].retrieveFile(), 'r')
                tempvalues = valuesfile.readlines()
                valuesfile.close()

        if tempvalues[0].find(
                '|') > 0:  # there are values in different languages available
            languages = [x.strip() for x in tempvalues[0].split('|')
                         ]  # find out the languages
            valuesdict = dict(
                (lang, []) for lang in
                languages)  # create a dictionary with languages as keys,
            # and list of respective values as dictionary values
            for i in range(len(tempvalues)):
                if i:  # if i not 0 - the language names itself shouldn't be included to the values
                    tmp = tempvalues[i].split('|')
                    for j in range(len(tmp)):
                        valuesdict[languages[j]].append(tmp[j])

            if not context.language:  # if there is no default language, the first language-value will be used
                context.language = languages[0]

            tempvalues = valuesdict[context.language]

        for val in tempvalues:
            indent = 0
            canbeselected = 0
            while val.startswith("*"):
                val = val[1:]
                indent = indent + 1
            if val.startswith(" "):
                canbeselected = 1
            val = val.strip()
            if not indent:
                canbeselected = 1
            if indent > 0:
                indent = indent - 1
            indentstr = "&nbsp;" * (2 * indent)

            if val in items.keys():
                num = int(items[val])

            try:
                if int(num) < 0:
                    raise ""
                elif int(num) == 0:
                    num = ""
                else:
                    num = " (" + str(num) + ")"
            except:
                num = ""

            val = esc(val)

            if not canbeselected:
                valuelist.append(
                    ("optgroup",
                     "<optgroup label=\"" + indentstr + val + "\">", "", ""))
            elif (val in context.value.split(";")):
                valuelist.append(("optionselected", indentstr, val, num))
            else:
                valuelist.append(("option", indentstr, val, num))

        return valuelist
コード例 #48
0
ファイル: jsonnode.py プロジェクト: hibozzy/mediatum
def buildNodeDescriptor(req,
                        node,
                        indent=None,
                        written=None,
                        children=True,
                        children_access=None,
                        parents=False):
    nd = []
    d = {}
    if written is None:
        written = {}

    nodedict = {}
    nodedict['id'] = node.id

    mask = req.params.get('mask', 'none').lower()

    attrlist = req.params.get('attrlist', [])
    if attrlist:
        attrlist = attrlist.split(',')

    attrspec = req.params.get('attrspec', 'default_mask')
    # 'all': no restriction, send all attributes
    # 'none': to not send any attribute at all
    # 'default_mask' (default): only send attributes that correspond to the default mask fields
    #
    # remark: attributes specified comma separated in 'attrlist' are added to those specified by 'attrspec'
    #

    if mask == 'default':
        maskcachetype = req.params.get('maskcache',
                                       'deep')  # 'deep', 'shallow', 'none'
        nodedict['defaultexport'] = node.show_node_text(
            labels=1,
            language=req.params.get('lang', ''),
            cachetype=maskcachetype)
        # except:
        #    logging.getLogger('services').error('Error: web.services.jsonnode: could not get default mask content')
        #    nodedict['defaultexport'] = []

    elif mask not in ["", "none"]:  # deliver every mask
        try:
            mask_obj = getMetaType(node.getSchema()).getMask(mask)
            if mask_obj:
                nodedict['defaultexport'] = mask_obj.getViewHTML([node],
                                                                 flags=8)
            else:
                nodedict['defaultexport'] = "mask not found"
        except:
            nodedict['defaultexport'] = "error"

    if children:
        nodedict['children'] = []
        for c in node.getChildren().sort_by_orderpos():
            if (not children_access) or (children_access and
                                         children_access.hasAccess(c, 'read')):
                nodedict['children'].append({
                    'id': str(c.id),
                    'type': c.type,
                    'name': esc(c.name)
                })

    if parents:
        nodedict['parents'] = []
        for c in node.getParents().sort_by_orderpos():
            if (not children_access) or (children_access and
                                         children_access.hasAccess(c, 'read')):
                nodedict['parents'].append({
                    'id': str(c.id),
                    'type': c.type,
                    'name': esc(c.name)
                })

    nd.append(nodedict)

    if (children):
        for c in node.getChildren().sort_by_orderpos():
            if (not children_access) or (children_access and
                                         children_access.hasAccess(c, 'read')):
                if c.id not in written:
                    written[c.id] = None
                    childnodedict = buildNodeDescriptor(
                        req, c, indent, children_access=children_access)
                    nd.append(childnodedict)

    # if node.read_access:
    #    nodedict['read'] = esc(node.read_access)
    # if node.write_access:
    #    nodedict['write'] = esc(node.write_access)
    # if node.data_access:
    #    nodedict['data'] = esc(node.data_access)

    nodeattributes_dict = {}

    if attrspec == 'none':
        # no attributes should be sent
        pass
    elif attrspec == 'default_mask' or attrspec not in ['none', 'all']:
        from contenttypes.default import make_lookup_key, get_maskcache_entry, maskcache
        language = req.params.get('lang', '')
        lookup_key = make_lookup_key(node, language=language, labels=False)
        if lookup_key not in maskcache:
            # fill cache
            node.show_node_text(labels=False,
                                language=language,
                                cachetype='deep')

        field_descriptors = get_maskcache_entry(lookup_key)

        try:
            mask = field_descriptors[0]
            for field_descriptor in field_descriptors[1:]:
                field_attribute = field_descriptor[0]
                #fd = field_descriptor[1]
                if field_attribute not in attrlist:
                    # attrlist may be an empty list or filled from the request parameters
                    attrlist.append(field_attribute)
        except:
            # no mask for this metadata type
            msg = "no 'nodesmall' or 'shortview' for node %s" % str(node.id)
            logging.getLogger("services").warning(msg)

    elif attrspec == 'all':
        nodeattributes_dict_all_attributes = node.attributes.copy()
        if nodeattributes_dict_all_attributes:
            for k in filter(attribute_name_filter,
                            nodeattributes_dict_all_attributes.keys()):
                nodeattributes_dict[k] = u2(
                    (nodeattributes_dict_all_attributes[k]))

    if attrlist:
        for attr in filter(attribute_name_filter, attrlist):
            nodeattributes_dict[attr] = u2(node.get(attr))

    if nodeattributes_dict:
        nodedict['attributes'] = nodeattributes_dict

    if 'files' in req.params:

        nodedict['files'] = []

        for file in node.getFiles():
            if file.type == "metadata" or file.type == "statistic":
                continue
            mimetype = file.mimetype
            if mimetype is None:
                mimetype = "application/x-download"
            nodedict['files'].append({
                'filename': esc(file.getName()),
                'mime-type': mimetype,
                'type': file.type
            })

    if 'nodename' in req.params:
        nodedict['name'] = node.name

    if 'nodetype' in req.params:
        if node.type is None:
            nodedict['type'] = 'node'
        else:
            nodedict['type'] = node.type
    return nd
コード例 #49
0
ファイル: list.py プロジェクト: hibozzy/mediatum
    def formatValues(self, context):
        valuelist = []
        items = {}
        try:
            n = context.collection
            if n is None:
                raise tree.NoSuchNodeError()
            items = n.getAllAttributeValues(context.field.getName(), context.access)
        except tree.NoSuchNodeError:
            None

        tempvalues = context.field.getValueList()
        valuesfiles = context.field.getFiles()

        if len(valuesfiles):  # a text file with list values was uploaded
            if os.path.isfile(valuesfiles[0].retrieveFile()):
                valuesfile = open(valuesfiles[0].retrieveFile(), "r")
                tempvalues = valuesfile.readlines()
                valuesfile.close()

        if len(tempvalues):  # Has the user entered any values?
            if tempvalues[0].find("|") > 0:  # there are values in different languages available
                languages = [x.strip() for x in tempvalues[0].split("|")]  # find out the languages
                valuesdict = dict((lang, []) for lang in languages)  # create a dictionary with languages as keys,
                # and list of respective values as dictionary values
                for i in range(len(tempvalues)):
                    if i:  # if i not 0 - the language names itself shouldn't be included to the values
                        tmp = tempvalues[i].split("|")
                        for j in range(len(tmp)):
                            valuesdict[languages[j]].append(tmp[j])

                lang = context.language
                # if there is no default language, the first language-value will be used
                if (not lang) or (lang not in valuesdict.keys()):
                    lang = languages[0]

                tempvalues = valuesdict[lang]

        for val in tempvalues:
            indent = 0
            canbeselected = 0
            while val.startswith("*"):
                val = val[1:]
                indent = indent + 1
            if val.startswith(" "):
                canbeselected = 1
            val = val.strip()
            if not indent:
                canbeselected = 1
            if indent > 0:
                indent = indent - 1
            indentstr = "&nbsp;" * (2 * indent)

            num = 0
            if val in items.keys():
                num = int(items[val])

            try:
                if int(num) < 0:
                    raise ""
                elif int(num) == 0:
                    num = ""
                else:
                    num = " (" + str(num) + ")"
            except:
                num = ""

            val = esc(val)
            if not canbeselected:
                valuelist.append(("optgroup", '<optgroup label="' + indentstr + val + '">', "", ""))
            elif val in context.value.split(";"):
                valuelist.append(("optionselected", indentstr, val, num))
            else:
                valuelist.append(("option", indentstr, val, num))

        return valuelist
コード例 #50
0
def buildNodeDescriptor(params,
                        node,
                        indent=None,
                        written=None,
                        children=True,
                        parents=False):
    nd = []
    d = {}
    if written is None:
        written = {}

    nodedict = {}
    nodedict['id'] = node.id

    mask = params.get('mask', 'none').lower()

    attrlist = params.get('attrlist', [])
    if attrlist:
        attrlist = attrlist.split(',')

    attrspec = params.get('attrspec', 'default_mask')
    # 'all': no restriction, send all attributes
    # 'none': to not send any attribute at all
    # 'default_mask' (default): only send attributes that correspond to the default mask fields
    #
    # remark: attributes specified comma separated in 'attrlist' are added to those specified by 'attrspec'
    #

    if mask == 'default':
        nodedict['defaultexport'] = node.show_node_text(labels=1,
                                                        language=params.get(
                                                            'lang', ''))
        # except:
        #    logg.error('Error: web.services.jsonnode: could not get default mask content')
        #    nodedict['defaultexport'] = []

    elif mask not in ["", "none"]:  # deliver every mask
        try:
            mask_obj = getMetaType(node.getSchema()).getMask(mask)
            if mask_obj:
                nodedict['defaultexport'] = mask_obj.getViewHTML([node],
                                                                 flags=8)
            else:
                nodedict['defaultexport'] = "mask not found"
        except Exception as e:
            logg.exception("exception in buildNodeDescriptor")
            nodedict['defaultexport'] = "error"

    if children:
        nodedict['children'] = []
        for c in node.getChildren().sort_by_orderpos():
            nodedict['children'].append({
                'id': ustr(c.id),
                'type': c.type,
                'name': esc(c.name)
            })

    if parents:
        nodedict['parents'] = []
        for c in node.getParents().sort_by_orderpos():
            nodedict['parents'].append({
                'id': ustr(c.id),
                'type': c.type,
                'name': esc(c.name)
            })

    nd.append(nodedict)

    if (children):
        for c in node.getChildren().sort_by_orderpos():
            if c.id not in written:
                written[c.id] = None
                childnodedict = buildNodeDescriptor(params, c, indent)
                nd.append(childnodedict)

    nodeattributes_dict = {}

    if attrspec == 'none':
        # no attributes should be sent
        pass
    elif attrspec == 'default_mask' or attrspec not in ['none', 'all']:
        from contenttypes.data import make_lookup_key, get_maskcache_entry
        from core.transition.globals import request
        language = params.get('lang', '')
        lookup_key = make_lookup_key(node, language=language, labels=False)
        if 'maskcache' not in request.app_cache or lookup_key not in request.app_cache[
                'maskcache']:
            # fill cache
            node.show_node_text(labels=False, language=language)

        mask_id, field_descriptors = get_maskcache_entry(
            lookup_key, request.app_cache['maskcache'],
            request.app_cache['maskcache_accesscount'])

        try:
            for field_descriptor in field_descriptors:
                field_attribute = field_descriptor[0]
                if field_attribute not in attrlist:
                    # attrlist may be an empty list or filled from the request parameters
                    attrlist.append(field_attribute)
        except:
            # no mask for this metadata type
            logg.exception(
                "exception in buildNodeDescriptor, no 'nodesmall' or 'shortview' for node %s",
                node.id)

    elif attrspec == 'all':
        nodeattributes_dict_all_attributes = node.attributes.copy()
        if nodeattributes_dict_all_attributes:
            for k in filter(attribute_name_filter,
                            nodeattributes_dict_all_attributes.keys()):
                nodeattributes_dict[k] = nodeattributes_dict_all_attributes[k]

    if attrlist:
        for attr in filter(attribute_name_filter, attrlist):
            if attr == "nodename":
                value = node.name
            else:
                value = node.get(attr)
            nodeattributes_dict[attr] = value

    if nodeattributes_dict:
        nodedict['attributes'] = nodeattributes_dict

    if 'files' in params:

        nodedict['files'] = []

        for file in node.getFiles():
            if file.type == "metadata" or file.type == "statistic":
                continue
            mimetype = file.mimetype
            if mimetype is None:
                mimetype = "application/x-download"
            nodedict['files'].append({
                'filename': esc(file.getName()),
                'mime-type': mimetype,
                'type': file.type
            })

    if 'nodename' in params:
        nodedict['name'] = node.name

    if 'nodetype' in params:
        nodedict['type'] = node.type

    if 'nodeschema' in params:
        nodedict['schema'] = node.schema
    return nd
コード例 #51
0
ファイル: list.py プロジェクト: schnittstabil/mediatum
    def formatValues(self, context):
        valuelist = []
        items = {}
        try:
            n = context.collection
            if not isinstance(n, Node):
                raise KeyError
            field_name = context.field.getName()
            id_attr_val = count_list_values_for_all_content_children(
                n.id, field_name)
            items = {pair[0]: pair[1] for pair in id_attr_val}
        except KeyError:
            None

        tempvalues = context.field.getValueList()
        valuesfiles = context.field.files.all()

        if len(valuesfiles):  # a text file with list values was uploaded
            if os.path.isfile(valuesfiles[0].abspath):
                with codecs.open(valuesfiles[0].abspath, 'r',
                                 encoding='utf8') as valuesfile:
                    tempvalues = valuesfile.readlines()

        if len(tempvalues):  # Has the user entered any values?
            if tempvalues[0].find(
                    '|'
            ) > 0:  # there are values in different languages available
                languages = [x.strip() for x in tempvalues[0].split('|')
                             ]  # find out the languages
                valuesdict = dict(
                    (lang, []) for lang in
                    languages)  # create a dictionary with languages as keys,
                # and list of respective values as dictionary values
                for i in range(len(tempvalues)):
                    if i:  # if i not 0 - the language names itself shouldn't be included to the values
                        tmp = tempvalues[i].split('|')
                        for j in range(len(tmp)):
                            valuesdict[languages[j]].append(tmp[j])

                lang = context.language
                # if there is no default language, the first language-value will be used
                if (not lang) or (lang not in valuesdict.keys()):
                    lang = languages[0]

                tempvalues = valuesdict[lang]

        for val in tempvalues:
            indent = 0
            canbeselected = 0
            while val.startswith("*"):
                val = val[1:]
                indent = indent + 1
            if val.startswith(" "):
                canbeselected = 1
            val = val.strip()
            if not indent:
                canbeselected = 1
            if indent > 0:
                indent = indent - 1
            indentstr = "&nbsp;" * (2 * indent)

            num = 0
            if val in items.keys():
                num = int(items[val])

            try:
                if int(num) < 0:
                    raise Exception()
                elif int(num) == 0:
                    num = ""
                else:
                    num = " (" + ustr(num) + ")"
            except:
                logg.exception(
                    "exception in getMaskEditorHTML, using empty string")
                num = ""

            val = esc(val)
            if not canbeselected:
                valuelist.append(
                    ("optgroup",
                     "<optgroup label=\"" + indentstr + val + "\">", "", ""))
            elif (val in context.value.split(";")):
                valuelist.append(("optionselected", indentstr, val, num))
            else:
                valuelist.append(("option", indentstr, val, num))

        return valuelist