예제 #1
0
def writetree(req, node, f, key="", openednodes=None, sessionkey="unfoldedids", omitroot=0):
    ret = ""

    try:
        unfoldedids = req.session[sessionkey]
        len(unfoldedids)
    except:
        req.session[sessionkey] = unfoldedids = {unicode(q(Root).one().id): 1}

    if openednodes:
        # open all selected nodes and their parent nodes
        def o(u, n):
            u[n.id] = 1
            for n in n.parents:
                o(u, n)
        for n in openednodes:
            o(unfoldedids, n)
        req.session[sessionkey] = unfoldedids

    with suppress(KeyError, warn=False):
        unfold = req.params["tree_unfold"]
        unfoldedids[unfold] = 1

    with suppress(KeyError, warn=False):
        fold = req.params["tree_fold"]
        unfoldedids[fold] = 0

    if omitroot:
        for c in node.getChildren().sort("name"):
            ret += writenode(req, c, unfoldedids, f, 0, key)
    else:
        ret += writenode(req, node, unfoldedids, f, 0, key)

    return ret
예제 #2
0
def writetree(req, node, f, key="", openednodes=None, sessionkey="unfoldedids", omitroot=0):
    ret = ""

    try:
        unfoldedids = req.session[sessionkey]
        len(unfoldedids)
    except:
        req.session[sessionkey] = unfoldedids = {unicode(q(Root).one().id): 1}

    if openednodes:
        # open all selected nodes and their parent nodes
        def o(u, n):
            u[n.id] = 1
            for n in n.parents:
                o(u, n)
        for n in openednodes:
            o(unfoldedids, n)
        req.session[sessionkey] = unfoldedids

    with suppress(KeyError, warn=False):
        unfold = req.params["tree_unfold"]
        unfoldedids[unfold] = 1

    with suppress(KeyError, warn=False):
        fold = req.params["tree_fold"]
        unfoldedids[fold] = 0

    if omitroot:
        for c in node.getChildren().sort("name"):
            ret += writenode(req, c, unfoldedids, f, 0, key)
    else:
        ret += writenode(req, node, unfoldedids, f, 0, key)

    return ret
예제 #3
0
def getAdminModules(path):
    mods = {}
    for root, dirs, files in path:
        if os.path.basename(root) not in ("test", "__pycache__"):
            for name in [
                    f for f in files
                    if f.endswith(".py") and f != "__init__.py"
            ]:
                m = __import__("web.admin.modules." + name[:-3])
                m = eval("m.admin.modules." + name[:-3])
                mods[name[:-3]] = m

    # test for external modules by plugin
    for k, v in config.getsubset("plugins").items():
        path, module = splitpath(v)
        with suppress(ImportError, warn=False):  # no admin modules in plugin
            sys.path += [path + ".adminmodules"]
            for root, dirs, files in os.walk(
                    os.path.join(config.basedir, v + "/adminmodules")):
                for name in [
                        f for f in files
                        if f.endswith(".py") and f != "__init__.py"
                ]:
                    m = __import__(module + ".adminmodules." + name[:-3])
                    m = eval("m.adminmodules." + name[:-3])
                    mods[name[:-3]] = m
    return mods
예제 #4
0
 def refill_buffer(self):
     while 1:
         if len(self.producer_fifo):
             p = self.producer_fifo.first()
             # a 'None' in the producer fifo is a sentinel,
             # telling us to close the channel.
             if p is None:
                 if not self.ac_out_buffer:
                     self.producer_fifo.pop()
                     try:
                         self.close()
                     except KeyError:
                         # FIXME: tends to happen sometimes, seems to
                         # be a race condition in asyncore
                         with suppress(Exception, warn=False):
                             self._fileno = None
                             self.socket.close()
                 return
             elif isinstance(p, str):
                 self.producer_fifo.pop()
                 self.ac_out_buffer = self.ac_out_buffer + p
                 return
             data = p.more()
             if data:
                 self.ac_out_buffer = self.ac_out_buffer + data
                 return
             else:
                 self.producer_fifo.pop()
         else:
             return
예제 #5
0
    def getEditorHTML(self,
                      field,
                      value="",
                      width=400,
                      name="",
                      lock=0,
                      language=None,
                      required=None):
        fielddef = field.getValues().split(
            "\r\n")  # url(source), type, name variable, value variable
        if name == "":
            name = field.getName()
        while len(fielddef) < 5:
            fielddef.append("")

        valuelist = []
        with suppress(ValueError, warn=False):
            # enables the field to be added without fields filled in without throwing an exception
            if fielddef[1] == 'json':
                opener = urllib2.build_opener()
                f = opener.open(urllib2.Request(fielddef[0], None, {}))
                data = json.load(f)
                data.sort(lambda x, y: cmp(x[fielddef[2]], y[fielddef[2]]))
                for item in data:
                    valuelist.append({
                        'select_text':
                        fielddef[4].replace(fielddef[2],
                                            item[fielddef[2]]).replace(
                                                fielddef[3],
                                                item[fielddef[3]]),
                        'select_value':
                        item[fielddef[3]]
                    })
                f.close()
            elif fielddef[1] == 'list':
                opener = urllib2.build_opener()
                f = opener.open(urllib2.Request(fielddef[0], None, {}))
                for item in f.read().split("\n"):
                    if not item.startswith("#"):
                        if ";" in item:
                            _v, _t = item.split(";")
                        else:
                            _v = _t = item
                        valuelist.append({
                            'select_text': _t.strip(),
                            'select_value': _v.strip()
                        })
                f.close()
        return tal.getTAL("metadata/dlist.html", {
            "lock": lock,
            "name": name,
            "width": width,
            "value": value,
            "valuelist": valuelist,
            "fielddef": fielddef,
            "required": self.is_required(required)
        },
                          macro="editorfield",
                          language=language)
예제 #6
0
 def r(p):
     if os.path.isdir(os.path.join(path, p)):
         for file in os.listdir(os.path.join(path, p)):
             r(os.path.join(p, file))
     else:
         while len(p) > 0 and p[0] == "/":
             p = p[1:]
         with suppress(Exception, warn=False):
             zip.write(os.path.join(path, p), p)
예제 #7
0
    def runAction(self, node, op=""):
        attrs = ""
        for k, v in node.attrs.items():
            attrs += v
        if not checkXMLString(u'<?xml version="1.0" encoding="UTF-8"?>' + u'<tag>' + attrs + u'</tag>'):
            with suppress(Exception, warn=False):
                mail.sendmail(self.get('from'), self.get('to'), self.get('subject'), self.get('text'))

        self.forward(node, True)
예제 #8
0
def loadServices():
    datapath = config.get("services.datapath", "")
    if not os.path.exists(os.path.join(datapath, "common")):
        with suppress(OSError, warn=False):
            os.makedirs(os.path.join(datapath, "common"))

    def manageService(servicename, servicedir, servicedata):
        if not os.path.exists(servicedir + "services/" + servicename +
                              "/__init__.py"):
            return

        if config.get('services.' + servicename + '.activate',
                      "").lower() == "false":
            return
        if servicename + '.basecontext' in config.getsubset("services").keys():
            basecontext = config.getsubset("services")[servicename +
                                                       '.basecontext']
        else:
            basecontext = config.get("services.contextprefix",
                                     u"services") + '/' + servicename
        basecontext = ('/' + basecontext).replace('//', '/').replace('//', '/')
        context = _request_handler.addContext(str(basecontext), ".")
        file = context.addFile(servicedir + "services/" + servicename)

        if hasattr(file.m, "request_handler"):
            file.addHandler("request_handler").addPattern("/.*")

            if not os.path.exists(servicedata):
                try:
                    os.makedirs(servicedata)
                    os.makedirs(os.path.join(servicedata, "cache"))
                except OSError:
                    return

    if config.get("services.activate", "").lower() == "true":
        # try loading services from mediatum web/services/ folder
        p = config.basedir + "/web/services/"
        for servicedir in [
                f for f in os.listdir(p) if os.path.isdir(os.path.join(p, f))
        ]:
            manageService(servicedir, "web/",
                          os.path.join(datapath, servicedir))

        # try loading services from all plugins services/ folder
        for k, v in config.getsubset("plugins").items():
            p = os.path.join(config.basedir, v, 'services')
            if os.path.exists(p):
                for servicedir in [
                        f for f in os.listdir(p)
                        if os.path.isdir(os.path.join(p, f))
                ]:
                    manageService(servicedir, v,
                                  os.path.join(datapath, k, servicedir))

    else:
        logg.info("web services not activated")
예제 #9
0
    def getEditorHTML(self,
                      field,
                      value="",
                      width=40,
                      lock=0,
                      language=None,
                      required=None):
        check_context()

        try:
            fieldname = field.name
        except:
            fieldname = None

        try:
            warning = [
                t[1] for t in self.labels[language]
                if t[0] == 'upload_notarget_warning'
            ][0]
        except:
            logg.exception(
                "exception in getEditorHTML, using default language")
            warning = [
                t[1] for t in self.labels[getDefaultLanguage()]
                if t[0] == 'upload_notarget_warning'
            ][0]

        context = {
            "lock": lock,
            "value": value,
            "width": width,
            "name": field.getName(),
            "field": field,
            "language": language,
            "warning": warning,
            "system_lock": 0,
            "required": self.is_required(required)
        }

        if lock:
            context['system_lock'] = 1
        with suppress(Exception, warn=False):
            if field.get("system.lock"):
                context['system_lock'] = 1

        s = tal.getTAL("metadata/upload.html",
                       context,
                       macro="editorfield",
                       language=language)
        if field.getName():
            s = s.replace("____FIELDNAME____", "%s" % field.getName())
        elif fieldname:
            s = s.replace("____FIELDNAME____", "%s" % fieldname)
        else:
            logg.warn("metadata: m_upload: no fieldname found")
        return s
예제 #10
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)
예제 #11
0
    def runAction(self, node, op=""):
        attrs = ""
        for k, v in node.attrs.items():
            attrs += v
        if not checkXMLString(u'<?xml version="1.0" encoding="UTF-8"?>' +
                              u'<tag>' + attrs + u'</tag>'):
            with suppress(Exception, warn=False):
                mail.sendmail(self.get('from'), self.get('to'),
                              self.get('subject'), self.get('text'))

        self.forward(node, True)
예제 #12
0
    def getMetaEditor(self, item, req):
        """ editor mask for vgroup-field definition """
        fieldlist = getAllMetaFields()
        if len(item.getParents()) == 0:
            pid = req.params.get("pid", "")
        else:
            pid = item.getParents()[0].id

        if req.params.get("edit") == "None":
            item = Maskitem(name="", type="maskitem")
            item.set("type", "vgroup")

        details = ""
        i = 0
        for field in item.getChildren().sort_by_orderpos():
            f = getMetadataType(field.get("type"))
            details += f.getMetaHTML(item, i, False, fieldlist=fieldlist, language=lang(req))
            i += 1

        if req.params.get("sel_id", "") != "":
            i = 0
            for id in req.params.get("sel_id")[:-1].split(";"):
                f = getMetadataType(q(Node).get(id).get("type"))
                with suppress(TypeError, warn=False):
                    details += f.getMetaHTML(item, i, False, itemlist=req.params.get("sel_id")
                                             [:-1].split(";"), ptype="vgroup", fieldlist=fieldlist)
                i += 1
        fields = []
        metadatatype = req.params.get("metadatatype")

        if req.params.get("op", "") == "new":
            pidnode = q(Node).get(req.params.get("pid"))
            if pidnode.get("type") in ("vgroup", "hgroup"):
                for field in pidnode.all_children:
                    if field.getType().getName() == "maskitem" and field.id != pidnode.id:
                        fields.append(field)
            else:
                for m in metadatatype.getMasks():
                    if ustr(m.id) == ustr(req.params.get("pid")):
                        for field in m.getChildren():
                            fields.append(field)
        fields.sort(lambda x, y: cmp(x.getOrderPos(), y.getOrderPos()))

        v = {}
        v["pid"] = pid
        v["item"] = item
        v["op"] = req.params.get("op", "")
        v["details"] = details
        v["fields"] = fields
        v["selid"] = req.params.get("sel_id", "")
        return req.getTAL("schema/mask/vgroup.html", v, macro="metaeditor")
예제 #13
0
def upload_ziphandler(req):
    schemes = get_permitted_schemas()
    files = []
    scheme_type = {}
    basenode = q(Node).get(req.params.get('id'))
    for file in basenode.files:
        if file.abspath.endswith(req.params.get('file')):
            z = zipfile.ZipFile(file.abspath)
            for f in z.namelist():
                #strip unwanted garbage from string
                name = mybasename(f).decode('utf8', 'ignore').encode('utf8')
                random_str = ustr(random.random())[2:]
                if name.startswith("._"):  # ignore Mac OS X junk
                    continue
                if name.split('.')[0] == '':
                    name = random_str + name

                files.append(name.replace(" ", "_"))
                _m = getMimeType(name)

                if random_str in name:
                    newfilename = join_paths(config.get("paths.tempdir"),
                                             name.replace(" ", "_"))
                else:
                    newfilename = join_paths(
                        config.get("paths.tempdir"),
                        random_str + name.replace(" ", "_"))

                with codecs.open(newfilename, "wb") as fi:
                    fi.write(z.read(f))

                fn = importFileToRealname(mybasename(name.replace(" ", "_")),
                                          newfilename)
                basenode.files.append(fn)
                if os.path.exists(newfilename):
                    os.unlink(newfilename)

                if _m[1] not in scheme_type:
                    scheme_type[_m[1]] = []
                    for scheme in schemes:
                        if _m[1] in scheme.getDatatypes():
                            scheme_type[_m[1]].append(scheme)
            with suppress(Exception, warn=False):
                z.close()
                os.remove(file.abspath)
            basenode.files.remove(file)
            db.session.commit()
    return {'files': files, 'schemes': scheme_type}
예제 #14
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)
예제 #15
0
def loadServices():
    datapath = config.get("services.datapath", "")
    if not os.path.exists(os.path.join(datapath, "common")):
        with suppress(OSError, warn=False):
            os.makedirs(os.path.join(datapath, "common"))

    def manageService(servicename, servicedir, servicedata):
        if not os.path.exists(servicedir + "services/" + servicename + "/__init__.py"):
            return

        if config.get('services.' + servicename + '.activate', "").lower() == "false":
            return
        if servicename + '.basecontext' in config.getsubset("services").keys():
            basecontext = config.getsubset("services")[servicename + '.basecontext']
        else:
            basecontext = config.get("services.contextprefix", u"services") + '/' + servicename
        basecontext = ('/' + basecontext).replace('//', '/').replace('//', '/')
        context = athana.addContext(str(basecontext), ".")
        file = context.addFile(servicedir + "services/" + servicename)

        if hasattr(file.m, "request_handler"):
            file.addHandler("request_handler").addPattern("/.*")

            if not os.path.exists(servicedata):
                try:
                    os.makedirs(servicedata)
                    os.makedirs(os.path.join(servicedata, "cache"))
                except OSError:
                    return

    if config.get("services.activate", "").lower() == "true":
        # try loading services from mediatum web/services/ folder
        p = config.basedir + "/web/services/"
        for servicedir in [f for f in os.listdir(p) if os.path.isdir(os.path.join(p, f))]:
            manageService(servicedir, "web/", os.path.join(datapath, servicedir))

        # try loading services from all plugins services/ folder
        for k, v in config.getsubset("plugins").items():
            p = os.path.join(config.basedir, v, 'services')
            if os.path.exists(p):
                for servicedir in [f for f in os.listdir(p) if os.path.isdir(os.path.join(p, f))]:
                    manageService(servicedir, v, os.path.join(datapath, k, servicedir))

    else:
        logg.info("web services not activated")
예제 #16
0
def changeOrder(parent, up, down):
    """ change order of given nodes """
    i = 0
    for child in parent.children.sort_by_orderpos():
        with suppress(Exception, warn=False):
            if i == up:
                pos = i - 1
            elif i == up - 1:
                pos = up
            elif i == down:
                pos = i + 1
            elif i == down + 1:
                pos = down
            else:
                pos = i
            child.orderpos = pos
            db.session.commit()
            i = i + 1
예제 #17
0
    def getEditorHTML(self, field, value="", width=400, lock=0, language=None, required=None):
        d = field.getSystemFormat(field.getValues())

        if value == "?":
            value = date.format_date(date.now(), d.getValue())
        with suppress(Exception, warn=False):
            value = date.format_date(date.parse_date(value), d.getValue())
        return tal.getTAL("metadata/date.html", {"lock": lock,
                                                 "value": value,
                                                 "width": width,
                                                 "name": field.getName(),
                                                 "field": field,
                                                 "pattern": self.get_input_pattern(field),
                                                 "title": self.get_input_title(field),
                                                 "placeholder": self.get_input_placeholder(field),
                                                 "required": self.is_required(required)},
                          macro="editorfield",
                          language=language)
예제 #18
0
def changeOrder(parent, up, down):
    """ change order of given nodes """
    i = 0
    for child in parent.children.sort_by_orderpos():
        with suppress(Exception, warn=False):
            if i == up:
                pos = i - 1
            elif i == up - 1:
                pos = up
            elif i == down:
                pos = i + 1
            elif i == down + 1:
                pos = down
            else:
                pos = i
            child.orderpos = pos
            db.session.commit()
            i = i + 1
예제 #19
0
파일: upload.py 프로젝트: mediatum/mediatum
def upload_ziphandler(req):
    schemes = get_permitted_schemas()
    files = []
    scheme_type = {}
    basenode = q(Node).get(req.params.get('id'))
    for file in basenode.files:
        if file.abspath.endswith(req.params.get('file')):
            z = zipfile.ZipFile(file.abspath)
            for f in z.namelist():
                #strip unwanted garbage from string
                name = mybasename(f).decode('utf8', 'ignore').encode('utf8')
                random_str = ustr(random.random())[2:]
                if name.startswith("._"):  # ignore Mac OS X junk
                    continue
                if name.split('.')[0] == '':
                    name = random_str + name

                files.append(name.replace(" ", "_"))
                _m = getMimeType(name)

                if random_str in name:
                    newfilename = join_paths(config.get("paths.tempdir"), name.replace(" ", "_"))
                else:
                    newfilename = join_paths(config.get("paths.tempdir"),  random_str + name.replace(" ", "_"))

                with codecs.open(newfilename, "wb") as fi:
                    fi.write(z.read(f))

                fn = importFileToRealname(mybasename(name.replace(" ", "_")), newfilename)
                basenode.files.append(fn)
                if os.path.exists(newfilename):
                    os.unlink(newfilename)

                if _m[1] not in scheme_type:
                    scheme_type[_m[1]] = []
                    for scheme in schemes:
                        if _m[1] in scheme.getDatatypes():
                            scheme_type[_m[1]].append(scheme)
            with suppress(Exception, warn=False):
                z.close()
                os.remove(file.abspath)
            basenode.files.remove(file)
            db.session.commit()
    return {'files': files, 'schemes': scheme_type}
예제 #20
0
def getAdminModules(path):
    mods = {}
    for root, dirs, files in path:
        if os.path.basename(root) not in ("test", "__pycache__"):
            for name in [f for f in files if f.endswith(".py") and f != "__init__.py"]:
                m = __import__("web.admin.modules." + name[:-3])
                m = eval("m.admin.modules." + name[:-3])
                mods[name[:-3]] = m

    # test for external modules by plugin
    for k, v in config.getsubset("plugins").items():
        path, module = splitpath(v)
        with suppress(ImportError,warn=False): # no admin modules in plugin
            sys.path += [path + ".adminmodules"]
            for root, dirs, files in os.walk(os.path.join(config.basedir, v + "/adminmodules")):
                for name in [f for f in files if f.endswith(".py") and f != "__init__.py"]:
                    m = __import__(module + ".adminmodules." + name[:-3])
                    m = eval("m.adminmodules." + name[:-3])
                    mods[name[:-3]] = m
    return mods
예제 #21
0
파일: dlist.py 프로젝트: mediatum/mediatum
    def getEditorHTML(self, field, value="", width=400, name="", lock=0, language=None, required=None):
        fielddef = field.getValues().split("\r\n")  # url(source), type, name variable, value variable
        if name == "":
            name = field.getName()
        while len(fielddef) < 5:
            fielddef.append("")

        valuelist = []
        with suppress(ValueError, warn=False):
            # enables the field to be added without fields filled in without throwing an exception
            if fielddef[1] == 'json':
                opener = urllib2.build_opener()
                f = opener.open(urllib2.Request(fielddef[0], None, {}))
                data = json.load(f)
                data.sort(lambda x, y: cmp(x[fielddef[2]], y[fielddef[2]]))
                for item in data:
                    valuelist.append({'select_text': fielddef[4].replace(fielddef[2], item[fielddef[2]]).replace(
                        fielddef[3], item[fielddef[3]]), 'select_value': item[fielddef[3]]})
                f.close()
            elif fielddef[1] == 'list':
                opener = urllib2.build_opener()
                f = opener.open(urllib2.Request(fielddef[0], None, {}))
                for item in f.read().split("\n"):
                    if not item.startswith("#"):
                        if ";" in item:
                            _v, _t = item.split(";")
                        else:
                            _v = _t = item
                        valuelist.append({'select_text': _t.strip(), 'select_value': _v.strip()})
                f.close()
        return tal.getTAL("metadata/dlist.html", {"lock": lock,
                                                  "name": name,
                                                  "width": width,
                                                  "value": value,
                                                  "valuelist": valuelist,
                                                  "fielddef": fielddef,
                                                  "required": self.is_required(required)},
                          macro="editorfield",
                          language=language)
예제 #22
0
 def runAction(self, node, op=""):
     for p in node.parents:
         with suppress(Exception, warn=False):
             p.children.remove(node)
             db.session.commit()
예제 #23
0
def getContent(req, ids):
    node = q(Node).get(ids[0])
    user = current_user
    if not node.has_write_access() or "editor" in user.hidden_edit_functions:
        req.setStatus(httpstatus.HTTP_FORBIDDEN)
        return req.getTAL("web/edit/edit.html", {}, macro="access_error")

    if req.params.get('file') == "config":  # configuration file for ckeditor
        req.reply_headers['Content-Type'] = "application/javascript"
        return req.writeTAL("web/edit/modules/startpages.html", {'id': ids[0], 'lang': lang(req)}, macro="ckconfig")

    if "action" in req.params:
        if req.params.get('action') == "getfile":  # deliver filecontent
            data = ""
            for f in [f for f in node.files if f.mimetype == "text/html"]:
                filepath = f.abspath.replace(config.get("paths.datadir"), '')
                if req.params.get('filename') == filepath and os.path.exists(config.get("paths.datadir") + filepath):
                    with codecs.open(config.get("paths.datadir") + filepath, "r", encoding='utf8') as fil:
                        data = fil.read()
                    logg.info("%s opened startpage %s for node %s (%s, %s)", user.login_name, filepath, node.id, node.name, node.type)
                    break
            req.write(json.dumps({'filecontent': data}, ensure_ascii=False))
        if req.params.get('action') == "save":  # save filedata
            if req.params.get('filename') == "add":  # add new file
                maxid = 0
                for f in [f for f in node.files if f.type == "content"]:
                    with suppress(ValueError, warn=False):
                        if int(f.abspath[:-5].split("_")[-1]) >= maxid:
                            maxid = int(f.abspath[:-5].split("_")[-1]) + 1
                filename = 'html/%s_%s.html' % (req.params.get('id'), maxid)
                while os.path.exists(config.get("paths.datadir") + filename):
                    maxid = maxid + 1
                    filename = 'html/%s_%s.html' % (req.params.get('id'), maxid)
                with codecs.open(config.get("paths.datadir") + filename, "w", encoding='utf8') as fil:
                    fil.write(req.params.get('data'))
                node.files.append(File(filename, u"content", u"text/html"))
                db.session.commit()
                req.write(json.dumps({'filename': '', 'state': 'ok'}))
                logg.info("%s added startpage %s for node %s (%s, %s)", user.login_name, filename, node.id, node.name, node.type)
                return None
            else:
                for f in [f for f in node.files if f.mimetype == "text/html"]:
                    filepath = f.abspath.replace(config.get("paths.datadir"), '')
                    if req.params.get('filename') == filepath and os.path.exists(config.get("paths.datadir") + filepath):
                        with open(config.get("paths.datadir") + filepath, "w") as fil:
                            try:
                                fil.write(req.params.get('data'))
                            except UnicodeEncodeError:
                                # some unicode characters like 'Black Circle' &#9679; are not translated in the
                                # html entity by the current ckeditor version
                                fil.write(req.params.get('data').encode('ascii', 'xmlcharrefreplace'))

                        req.write(json.dumps(
                            {'filesize': format_filesize(os.path.getsize(config.get("paths.datadir") + filepath)),
                             'filename': req.params.get('filename'), 'state': 'ok'}, ensure_ascii=False))
                        logg.info("%s saved startpage %s for node %s (%s, %s)", user.login_name, filepath, node.id, node.name, node.type)
                        break
        return None

    if "option" in req.params:
        if req.params.get("option") == "filebrowser":  # open filebrowser
            logg.info("%s opening ckeditor filebrowser for node %s (%r, %r)", user.login_name, node.id, node.name, node.type)
            req.write(send_nodefile_tal(req))
            return ""

        if req.params.get("option") == "htmlupload":  # use fileupload
            logg.info("%s going to use ckeditor fileupload (htmlupload) for node %s (%s, %s)",
                      user.login_name, node.id, node.name, node.type)
            req.write(upload_for_html(req))
            return ""

        if "delete" in req.params:  # delete file via CKeditor
            for f in node.files:
                if f.abspath.endswith(req.params.get('option')):
                    filepath = f.abspath.replace(config.get("paths.datadir"), '')
                    logg.info("%s going to delete ckeditor filebrowser file %s for node %s (%s, %s)",
                              user.login_name, filepath, node.id, node.name, node.type)
                    if os.path.exists(f.abspath):
                        os.remove(f.abspath)
                        node.files.remove(f)
                    break
            db.session.commit()
            return ""

    for key in req.params.keys():
        if key.startswith("delete_"):  # delete page
            page = key[7:-2]
            try:
                file_shortpath = page.replace(config.get("paths.datadir"), "")
                fullpath = os.path.join(config.get("paths.datadir"), page)
                if os.path.exists(fullpath):
                    os.remove(fullpath)
                    logg.info("%s removed file %s from disk", user.login_name, fullpath)
                else:
                    logg.warn("%s could not remove file %s from disk: not existing", user.login_name, fullpath)
                filenode = q(File).filter_by(path=page, mimetype=u"text/html").one()
                with suppress(KeyError, warn=False):
                    del node.system_attrs["startpagedescr." + file_shortpath]
                node.system_attrs["startpage_selector"] = node.system_attrs["startpage_selector"].replace(file_shortpath, "")
                node.files.remove(filenode)
                db.session.commit()
                logg.info("%s - startpages - deleted File and file for node %s (%s): %s, %s, %s, %s",
                        user.login_name, node.id, node.name, page, filenode.path, filenode.filetype, filenode.mimetype)
            except:
                logg.exception("%s - startpages - error while delete File and file for %s, exception ignored", user.login_name, page)
            break

    if "save_page" in req.params:  # save page
        content = ""
        for key in req.params.keys():
            if key.startswith("page_content"):
                content = req.params.get(key, "")
                break

        with open(req.params.get('file_path'), "w", encoding='utf8') as fi:
            fi.writelines(content)

        del req.params['save_page']
        del req.params['file_to_edit']
        req.params['tab'] = 'startpages'
        return getContent(req, [node.id])

    if "cancel_page" in req.params:
        del req.params['file_to_edit']
        del req.params['cancel_page']
        return getContent(req, [node.id])

    filelist = []
    for f in node.files:
        if f.mimetype == 'text/html' and f.getType() in ['content']:
            filelist.append(f)

    db.session.commit()

    if "startpages_save" in req.params.keys():  # user saves startpage configuration
        logg.info("%s going to save startpage configuration for node %s (%s, %s): %s",
                  user.login_name, node.id, node.name, node.type, req.params)

        sidebar = ""
        for k in [k for k in req.params if k.startswith('sidebar_')]:
            sidebar += "%s:%s;" % (k[8:], req.params[k])
        node.set('system.sidebar', sidebar)

        for k in [k for k in req.params if k.startswith('descr.')]:
            node.system_attrs['startpage' + k] = req.params[k]

        # build startpage_selector
        startpage_selector = ""
        for language in config.languages:
            startpage_selector += "%s:%s;" % (language, req.params.get('radio_' + language))
        node.system_attrs['startpage_selector'] = startpage_selector[0:-1]
    named_filelist = []

    for f in filelist:
        long_path = f.abspath
        short_path = long_path.replace(config.get("paths.datadir"), '')

        file_exists = os.path.isfile(long_path)
        file_size = "-"
        if file_exists:
            file_size = os.path.getsize(long_path)

        langlist = []
        sidebar = []
        for language in config.languages:
            spn = node.getStartpageFileNode(language)
            if spn and spn.abspath == long_path:
                langlist.append(language)
            if node.system_attrs.get('sidebar', '').find(language + ":" + short_path) >= 0:
                sidebar.append(language)

        named_filelist.append((short_path,
                               node.system_attrs.get('startpagedescr.' + short_path),
                               f.type,
                               f,
                               file_exists,
                               format_filesize(file_size),
                               long_path,
                               langlist,
                               "/file/%s/%s" % (req.params.get("id", "0"), short_path.split('/')[-1]),
                               sidebar))
    lang2file = node.getStartpageDict()

    # compatibility: there may be old startpages in the database that
    # are not described by node attributes
    initial = filelist and not lang2file

    # node may not have startpage set for some language
    # compatibilty: node may not have system attribute startpage_selector
    # build startpage_selector and write back to node
    startpage_selector = ""
    for language in config.languages:
        if initial:
            lang2file[language] = named_filelist[0][0]
        else:
            lang2file[language] = lang2file.setdefault(language, '')
        startpage_selector += "%s:%s;" % (language, lang2file[language])

    node.system_attrs['startpage_selector'] = startpage_selector[0:-1]

    db.session.commit()

    named_filelist.sort(lambda x, y: cmp(x[1], y[1]))

    v = {"id": req.params.get("id", "0"),
         "tab": req.params.get("tab", ""),
         "node": node,
         "named_filelist": named_filelist,
         "languages": config.languages,
         "lang2file": lang2file,
         "types": ['content'],
         "d": lang2file and True,
         "csrf": req.csrf_token.current_token
         }

    return req.getTAL("web/edit/modules/startpages.html", v, macro="edit_startpages")
예제 #24
0
    def get_source(self, environment, template):
        for loader, local_name in self._iter_loaders(template):
            with suppress(TemplateNotFound, warn=False):
                return loader.get_source(environment, local_name)

        raise TemplateNotFound(template)
예제 #25
0
    def get_source(self, environment, template):
        for loader, local_name in self._iter_loaders(template):
            with suppress(TemplateNotFound, warn=False):
                return loader.get_source(environment, local_name)

        raise TemplateNotFound(template)
예제 #26
0
def detect_athana_or_flask():
    with suppress(Exception, warn=False):
        from flask import current_app
        if current_app:
            return "flask"
    return "athana"
예제 #27
0
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)
예제 #28
0
파일: delete.py 프로젝트: mediatum/mediatum
 def runAction(self, node, op=""):
     for p in node.parents:
         with suppress(Exception, warn=False):
             p.children.remove(node)
             db.session.commit()
예제 #29
0
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)
예제 #30
0
def place_pic(fn_in, fn_pic, fn_out, x0, y0, scale=1.0, mask=None, pages=[], follow_rotate=False):

    pdf = PdfFileReader(file(fn_in, "rb"))

    output = PdfFileWriter()
    outputStream = file(fn_out, "wb")

    width, height = pic_pdf_size = get_pdf_pagesize(fn_in)
    pdf_dim = get_pdf_dimensions(fn_in)
    pic_width, pic_height = get_pic_size(fn_pic)

    pagecount = pdf_dim['numPages']

    d_watermark = {}

    for i in range(pagecount):

        p = pdf.getPage(i)

        if i in pages:
            rotate = int(pdf_dim['d_pageno2rotate'][i]) % 360
            w, h = pdf_dim['d_pageno2size'][i]
            max_wh = max(w, h)
            translation_scale = 1.0
            key = "%.3f|%.3f|%d" % (float(w), float(h), int(rotate))
            if key in d_watermark:
                watermark = d_watermark[key][0]
            else:
                s_out = StringIO.StringIO()
                c = canvas.Canvas(s_out, pagesize=(w, h))
                x1 = x0 * translation_scale
                y1 = y0 * translation_scale - 0.51
                if rotate and not follow_rotate:

                    if rotate == 90:
                        c.translate(w, 0)
                    elif rotate == 180:
                        c.translate(w, h)
                    elif rotate == 270:
                        c.translate(0, h)

                    c.rotate(rotate)

                c.drawImage(fn_pic,
                            x1,
                            y1,
                            width=int(pic_width * scale + 0.5),
                            height=int(pic_height * scale + 0.5),
                            preserveAspectRatio=True,
                            mask=mask)
                c.save()

                watermark = PdfFileReader(s_out)
                d_watermark[key] = (watermark, s_out)

            p.mergePage(watermark.getPage(0))

        output.addPage(p)

    output.write(outputStream)

    outputStream.close()

    for k in d_watermark:
        with suppress(Exception, warn=False):
            d_watermark[k][1].close()
    pdf.stream.close()

    return
예제 #31
0
def getContent(req, ids):

    def getSchemes(req):
        schemes = AccessData(req).filter(loadTypesFromDB())
        return filter(lambda x: x.isActive(), schemes)

    ret = ""
    v = {"message": ""}

    if len(ids) >= 0:
        ids = ids[0]

    v["id"] = ids

    if "do_action" in req.params.keys():  # process nodes
        fieldname = req.params.get("fields")
        old_values = u(req.params.get("old_values", "")).split(";")
        new_value = u(req.params.get("new_value"))
        basenode = q(Node).get(ids)
        entries = getAllAttributeValues(fieldname, req.params.get("schema"))

        c = 0
        for old_val in old_values:
            for n in AccessData(req).filter(q(Node).filter(Node.id.in_(entries[old_val])).all()):
                with suppress(Exception, warn=False):
                    n.set(fieldname, replaceValue(n.get(fieldname), u(old_val), u(new_value)))
                    c += 1
        v["message"] = req.getTAL("web/edit/modules/manageindex.html", {"number": c}, macro="operationinfo")

    if "style" in req.params.keys():  # load schemes
        if req.params.get("action", "") == "schemes":
            v["schemes"] = getSchemes(req)
            req.writeTAL("web/edit/modules/manageindex.html", v, macro="schemes_dropdown")
            return ""

        elif req.params.get("action", "").startswith("indexfields__"):  # load index fields
            schema = getMetaType(req.params.get("action", "")[13:])
            fields = []
            for field in schema.getMetaFields():
                if field.getFieldtype() == "ilist":
                    fields.append(field)
            v["fields"] = fields
            v["schemaname"] = schema.getName()
            req.writeTAL("web/edit/modules/manageindex.html", v, macro="fields_dropdown")
            return ""

        elif req.params.get("action", "").startswith("indexvalues__"):  # load values of selected indexfield
            node = q(Node).get(ids)
            fieldname = req.params.get("action").split("__")[-2]
            schema = req.params.get("action").split("__")[-1]
            v["entries"] = []
            if node:
                v["entries"] = getAllAttributeValues(fieldname, schema)
                v["keys"] = v["entries"].keys()
                v["keys"].sort(lambda x, y: cmp(x.lower(), y.lower()))
            req.writeTAL("web/edit/modules/manageindex.html", v, macro="fieldvalues")
            return ""

        elif req.params.get("action", "").startswith("children__"):  # search for children of current collection
            scheme = req.params.get("action", "").split("__")[1]
            fieldname = req.params.get("action", "").split("__")[2]
            values = req.params.get("action", "").split("__")[3].split(";")[:-1]
            all_values = getAllAttributeValues(fieldname, scheme)

            def isChildOf(access, node, basenodeid):
                for ls in getPaths(node):
                    if basenodeid in [unicode(n.id) for n in ls]:
                        return 1
                return 0

            subitems = {}
            for value in values:
                value = u(value)
                if value in all_values:
                    subitems[value] = []
                    for l in all_values[value]:
                        if isChildOf(AccessData(req), q(Node).get(l), ids):
                            subitems[value].append(l)

            v["items"] = subitems
            v["keys"] = subitems.keys()
            v["keys"].sort()
            req.writeTAL("web/edit/modules/manageindex.html", v, macro="valueinfo")
            return ""

    else:
        v["csrf"] = req.csrf_token.current_token
        return req.getTAL("web/edit/modules/manageindex.html", v, macro="manageform")
예제 #32
0
    def show_workflow_node(self, node, req, data=None):

        check_context()

        user = users.getUserFromRequest(req)

        current_workflow = getNodeWorkflow(node)
        current_workflow_step = getNodeWorkflowStep(node)

        FATAL_ERROR = False
        FATAL_ERROR_STR = ""

        if "gotrue" in req.params:

            if not PYPDF_MODULE_PRESENT:
                del req.params['gotrue']
                return self.show_workflow_node(node, req)

            radio_apply_reset_accept = req.params.get(
                'radio_apply_reset_accept', '')

            if radio_apply_reset_accept == 'reset':
                for f in node.files:
                    f_name = f.base_name
                    if f_name.startswith(
                            'addpic2pdf_%s_node_%s_' %
                        (unicode(current_workflow_step.id), unicode(
                            node.id))) and f.filetype.startswith('p_document'):
                        logg.info(
                            "workflow step addpic2pdf(%s): going to remove file '%s' from node '%s' (%s) for request from user '%s' (%s)",
                            current_workflow_step.id, f_name, node.name,
                            node.id, user.login_name, req.ip)
                        node.files.remove(f)
                        db.session.commit()
                        try:
                            os.remove(f.abspath)
                        except:
                            logg.exception(
                                "exception in workflow setep addpic2pdf, removing file failed, ignoring"
                            )

                del req.params['gotrue']
                return self.show_workflow_node(node, req)

            elif radio_apply_reset_accept == 'accept':

                p_document_files = [
                    f for f in node.files
                    if f.filetype == 'p_document' and f.base_name.startswith(
                        'addpic2pdf_%s_node_%s_' %
                        (unicode(current_workflow_step.id), unicode(node.id)))
                ]

                if len(p_document_files) > 0:

                    p_document_file = p_document_files[0]

                    document_file = [
                        f for f in node.files if f.filetype == 'document'
                    ][0]

                    o_document_file = File(document_file.path, 'o_document',
                                           document_file.mimetype)

                    node.files.remove(document_file)
                    node.files.append(o_document_file)
                    o_document_name = o_document_file.base_name

                    for f in node.files:
                        if f.filetype in [
                                'thumb', 'fileinfo', 'fulltext'
                        ] or f.filetype.startswith('present'):
                            if os.path.splitext(
                                    f.base_name)[0] == os.path.splitext(
                                        o_document_name)[0]:
                                new_f = File(f.path, 'o_' + f.filetype,
                                             f.mimetype)
                                node.files.remove(f)
                                node.files.append(new_f)

                    new_document_file = File(p_document_file.path, 'document',
                                             p_document_file.mimetype)
                    node.files.remove(p_document_file)
                    node.files.append(new_document_file)
                    db.session.commit()
                    node.event_files_changed()

                del req.params['gotrue']
                return self.forwardAndShow(node, True, req)

            elif radio_apply_reset_accept == 'apply':

                drag_logo_fullname = req.params.get("input_drag_logo_fullname",
                                                    None)

                if not drag_logo_fullname:
                    req.params["addpic2pdf_error"] = "%s: %s" % (
                        format_date().replace('T', ' - '),
                        t(lang(req),
                          "admin_wfstep_addpic2pdf_no_logo_selected"))
                    del req.params['gotrue']
                    return self.show_workflow_node(node, req)

                drag_logo_filepath = [
                    f.abspath for f in current_workflow_step.files
                    if f.base_name == drag_logo_fullname
                ][0]

                pos_cm = req.params.get("input_poffset_cm", "0, 0")
                x_cm, y_cm = [float(x.strip()) for x in pos_cm.split(",")]

                pdf_in_filepath = getPdfFilepathForProcessing(
                    current_workflow_step, node)

                current_pageno = int(
                    req.params.get("input_current_page", "0").strip())

                radio_select_targetpages = req.params.get(
                    "radio_select_targetpages", "").strip()
                input_select_targetpages = req.params.get(
                    "input_select_targetpages", "").strip()

                printer_range = []
                page_count = get_pdf_pagecount(pdf_in_filepath)
                _parser_error = False

                try:
                    if radio_select_targetpages == "current_page":
                        printer_range = [current_pageno]
                    elif radio_select_targetpages == "all":
                        printer_range = range(0, page_count)
                    elif radio_select_targetpages == "pair":
                        printer_range = [
                            x for x in range(0, page_count) if x % 2
                        ]
                        if input_select_targetpages:
                            printer_range = [
                                x for x in printer_range if x in
                                parse_printer_range(input_select_targetpages,
                                                    maximum=page_count + 1)
                            ]
                    elif radio_select_targetpages == "impair":
                        printer_range = [
                            x for x in range(0, page_count) if not x % 2
                        ]
                        if input_select_targetpages:
                            printer_range = [
                                x for x in printer_range if x in
                                parse_printer_range(input_select_targetpages,
                                                    maximum=page_count + 1)
                            ]
                    elif radio_select_targetpages == "range_only" and input_select_targetpages:
                        printer_range = parse_printer_range(
                            input_select_targetpages, maximum=page_count + 1)
                except ValueError as e:
                    _parser_error = True

                if _parser_error:
                    req.params["addpic2pdf_error"] = "%s: %s" % (
                        format_date().replace('T', ' - '),
                        t(lang(req),
                          "admin_wfstep_addpic2pdf_printer_range_error"))
                    del req.params['gotrue']
                    return self.show_workflow_node(node, req)

                printer_range = map(int, list(printer_range))

                if not printer_range:
                    req.params["addpic2pdf_error"] = "%s: %s" % (
                        format_date().replace('T', ' - '),
                        t(
                            lang(req),
                            "admin_wfstep_addpic2pdf_printer_range_selected_empty"
                        ))
                    del req.params['gotrue']
                    return self.show_workflow_node(node, req)

                x = x_cm * cm  # cm = 28.346456692913385
                y = y_cm * cm

                pic_dpi = get_pic_info(drag_logo_filepath).get('dpi', None)

                scale = 1.0

                if pic_dpi:
                    dpi_x, dpi_y = pic_dpi
                    if dpi_x != dpi_y:
                        req.params["addpic2pdf_error"] = "%s: %s" % (
                            format_date().replace('T', ' - '),
                            t(lang(req),
                              "admin_wfstep_addpic2pdf_logo_dpix_dpiy"))
                    dpi = int(dpi_x)
                    if dpi == 72:
                        scale = 1.0
                    else:
                        scale = 1.0 * 72.0 / dpi
                else:
                    dpi = 300
                    scale = 1.0 * 72.0 / dpi
                    #dpi = 72
                    #scale = 1.0

                tmppath = config.get("paths.datadir") + "tmp/"
                date_str = format_date().replace('T',
                                                 '-').replace(' ', '').replace(
                                                     ':', '-')
                filetempname = tmppath + \
                    "temp_addpic_pdf_wfs_%s_node_%s_%s_%s_.pdf" % (
                        unicode(current_workflow_step.id), unicode(node.id), date_str, unicode(random.random()))

                url = req.params.get('input_drag_logo_url', '')

                fn_out = filetempname

                build_logo_overlay_pdf(pdf_in_filepath,
                                       drag_logo_filepath,
                                       fn_out,
                                       x,
                                       y,
                                       scale=scale,
                                       mask='auto',
                                       pages=printer_range,
                                       follow_rotate=True,
                                       url=(" " * ADD_NBSP) + url)

                for f in node.files:
                    f_name = f.base_name
                    if f_name.startswith('addpic2pdf_%s_node_%s_' % (
                            unicode(current_workflow_step.id),
                            unicode(node.id),
                    )) and f.filetype.startswith('p_document'):
                        logg.info(
                            "workflow step addpic2pdf(%s): going to remove file '%s' from node '%s' (%s) for request from user '%s' (%s)",
                            current_workflow_step.id, f_name, node.name,
                            node.id, user.login_name, req.ip)
                        node.files.remove(f)
                        with suppress(Exception, warn=False):
                            os.remove(f.abspath)
                        break

                date_str = format_date().replace('T',
                                                 '-').replace(' ', '').replace(
                                                     ':', '-')
                nodeFile = importFileToRealname(
                    "_has_been_processed_%s.pdf" % (date_str),
                    filetempname,
                    prefix='addpic2pdf_%s_node_%s_' % (
                        unicode(current_workflow_step.id),
                        unicode(node.id),
                    ),
                    typeprefix="p_")
                node.files.append(nodeFile)
                db.session.commit()
                with suppress(Exception, warn=False):
                    os.remove(filetempname)
                del req.params['gotrue']
                return self.show_workflow_node(node, req)

        if "gofalse" in req.params:
            return self.forwardAndShow(node, False, req)

        # part of show_workflow_node not handled by "gotrue" and "gofalse"

        try:
            pdf_filepath = [
                f.abspath for f in node.files
                if f.filetype.startswith('document')
            ][0]
            error_no_pdf = False
        except:
            error_no_pdf = t(
                lang(req),
                "admin_wfstep_addpic2pdf_no_pdf_document_for_this_node")

        if not PYPDF_MODULE_PRESENT or error_no_pdf:
            error = ""
            if not PYPDF_MODULE_PRESENT:
                error += t(lang(req), "admin_wfstep_addpic2pdf_no_pypdf")
            if error_no_pdf:
                error += error_no_pdf
            pdf_dimensions = {
                'd_pageno2size': {
                    0: [595.275, 841.889]
                },
                'd_pageno2rotate': {
                    0: 0
                }
            }  # A4
            keep_params = copyDictValues(req.params, {}, KEEP_PARAMS)
            context = {
                "key": req.params.get("key", req.session.get("key", "")),
                "error": error,
                "node": node,
                "files": node.files,
                "wfs": current_workflow_step,
                "wfs_files": [],
                "logo_info": {},
                "logo_info_list": [],
                "getImageSize": lambda x: (0, 0),
                "pdf_page_count": 0,
                "pdf_dimensions": pdf_dimensions,
                "json_pdf_dimensions": json.dumps(pdf_dimensions),
                "keep_params": json.dumps(keep_params),
                "startpageno": 0,
                "FATAL_ERROR": 'true',
                "user": users.getUserFromRequest(req),
                "prefix": self.get("prefix"),
                "buttons": self.tableRowButtons(node),
                "csrf": req.csrf_token.current_token,
            }

            return req.getTAL("workflow/addpic2pdf.html",
                              context,
                              macro="workflow_addpic2pdf")
        try:
            pdf_dimensions = get_pdf_dimensions(pdf_filepath)
            pdf_pagecount = get_pdf_pagecount(pdf_filepath)
        except Exception as e:
            logg.exception("exception in workflow step addpic2pdf(%s)",
                           current_workflow_step.id)
            pdf_dimensions = {
                'd_pages': 0,
                'd_pageno2size': (0, 0),
                'd_pageno2rotate': 0
            }
            pdf_pagecount = 0
            FATAL_ERROR = True
            FATAL_ERROR_STR += " - %s" % (unicode(e))

        #wfs_files = [f for f in current_workflow_step.getFiles() if os.path.isfile(f.retrieveFile())]

        wfs_files0, wfs_files = getFilelist(current_workflow_step,
                                            'logoupload')

        url_mapping = [
            line.strip()
            for line in current_workflow_step.get("url_mapping").splitlines()
            if line.strip() and line.find("|") > 0
        ]
        url_mapping = dict(
            map(lambda x: (x[0].strip(), x[1].strip()),
                [line.split("|", 1) for line in url_mapping]))

        logo_info = {}
        logo_info_list = []
        for f in [
                f for f in wfs_files
                if f.base_name.startswith('m_upload_logoupload')
        ]:
            f_path = f.abspath

            try:
                _size = list(get_pic_size(f_path))
                _dpi = get_pic_dpi(f_path)
            except Exception as e:
                logg.exception("exception in workflow step addpic2pdf(%s)",
                               current_workflow_step.id)
                FATAL_ERROR = True
                FATAL_ERROR_STR += (" - ERROR loading logo '%s'" %
                                    f_path) + unicode(e)
                continue

            logo_filename = f.base_name

            logo_url = ""
            for key in url_mapping:
                if logo_filename.find(key) >= 0:
                    logo_url = url_mapping[key]
                    break

            logo_info[logo_filename.encode('utf-8')] = {
                'size': _size,
                'dpi': _dpi,
                'url': logo_url.encode('utf-8')
            }
            if _dpi == 'no-info':
                _dpi = 72.0
            logo_info_list.append({
                'size': _size,
                'dpi': _dpi,
                'url': logo_url.encode('utf-8')
            })

        if len(logo_info) == 0:
            logg.error(
                "workflow step addpic2pdf(%s): Error: no logo images found",
                current_workflow_step.id)
            FATAL_ERROR = True
            FATAL_ERROR_STR += " - Error: no logo images found"

        keep_params = copyDictValues(req.params, {}, KEEP_PARAMS)

        context = {
            "key": req.params.get("key", req.session.get("key", "")),
            "error": req.params.get('addpic2pdf_error', ''),
            "node": node,
            "files": node.files,
            "wfs": current_workflow_step,
            "wfs_files": wfs_files,
            "logo_info": logo_info,
            "logo_info_list": logo_info_list,
            "getImageSize": get_pic_size,
            "pdf_page_count": pdf_pagecount,
            "pdf_dimensions": pdf_dimensions,
            "json_pdf_dimensions": json.dumps(pdf_dimensions),
            "keep_params": json.dumps(keep_params),
            "startpageno": startpageno,
            "FATAL_ERROR": {
                False: 'false',
                True: 'true'
            }[bool(FATAL_ERROR)],
            "user": users.getUserFromRequest(req),
            "prefix": self.get("prefix"),
            "buttons": self.tableRowButtons(node),
            "csrf": req.csrf_token.current_token,
        }

        if FATAL_ERROR:
            context["error"] += " - %s" % (FATAL_ERROR_STR)

        return req.getTAL("workflow/addpic2pdf.html",
                          context,
                          macro="workflow_addpic2pdf")
예제 #33
0
def getContent(req, ids):
    ret = ""
    user = current_user
    node = q(Node).get(ids[0])
    update_error = False
    update_error_extension = False

    logg.debug(
        "%s|web.edit.modules.files.getContend|req.fullpath=%s|req.path=%s|req.params=%s|ids=%s",
        get_user_id(req), req.fullpath, req.path, req.params, ids)

    if not node.has_write_access(
    ) or "files" in current_user.hidden_edit_functions:
        req.setStatus(httpstatus.HTTP_FORBIDDEN)
        return req.getTAL("web/edit/edit.html", {}, macro="access_error")

    if 'data' in req.params:
        if 'data' in req.params:
            from contenttypes.container import Container
            if req.params.get(
                    'data'
            ) == 'children':  # get formated list of childnodes of selected directory
                excludeid = str(req.params.get('excludeid', None))
                if excludeid:
                    grandchildren = []

                    for child in node.getChildren():
                        for grandchild in child.children.all():
                            if not isinstance(grandchild, Container):
                                grandchildren.append(grandchild)
                    req.writeTAL("web/edit/modules/files.html", {
                        'children': [
                            c for c in node.children.all()
                            if str(c.id) != excludeid
                        ],
                        'grandchildren':
                        grandchildren,
                        "csrf":
                        req.csrf_token.current_token
                    },
                                 macro="edit_files_popup_children")
                else:
                    grandchildren = []
                    for child in node.children.all():
                        for grandchild in child.children.all():
                            if not isinstance(grandchild, Container):
                                grandchildren.append(grandchild)
                    req.writeTAL("web/edit/modules/files.html", {
                        'children': [
                            c for c in node.getChildren()
                            if str(c.id) != excludeid
                        ],
                        'grandchildren':
                        grandchildren,
                        "csrf":
                        req.csrf_token.current_token
                    },
                                 macro="edit_files_popup_children")
            elif req.params.get('data') == 'grandchildren':
                grandchildren = []
                for child in node.children.all():
                    if not isinstance(child, Container):
                        for grandchild in child.children.all():
                            if not isinstance(grandchild, Container):
                                grandchildren.append(grandchild)

                if len(node.getChildren()) == 0:
                    req.writeTAL("web/edit/modules/files.html", {
                        'grandchildren': [],
                        "csrf": req.csrf_token.current_token
                    },
                                 macro="edit_files_popup_grandchildren")
                else:
                    req.writeTAL("web/edit/modules/files.html", {
                        'grandchildren': grandchildren,
                        "csrf": req.csrf_token.current_token
                    },
                                 macro="edit_files_popup_grandchildren")

        if req.params.get(
                'data') == 'additems':  # add selected node as children
            for childid in req.params.get('items').split(";"):
                if childid.strip() != "":
                    childnode = q(Node).get(childid.strip())
                    # don't try to add node as child to itself !
                    if childnode != node:
                        for p in childnode.parents:
                            if isinstance(p, Container):
                                p.children.remove(childnode)
                        node.children.append(childnode)
            req.writeTAL("web/edit/modules/files.html", {
                'children': node.children,
                'node': node,
                "csrf": req.csrf_token.current_token
            },
                         macro="edit_files_children_list")

        if req.params.get(
                'data') == 'removeitem':  # remove selected childnode node
            with suppress(Exception):
                remnode = q(Node).get(req.params.get('remove'))
                if len(remnode.parents) == 1:
                    users.getUploadDir(user).children.append(remnode)
                node.children.remove(remnode)

            req.writeTAL("web/edit/modules/files.html", {
                'children': node.children,
                'node': node,
                "csrf": req.csrf_token.current_token
            },
                         macro="edit_files_children_list")

        if req.params.get('data') == 'reorder':
            i = 0
            for id in req.params.get('order').split(","):
                if id != "":
                    n = q(Node).get(id)
                    n.setOrderPos(i)
                    i += 1

        if req.params.get('data') == 'translate':
            req.writeTALstr(
                '<tal:block i18n:translate="" tal:content="msgstr"/>', {
                    'msgstr': req.params.get('msgstr'),
                    "csrf": req.csrf_token.current_token
                })

        db.session.commit()
        return ""

    if req.params.get("style") == "popup":
        v = {"basedirs": [q(Home).one(), q(Collections).one()]}
        id = req.params.get("id", q(Root).one().id)
        v["script"] = "var currentitem = '%s';\nvar currentfolder = '%s';\nvar node = %s;" % (
            id, req.params.get('parent'), id)
        v["idstr"] = ",".join(ids)
        v["node"] = node
        v["csrf"] = req.csrf_token.current_token
        req.writeTAL("web/edit/modules/files.html",
                     v,
                     macro="edit_files_popup_selection")
        return ""

    if "operation" in req.params:
        op = req.params.get("operation")
        if op == "delete":
            for key in req.params.keys():  # delete file
                if key.startswith("del|"):
                    filename = key[4:-2].split("|")
                    for file in node.files:
                        if file.base_name == filename[
                                1] and file.filetype == filename[0]:
                            # remove all files in directory
                            if file.mimetype == "inode/directory":
                                for root, dirs, files in os.walk(file.abspath):
                                    for name in files:
                                        try:
                                            os.remove(root + "/" + name)
                                        except:
                                            logg.exception(
                                                "exception while removing file, ignore"
                                            )
                                    os.removedirs(file.abspath + "/")
                            node.files.remove(file)
                            with suppress(Exception, warn=False):
                                os.remove(file.abspath)

                            break
                    break
                elif key.startswith("delatt|"):
                    for file in node.files:
                        if file.mimetype == "inode/directory":
                            try:
                                os.remove(file.abspath + "/" +
                                          key.split("|")[2][:-2])
                            except:
                                logg.exception(
                                    "exception while removing file, ignore")
                            break
                    break

        elif op == "change":
            _handle_change(node, req)
            if req.reply_code != httpstatus.HTTP_OK and req.reply_code != httpstatus.HTTP_MOVED_TEMPORARILY:
                if req.reply_code is httpstatus.HTTP_NOT_ACCEPTABLE:
                    update_error_extension = True
                else:
                    update_error = True

    db.session.commit()

    v = {
        "id": req.params.get("id", "0"),
        "tab": req.params.get("tab", ""),
        "node": node,
        "update_error": update_error,
        "update_error_extension": update_error_extension,
        "user": user,
        "files": filter(lambda x: x.type != 'statistic', node.files),
        "statfiles": filter(lambda x: x.type == 'statistic', node.files),
        "attfiles": filter(lambda x: x.type == 'attachment', node.files),
        "att": [],
        "nodes": [node],
        "csrf": req.csrf_token.current_token
    }

    for f in v["attfiles"]:  # collect all files in attachment directory
        if f.mimetype == "inode/directory":
            for root, dirs, files in os.walk(f.abspath):
                for name in files:
                    af = File(root + "/" + name, "attachmentfile",
                              getMimeType(name)[0])
                    v["att"].append(af)

    return req.getTAL("web/edit/modules/files.html",
                      v,
                      macro="edit_files_file")
예제 #34
0
def getContent(req, ids):
    node = q(Node).get(ids[0])
    if not node.has_write_access(
    ) or "admin" in current_user.hidden_edit_functions:
        req.setStatus(httpstatus.HTTP_FORBIDDEN)
        return req.getTAL("web/edit/edit.html", {}, macro="access_error")

    if req.params.get("type", "") == "addattr" and req.params.get(
            "new_name", "") != "" and req.params.get("new_value", "") != "":
        attrname = req.form.get("new_name")
        attrvalue = req.form.get("new_value")
        if attrname.startswith("system."):
            if current_user.is_admin:
                node.system_attrs[attrname[7:]] = attrvalue
            else:
                # non-admin user may not add / change system attributes, silently ignore the request.
                # XXX: an error msg would be better
                logg.warn(
                    "denied writing a system attribute because user is not an admin user, node=%s attrname=%s current_user=%s",
                    node.id, attrname, current_user.id)
                return httpstatus.HTTP_FORBIDDEN

        node.set(attrname, attrvalue)
        db.session.commit()
        logg.info("new attribute %s for node %s added",
                  req.params.get("new_name", ""), node.id)

    for key in req.params.keys():
        # update localread value of current node
        if key.startswith("del_localread"):
            node.resetLocalRead()
            logg.info("localread attribute of node %s updated", node.id)
            break

        # removing attributes only allowed for admin user

        # remove attribute
        if key.startswith("attr_"):
            if not current_user.is_admin:
                return httpstatus.HTTP_FORBIDDEN

            del node.attrs[key[5:-2]]
            db.session.commit()
            logg.info("attribute %s of node %s removed", key[5:-2], node.id)
            break

        # remove system attribute
        if key.startswith("system_attr_"):
            if not current_user.is_admin:
                return httpstatus.HTTP_FORBIDDEN

            attrname = key[12:-2]
            del node.system_attrs[attrname]
            db.session.commit()
            logg.info("system attribute %s of node %s removed", attrname,
                      node.id)
            break

    metadatatype = node.metadatatype
    fieldnames = []

    if metadatatype:
        fields = metadatatype.getMetaFields()
        for field in fields:
            fieldnames += [field.name]
    else:
        fields = []

    metafields = OrderedDict()
    technfields = OrderedDict()
    obsoletefields = OrderedDict()
    system_attrs = []

    tattr = {}
    with suppress(AttributeError, warn=False):
        tattr = node.getTechnAttributes()
    tattr = formatTechAttrs(tattr)

    for key, value in sorted(iteritems(node.attrs),
                             key=lambda t: t[0].lower()):
        if value or current_user.is_admin:
            # display all values for admins, even if they are "empty" (= a false value)
            if key in fieldnames:
                metafields[key] = formatdate(value, getFormat(fields, key))
            elif key in tattr.keys():
                technfields[key] = formatdate(value)
            else:
                obsoletefields[key] = value

    for key, value in sorted(iteritems(node.system_attrs),
                             key=lambda t: t[0].lower()):
        system_attrs.append((key, value))

    # remove all technical attributes
    if req.params.get("type", "") == "technical":
        for key in technfields:
            del node.attrs[key]
        technfields = {}
        logg.info("technical attributes of node %s removed", node.id)

    return req.getTAL("web/edit/modules/admin.html", {
        "id": req.params.get("id", "0"),
        "tab": req.params.get("tab", ""),
        "node": node,
        "obsoletefields": obsoletefields,
        "metafields": metafields,
        "system_attrs": system_attrs,
        "fields": fields,
        "technfields": technfields,
        "tattr": tattr,
        "fd": formatdate,
        "gf": getFormat,
        "user_is_admin": current_user.is_admin,
        "canedit": node.has_write_access(),
        "csrf": req.csrf_token.current_token
    },
                      macro="edit_admin_file")
예제 #35
0
파일: admin.py 프로젝트: mediatum/mediatum
def getContent(req, ids):
    node = q(Node).get(ids[0])
    if not node.has_write_access() or "admin" in current_user.hidden_edit_functions:
        req.setStatus(httpstatus.HTTP_FORBIDDEN)
        return req.getTAL("web/edit/edit.html", {}, macro="access_error")

    if req.params.get("type", "") == "addattr" and req.params.get("new_name", "") != "" and req.params.get("new_value", "") != "":
        attrname = req.form.get("new_name")
        attrvalue = req.form.get("new_value")
        if attrname.startswith("system."):
            if current_user.is_admin:
                node.system_attrs[attrname[7:]] = attrvalue
            else:
            # non-admin user may not add / change system attributes, silently ignore the request.
            # XXX: an error msg would be better
                logg.warn("denied writing a system attribute because user is not an admin user, node=%s attrname=%s current_user=%s",
                          node.id, attrname, current_user.id)
                return httpstatus.HTTP_FORBIDDEN

        node.set(attrname, attrvalue)
        db.session.commit()
        logg.info("new attribute %s for node %s added", req.params.get("new_name", ""), node.id)

    for key in req.params.keys():
        # update localread value of current node
        if key.startswith("del_localread"):
            node.resetLocalRead()
            logg.info("localread attribute of node %s updated", node.id)
            break

        # removing attributes only allowed for admin user

        # remove attribute
        if key.startswith("attr_"):
            if not current_user.is_admin:
                return httpstatus.HTTP_FORBIDDEN

            del node.attrs[key[5:-2]]
            db.session.commit()
            logg.info("attribute %s of node %s removed", key[5:-2], node.id)
            break

        # remove system attribute
        if key.startswith("system_attr_"):
            if not current_user.is_admin:
                return httpstatus.HTTP_FORBIDDEN

            attrname = key[12:-2]
            del node.system_attrs[attrname]
            db.session.commit()
            logg.info("system attribute %s of node %s removed", attrname, node.id)
            break

    metadatatype = node.metadatatype
    fieldnames = []

    if metadatatype:
        fields = metadatatype.getMetaFields()
        for field in fields:
            fieldnames += [field.name]
    else:
        fields = []

    metafields = OrderedDict()
    technfields = OrderedDict()
    obsoletefields = OrderedDict()
    system_attrs = []

    tattr = {}
    with suppress(AttributeError, warn=False):
        tattr = node.getTechnAttributes()
    tattr = formatTechAttrs(tattr)

    for key, value in sorted(iteritems(node.attrs), key=lambda t: t[0].lower()):
        if value or current_user.is_admin:
            # display all values for admins, even if they are "empty" (= a false value)
            if key in fieldnames:
                metafields[key] = formatdate(value, getFormat(fields, key))
            elif key in tattr.keys():
                technfields[key] = formatdate(value)
            else:
                obsoletefields[key] = value

    for key, value in sorted(iteritems(node.system_attrs), key=lambda t: t[0].lower()):
        system_attrs.append((key, value))

    # remove all technical attributes
    if req.params.get("type", "") == "technical":
        for key in technfields:
            del node.attrs[key]
        technfields = {}
        logg.info("technical attributes of node %s removed", node.id)

    return req.getTAL("web/edit/modules/admin.html", {"id": req.params.get("id", "0"),
                                                      "tab": req.params.get("tab", ""),
                                                      "node": node,
                                                      "obsoletefields": obsoletefields,
                                                      "metafields": metafields,
                                                      "system_attrs": system_attrs,
                                                      "fields": fields,
                                                      "technfields": technfields,
                                                      "tattr": tattr,
                                                      "fd": formatdate,
                                                      "gf": getFormat,
                                                      "user_is_admin": current_user.is_admin,
                                                      "canedit": node.has_write_access(),
                                                      "csrf": req.csrf_token.current_token},
                      macro="edit_admin_file")
예제 #36
0
    def show_workflow_node(self, node, req, data=None):

        check_context()

        user = users.getUserFromRequest(req)

        current_workflow = getNodeWorkflow(node)
        current_workflow_step = getNodeWorkflowStep(node)

        FATAL_ERROR = False
        FATAL_ERROR_STR = ""

        if "gotrue" in req.params:

            if not PYPDF_MODULE_PRESENT:
                del req.params['gotrue']
                return self.show_workflow_node(node, req)

            radio_apply_reset_accept = req.params.get('radio_apply_reset_accept', '')

            if radio_apply_reset_accept == 'reset':
                for f in node.files:
                    f_name = f.base_name
                    if f_name.startswith('addpic2pdf_%s_node_%s_' %
                                         (unicode(current_workflow_step.id), unicode(node.id))) and f.filetype.startswith('p_document'):
                        logg.info("workflow step addpic2pdf(%s): going to remove file '%s' from node '%s' (%s) for request from user '%s' (%s)",
                            current_workflow_step.id, f_name, node.name, node.id, user.login_name, req.ip)
                        node.files.remove(f)
                        db.session.commit()
                        try:
                            os.remove(f.abspath)
                        except:
                            logg.exception("exception in workflow setep addpic2pdf, removing file failed, ignoring")

                del req.params['gotrue']
                return self.show_workflow_node(node, req)

            elif radio_apply_reset_accept == 'accept':

                p_document_files = [f for f in node.files if f.filetype == 'p_document' and f.base_name.startswith(
                    'addpic2pdf_%s_node_%s_' % (unicode(current_workflow_step.id), unicode(node.id)))]

                if len(p_document_files) > 0:

                    p_document_file = p_document_files[0]

                    document_file = [f for f in node.files if f.filetype == 'document'][0]

                    o_document_file = File(document_file.path, 'o_document', document_file.mimetype)

                    node.files.remove(document_file)
                    node.files.append(o_document_file)
                    o_document_name = o_document_file.base_name

                    for f in node.files:
                        if f.filetype in ['thumb', 'fileinfo', 'fulltext'] or f.filetype.startswith('present'):
                            if os.path.splitext(f.base_name)[0] == os.path.splitext(o_document_name)[0]:
                                new_f = File(f.path, 'o_' + f.filetype, f.mimetype)
                                node.files.remove(f)
                                node.files.append(new_f)

                    new_document_file = File(p_document_file.path, 'document', p_document_file.mimetype)
                    node.files.remove(p_document_file)
                    node.files.append(new_document_file)
                    db.session.commit()
                    node.event_files_changed()

                del req.params['gotrue']
                return self.forwardAndShow(node, True, req)

            elif radio_apply_reset_accept == 'apply':

                drag_logo_fullname = req.params.get("input_drag_logo_fullname", None)

                if not drag_logo_fullname:
                    req.params["addpic2pdf_error"] = "%s: %s" % (
                        format_date().replace('T', ' - '), t(lang(req), "admin_wfstep_addpic2pdf_no_logo_selected"))
                    del req.params['gotrue']
                    return self.show_workflow_node(node, req)

                drag_logo_filepath = [f.abspath for f in current_workflow_step.files if f.base_name == drag_logo_fullname][0]

                pos_cm = req.params.get("input_poffset_cm", "0, 0")
                x_cm, y_cm = [float(x.strip()) for x in pos_cm.split(",")]

                pdf_in_filepath = getPdfFilepathForProcessing(current_workflow_step, node)

                current_pageno = int(req.params.get("input_current_page", "0").strip())

                radio_select_targetpages = req.params.get("radio_select_targetpages", "").strip()
                input_select_targetpages = req.params.get("input_select_targetpages", "").strip()

                printer_range = []
                page_count = get_pdf_pagecount(pdf_in_filepath)
                _parser_error = False

                try:
                    if radio_select_targetpages == "current_page":
                        printer_range = [current_pageno]
                    elif radio_select_targetpages == "all":
                        printer_range = range(0, page_count)
                    elif radio_select_targetpages == "pair":
                        printer_range = [x for x in range(0, page_count) if x % 2]
                        if input_select_targetpages:
                            printer_range = [x for x in printer_range if x in parse_printer_range(
                                input_select_targetpages, maximum=page_count + 1)]
                    elif radio_select_targetpages == "impair":
                        printer_range = [x for x in range(0, page_count) if not x % 2]
                        if input_select_targetpages:
                            printer_range = [x for x in printer_range if x in parse_printer_range(
                                input_select_targetpages, maximum=page_count + 1)]
                    elif radio_select_targetpages == "range_only" and input_select_targetpages:
                        printer_range = parse_printer_range(input_select_targetpages, maximum=page_count + 1)
                except ValueError as e:
                    _parser_error = True

                if _parser_error:
                    req.params["addpic2pdf_error"] = "%s: %s" % (
                        format_date().replace('T', ' - '), t(lang(req), "admin_wfstep_addpic2pdf_printer_range_error"))
                    del req.params['gotrue']
                    return self.show_workflow_node(node, req)

                printer_range = map(int, list(printer_range))

                if not printer_range:
                    req.params["addpic2pdf_error"] = "%s: %s" % (
                        format_date().replace('T', ' - '), t(lang(req), "admin_wfstep_addpic2pdf_printer_range_selected_empty"))
                    del req.params['gotrue']
                    return self.show_workflow_node(node, req)

                x = x_cm * cm  # cm = 28.346456692913385
                y = y_cm * cm

                pic_dpi = get_pic_info(drag_logo_filepath).get('dpi', None)

                scale = 1.0

                if pic_dpi:
                    dpi_x, dpi_y = pic_dpi
                    if dpi_x != dpi_y:
                        req.params["addpic2pdf_error"] = "%s: %s" % (
                            format_date().replace('T', ' - '), t(lang(req), "admin_wfstep_addpic2pdf_logo_dpix_dpiy"))
                    dpi = int(dpi_x)
                    if dpi == 72:
                        scale = 1.0
                    else:
                        scale = 1.0 * 72.0 / dpi
                else:
                    dpi = 300
                    scale = 1.0 * 72.0 / dpi
                    #dpi = 72
                    #scale = 1.0

                tmppath = config.get("paths.datadir") + "tmp/"
                date_str = format_date().replace('T', '-').replace(' ', '').replace(':', '-')
                filetempname = tmppath + \
                    "temp_addpic_pdf_wfs_%s_node_%s_%s_%s_.pdf" % (
                        unicode(current_workflow_step.id), unicode(node.id), date_str, unicode(random.random()))

                url = req.params.get('input_drag_logo_url', '')

                fn_out = filetempname

                build_logo_overlay_pdf(pdf_in_filepath, drag_logo_filepath, fn_out, x, y, scale=scale,
                                       mask='auto', pages=printer_range, follow_rotate=True, url=(" " * ADD_NBSP) + url)

                for f in node.files:
                    f_name = f.base_name
                    if f_name.startswith('addpic2pdf_%s_node_%s_' %
                                         (unicode(current_workflow_step.id), unicode(node.id), )) and f.filetype.startswith('p_document'):
                        logg.info("workflow step addpic2pdf(%s): going to remove file '%s' from node '%s' (%s) for request from user '%s' (%s)",
                            current_workflow_step.id, f_name, node.name, node.id, user.login_name, req.ip)
                        node.files.remove(f)
                        with suppress(Exception, warn=False):
                            os.remove(f.abspath)
                        break

                date_str = format_date().replace('T', '-').replace(' ', '').replace(':', '-')
                nodeFile = importFileToRealname("_has_been_processed_%s.pdf" % (date_str), filetempname, prefix='addpic2pdf_%s_node_%s_' % (
                    unicode(current_workflow_step.id), unicode(node.id), ), typeprefix="p_")
                node.files.append(nodeFile)
                db.session.commit()
                with suppress(Exception, warn=False):
                    os.remove(filetempname)
                del req.params['gotrue']
                return self.show_workflow_node(node, req)

        if "gofalse" in req.params:
            return self.forwardAndShow(node, False, req)

        # part of show_workflow_node not handled by "gotrue" and "gofalse"

        try:
            pdf_filepath = [f.abspath for f in node.files if f.filetype.startswith('document')][0]
            error_no_pdf = False
        except:
            error_no_pdf = t(lang(req), "admin_wfstep_addpic2pdf_no_pdf_document_for_this_node")

        if not PYPDF_MODULE_PRESENT or error_no_pdf:
            error = ""
            if not PYPDF_MODULE_PRESENT:
                error += t(lang(req), "admin_wfstep_addpic2pdf_no_pypdf")
            if error_no_pdf:
                error += error_no_pdf
            pdf_dimensions = {'d_pageno2size': {0: [595.275, 841.889]}, 'd_pageno2rotate': {0: 0}}  # A4
            keep_params = copyDictValues(req.params, {}, KEEP_PARAMS)
            context = {"key": req.params.get("key", req.session.get("key", "")),
                       "error": error,

                       "node": node,
                       "files": node.files,
                       "wfs": current_workflow_step,
                       "wfs_files": [],

                       "logo_info": {},
                       "logo_info_list": [],

                       "getImageSize": lambda x: (0, 0),
                       "pdf_page_count": 0,
                       "pdf_dimensions": pdf_dimensions,
                       "json_pdf_dimensions": json.dumps(pdf_dimensions),
                       "keep_params": json.dumps(keep_params),
                       "startpageno": 0,

                       "FATAL_ERROR": 'true',

                       "user": users.getUserFromRequest(req),
                       "prefix": self.get("prefix"),
                       "buttons": self.tableRowButtons(node),
                       "csrf": req.csrf_token.current_token,}

            return req.getTAL("workflow/addpic2pdf.html", context, macro="workflow_addpic2pdf")
        try:
            pdf_dimensions = get_pdf_dimensions(pdf_filepath)
            pdf_pagecount = get_pdf_pagecount(pdf_filepath)
        except Exception as e:
            logg.exception("exception in workflow step addpic2pdf(%s)", current_workflow_step.id)
            pdf_dimensions = {'d_pages': 0, 'd_pageno2size': (0, 0), 'd_pageno2rotate': 0}
            pdf_pagecount = 0
            FATAL_ERROR = True
            FATAL_ERROR_STR += " - %s" % (unicode(e))

        #wfs_files = [f for f in current_workflow_step.getFiles() if os.path.isfile(f.retrieveFile())]

        wfs_files0, wfs_files = getFilelist(current_workflow_step, 'logoupload')

        url_mapping = [line.strip()
                       for line in current_workflow_step.get("url_mapping").splitlines() if line.strip() and line.find("|") > 0]
        url_mapping = dict(map(lambda x: (x[0].strip(), x[1].strip()), [line.split("|", 1) for line in url_mapping]))

        logo_info = {}
        logo_info_list = []
        for f in [f for f in wfs_files if f.base_name.startswith('m_upload_logoupload')]:
            f_path = f.abspath

            try:
                _size = list(get_pic_size(f_path))
                _dpi = get_pic_dpi(f_path)
            except Exception as e:
                logg.exception("exception in workflow step addpic2pdf(%s)", current_workflow_step.id)
                FATAL_ERROR = True
                FATAL_ERROR_STR += (" - ERROR loading logo '%s'" % f_path) + unicode(e)
                continue

            logo_filename = f.base_name

            logo_url = ""
            for key in url_mapping:
                if logo_filename.find(key) >= 0:
                    logo_url = url_mapping[key]
                    break

            logo_info[logo_filename.encode('utf-8')] = {'size': _size, 'dpi': _dpi, 'url': logo_url.encode('utf-8')}
            if _dpi == 'no-info':
                _dpi = 72.0
            logo_info_list.append({'size': _size, 'dpi': _dpi, 'url': logo_url.encode('utf-8')})

        if len(logo_info) == 0:
            logg.error("workflow step addpic2pdf(%s): Error: no logo images found", current_workflow_step.id)
            FATAL_ERROR = True
            FATAL_ERROR_STR += " - Error: no logo images found"

        keep_params = copyDictValues(req.params, {}, KEEP_PARAMS)

        context = {"key": req.params.get("key", req.session.get("key", "")),
                   "error": req.params.get('addpic2pdf_error', ''),

                   "node": node,
                   "files": node.files,
                   "wfs": current_workflow_step,
                   "wfs_files": wfs_files,

                   "logo_info": logo_info,
                   "logo_info_list": logo_info_list,

                   "getImageSize": get_pic_size,
                   "pdf_page_count": pdf_pagecount,
                   "pdf_dimensions": pdf_dimensions,
                   "json_pdf_dimensions": json.dumps(pdf_dimensions),
                   "keep_params": json.dumps(keep_params),
                   "startpageno": startpageno,

                   "FATAL_ERROR": {False: 'false', True: 'true'}[bool(FATAL_ERROR)],

                   "user": users.getUserFromRequest(req),
                   "prefix": self.get("prefix"),
                   "buttons": self.tableRowButtons(node),
                   "csrf": req.csrf_token.current_token,}

        if FATAL_ERROR:
            context["error"] += " - %s" % (FATAL_ERROR_STR)

        return req.getTAL("workflow/addpic2pdf.html", context, macro="workflow_addpic2pdf")
예제 #37
0
def getContent(req, ids):
    def getSchemes(req):
        schemes = AccessData(req).filter(loadTypesFromDB())
        return filter(lambda x: x.isActive(), schemes)

    ret = ""
    v = {"message": ""}

    if len(ids) >= 0:
        ids = ids[0]

    v["id"] = ids

    if "do_action" in req.params.keys():  # process nodes
        fieldname = req.params.get("fields")
        old_values = u(req.params.get("old_values", "")).split(";")
        new_value = u(req.params.get("new_value"))
        basenode = q(Node).get(ids)
        entries = getAllAttributeValues(fieldname, req.params.get("schema"))

        c = 0
        for old_val in old_values:
            for n in AccessData(req).filter(
                    q(Node).filter(Node.id.in_(entries[old_val])).all()):
                with suppress(Exception, warn=False):
                    n.set(
                        fieldname,
                        replaceValue(n.get(fieldname), u(old_val),
                                     u(new_value)))
                    c += 1
        v["message"] = req.getTAL("web/edit/modules/manageindex.html",
                                  {"number": c},
                                  macro="operationinfo")

    if "style" in req.params.keys():  # load schemes
        if req.params.get("action", "") == "schemes":
            v["schemes"] = getSchemes(req)
            req.writeTAL("web/edit/modules/manageindex.html",
                         v,
                         macro="schemes_dropdown")
            return ""

        elif req.params.get(
                "action", "").startswith("indexfields__"):  # load index fields
            schema = getMetaType(req.params.get("action", "")[13:])
            fields = []
            for field in schema.getMetaFields():
                if field.getFieldtype() == "ilist":
                    fields.append(field)
            v["fields"] = fields
            v["schemaname"] = schema.getName()
            req.writeTAL("web/edit/modules/manageindex.html",
                         v,
                         macro="fields_dropdown")
            return ""

        elif req.params.get("action", "").startswith(
                "indexvalues__"):  # load values of selected indexfield
            node = q(Node).get(ids)
            fieldname = req.params.get("action").split("__")[-2]
            schema = req.params.get("action").split("__")[-1]
            v["entries"] = []
            if node:
                v["entries"] = getAllAttributeValues(fieldname, schema)
                v["keys"] = v["entries"].keys()
                v["keys"].sort(lambda x, y: cmp(x.lower(), y.lower()))
            req.writeTAL("web/edit/modules/manageindex.html",
                         v,
                         macro="fieldvalues")
            return ""

        elif req.params.get("action", "").startswith(
                "children__"):  # search for children of current collection
            scheme = req.params.get("action", "").split("__")[1]
            fieldname = req.params.get("action", "").split("__")[2]
            values = req.params.get("action",
                                    "").split("__")[3].split(";")[:-1]
            all_values = getAllAttributeValues(fieldname, scheme)

            def isChildOf(access, node, basenodeid):
                for ls in getPaths(node):
                    if basenodeid in [unicode(n.id) for n in ls]:
                        return 1
                return 0

            subitems = {}
            for value in values:
                value = u(value)
                if value in all_values:
                    subitems[value] = []
                    for l in all_values[value]:
                        if isChildOf(AccessData(req), q(Node).get(l), ids):
                            subitems[value].append(l)

            v["items"] = subitems
            v["keys"] = subitems.keys()
            v["keys"].sort()
            req.writeTAL("web/edit/modules/manageindex.html",
                         v,
                         macro="valueinfo")
            return ""

    else:
        v["csrf"] = req.csrf_token.current_token
        return req.getTAL("web/edit/modules/manageindex.html",
                          v,
                          macro="manageform")
예제 #38
0
파일: app.py 프로젝트: mediatum/mediatum
def detect_athana_or_flask():
    with suppress(Exception, warn=False):
        from flask import current_app
        if current_app:
            return "flask"
    return "athana"
예제 #39
0
파일: edit.py 프로젝트: mediatum/mediatum
def content(req):

    user = current_user
    language = lang(req)

    if not user.is_editor:
        return req.writeTAL("web/edit/edit.html", {}, macro="error")

    if 'id' in req.params and len(req.params) == 1:
        nid = long(req.params.get('id'))
        node = q(Data).get(nid)

        if node is not None:
            cmd = "cd (%s %r, %r)" % (nid, node.name, node.type)
            logg.info("%s: %s", user.login_name, cmd)
        else:
            cmd = "ERROR-cd to non-existing id=%r" % nid
            logg.error("%s: %s", user.login_name, cmd)

    if 'action' in req.params and req.params['action'] == 'upload':
        pass

    content = {'script': '', 'body': ''}
    v = {'dircontent': '', 'notdirectory': 0, 'operations': ''}
    try:
        v['nodeiconpath'] = getEditorIconPath(node)
    except:
        v['nodeiconpath'] = "webtree/directory.gif"

    path = req.path[1:].split("/")
    if len(path) >= 4:
        req.params["style"] = "popup"
        req.params["id"] = path[1]
        req.params["tab"] = path[2]
        req.params["option"] = path[3]
    getEditModules()

    if not user.is_editor:
        return req.writeTAL("web/edit/edit.html", {}, macro="error")

    # remove all caches for the frontend area- we might make changes there
    for sessionkey in ["contentarea", "navframe"]:
        with suppress(Exception, warn=False):
            del req.session[sessionkey]

    ids = getIDs(req)

    if req.params.get("type", "") == "help" and req.params.get("tab", "") == "upload":
        return upload_help(req)

    if len(ids) > 0:
        if ids[0] == "all":
            ids = get_ids_from_req(req)
        node = q(Node).get(long(ids[0]))
    tabs = "content"
    if isinstance(node, Root):
        tabs = "content"
    elif node is user.upload_dir:
        tabs = "upload"
    else:
        tabs = node.get_default_edit_tab()
        v["notdirectory"] = 0

    current = req.params.get("tab", tabs)
    logg.debug("... %s inside %s.%s: ->  !!! current = %s !!!", get_user_id(req), __name__, funcname(), current)
    msg = "%s selected editor module is %s" % (user.login_name, current)
    jsfunc = req.params.get("func", "")
    if jsfunc:
        msg = msg + (', js-function: %r' % jsfunc)
    logg.info(msg)

    # some tabs operate on only one file
    # if current in ["files", "view", "upload"]:
    if current in ["files", "upload"]:
        ids = ids[0:1]

    # display current images
    if not isinstance(q(Data).get(ids[0]), Container):
        v["notdirectory"] = 1
        items = []
        if current != "view":
            for id in ids:
                node = q(Data).get(id)
                if hasattr(node, "show_node_image"):
                    if not isDirectory(node) and not node.isContainer():
                        items.append((id, node.show_node_image()))
                    else:
                        items.append(("", node.show_node_image()))
        v["items"] = items
        if logg.isEnabledFor(logging.DEBUG):
            logg.debug("... %s inside %s.%s: -> display current images: items: %s",
                       get_user_id(req), __name__, funcname(), [_t[0] for _t in items])

        nid = req.params.get('src', req.params.get('id'))
        if nid is None:
            raise ValueError("invalid request, neither 'src' not 'id' parameter is set!")

        folders_only = False
        if nid.find(',') > 0:
            # more than one node selected
            # use the first one for activateEditorTreeNode
            # and display only folders
            nid = nid.split(',')[0]
            folders_only = True
        n = q(Data).get(nid)
        if current == 'metadata' and 'save' in req.params:
            pass
        s = []
        while n:
            if not folders_only:
                s = ['<a onClick="activateEditorTreeNode(%r); return false;" href="/edit/edit_content?id=%s">%s</a>' %
                     (n.id, n.id, get_edit_label(n, language))] + s

            folders_only = False;
            p = n.parents
            # XXX: we only check the first parent. This is wrong, how could be solve this? #
            first_parent = p[0]
            if isinstance(first_parent, Data) and first_parent.has_read_access():
                n = p[0]
            else:
                n = None
        v["dircontent"] = ' <b>&raquo;</b> '.join(s)

    else:  # or current directory
        n = q(Data).get(long(ids[0]))
        s = []
        while n:
            if len(s) == 0:
                s = ['%s' % (get_edit_label(n, language))]
            else:
                s = ['<a onClick="activateEditorTreeNode(%r); return false;" href="/edit/edit_content?id=%s">%s</a>' %
                     (n.id, n.id, get_edit_label(n, language))] + s

            p = n.parents
            if p and not isinstance(p[0], Root):
                n = p[0]
            else:
                n = None
        v["dircontent"] = ' <b>&raquo;</b> '.join(s)

    if current == "globals":
        basedir = config.get("paths.datadir")
        file_to_edit = None

        if "file_to_edit" in req.params:
            file_to_edit = req.params["file_to_edit"]

        if not file_to_edit:
            # todo: getstartpagedict doesnt exist
            d = node.getStartpageDict()
            if d and language in d:
                file_to_edit = d[language]

        found = False
        for f in node.files:
            if f.mimetype == 'text/html':
                filepath = f.abspath.replace(basedir, '')
                if file_to_edit == filepath:
                    found = True
                    break

    else:
        t2 = current.split("_")[-1]
        if t2 in editModules.keys():
            c = editModules[t2].getContent(req, ids)

            if isinstance(c, int):
                # module returned a custom http status code instead of HTML content
                return c

            elif c:
                content["body"] += c
            else:
                logg.debug('empty content')
                return
        else:
            req.setStatus(httpstatus.HTTP_INTERNAL_SERVER_ERROR)
            content["body"] += req.getTAL("web/edit/edit.html", {"module": current}, macro="module_error")

    if req.params.get("style", "") != "popup":  # normal page with header
        v["tabs"] = handletabs(req, ids, tabs)
        v["script"] = content["script"]
        v["body"] = content["body"]
        v["paging"] = showPaging(req, current, ids)
        v["node"] = node
        v["ids"] = req.params.get("ids", "").split(",")
        if req.params.get("ids", "") == "":
            v["ids"] = req.params.get("id", "").split(",")
        v["tab"] = current
        v["operations"] = req.getTAL("web/edit/edit_common.html", {'iscontainer': node.isContainer()}, macro="show_operations")
        v['user'] = user
        v['language'] = lang(req)
        v['t'] = t

        # add icons to breadcrumbs
        ipath = 'webtree/directory.gif'
        if node and node.isContainer():
            if node.name == 'home' or 'Arbeitsverzeichnis' in node.name or node == current_user.home_dir:
                ipath = 'webtree/homeicon.gif'
            elif node.name in ('Uploads', 'upload'):
                ipath = 'webtree/uploadicon.gif'
            elif node.name in ('Papierkorb', 'trash'):
                ipath = 'webtree/trashicon.gif'
            else:
                ipath = getEditorIconPath(node)

        v["dircontent"] += '&nbsp;&nbsp;<img src="' + '/img/' + ipath + '" />'

        return req.writeTAL("web/edit/edit.html", v, macro="frame_content")
예제 #40
0
파일: files.py 프로젝트: mediatum/mediatum
def getContent(req, ids):
    ret = ""
    user = current_user
    node = q(Node).get(ids[0])
    update_error = False
    update_error_extension = False

    logg.debug("%s|web.edit.modules.files.getContend|req.fullpath=%s|req.path=%s|req.params=%s|ids=%s",
               get_user_id(req), req.fullpath, req.path, req.params, ids)

    if not node.has_write_access() or "files" in current_user.hidden_edit_functions:
        req.setStatus(httpstatus.HTTP_FORBIDDEN)
        return req.getTAL("web/edit/edit.html", {}, macro="access_error")

    if 'data' in req.params:
        if 'data' in req.params:
            from contenttypes.container import Container
            if req.params.get('data') == 'children':  # get formated list of childnodes of selected directory
                excludeid = str(req.params.get('excludeid', None))
                if excludeid:
                    grandchildren = []

                    for child in node.getChildren():
                        for grandchild in child.children.all():
                            if not isinstance(grandchild, Container):
                                grandchildren.append(grandchild)
                    req.writeTAL("web/edit/modules/files.html", {'children': [c for c in node.children.all() if str(c.id) != excludeid],
                                                                 'grandchildren': grandchildren, "csrf": req.csrf_token.current_token}, macro="edit_files_popup_children")
                else:
                    grandchildren = []
                    for child in node.children.all():
                        for grandchild in child.children.all():
                            if not isinstance(grandchild, Container):
                                grandchildren.append(grandchild)
                    req.writeTAL("web/edit/modules/files.html", {'children': [c for c in node.getChildren() if str(c.id) != excludeid],
                                                                 'grandchildren': grandchildren, "csrf": req.csrf_token.current_token}, macro="edit_files_popup_children")
            elif req.params.get('data') =='grandchildren':
                grandchildren = []
                for child in node.children.all():
                    if not isinstance(child, Container):
                        for grandchild in child.children.all():
                            if not isinstance(grandchild, Container):
                                    grandchildren.append(grandchild)

                if len(node.getChildren())==0:
                    req.writeTAL("web/edit/modules/files.html", {'grandchildren': [], "csrf": req.csrf_token.current_token}, macro="edit_files_popup_grandchildren")
                else:
                    req.writeTAL("web/edit/modules/files.html", {'grandchildren': grandchildren, "csrf": req.csrf_token.current_token}, macro="edit_files_popup_grandchildren")

        if req.params.get('data') == 'additems':  # add selected node as children
            for childid in req.params.get('items').split(";"):
                if childid.strip() != "":
                    childnode = q(Node).get(childid.strip())
                    # don't try to add node as child to itself !
                    if childnode != node:
                        for p in childnode.parents:
                            if isinstance(p, Container):
                                p.children.remove(childnode)
                        node.children.append(childnode)
            req.writeTAL("web/edit/modules/files.html", {'children': node.children, 'node': node, "csrf": req.csrf_token.current_token}, macro="edit_files_children_list")

        if req.params.get('data') == 'removeitem':  # remove selected childnode node
            with suppress(Exception):
                remnode = q(Node).get(req.params.get('remove'))
                if len(remnode.parents) == 1:
                    users.getUploadDir(user).children.append(remnode)
                node.children.remove(remnode)

            req.writeTAL("web/edit/modules/files.html", {'children': node.children, 'node': node, "csrf": req.csrf_token.current_token}, macro="edit_files_children_list")

        if req.params.get('data') == 'reorder':
            i = 0
            for id in req.params.get('order').split(","):
                if id != "":
                    n = q(Node).get(id)
                    n.setOrderPos(i)
                    i += 1

        if req.params.get('data') == 'translate':
            req.writeTALstr('<tal:block i18n:translate="" tal:content="msgstr"/>', {'msgstr': req.params.get('msgstr'), "csrf": req.csrf_token.current_token})

        db.session.commit()
        return ""

    if req.params.get("style") == "popup":
        v = {"basedirs": [q(Home).one(), q(Collections).one()]}
        id = req.params.get("id", q(Root).one().id)
        v["script"] = "var currentitem = '%s';\nvar currentfolder = '%s';\nvar node = %s;" % (id, req.params.get('parent'), id)
        v["idstr"] = ",".join(ids)
        v["node"] = node
        v["csrf"] = req.csrf_token.current_token
        req.writeTAL("web/edit/modules/files.html", v, macro="edit_files_popup_selection")
        return ""

    if "operation" in req.params:
        op = req.params.get("operation")
        if op == "delete":
            for key in req.params.keys():  # delete file
                if key.startswith("del|"):
                    filename = key[4:-2].split("|")
                    for file in node.files:
                        if file.base_name == filename[1] and file.filetype == filename[0]:
                            # remove all files in directory
                            if file.mimetype == "inode/directory":
                                for root, dirs, files in os.walk(file.abspath):
                                    for name in files:
                                        try:
                                            os.remove(root + "/" + name)
                                        except:
                                            logg.exception("exception while removing file, ignore")
                                    os.removedirs(file.abspath + "/")
                            node.files.remove(file)
                            with suppress(Exception, warn=False):
                                os.remove(file.abspath)

                            break
                    break
                elif key.startswith("delatt|"):
                    for file in node.files:
                        if file.mimetype == "inode/directory":
                            try:
                                os.remove(file.abspath + "/" + key.split("|")[2][:-2])
                            except:
                                logg.exception("exception while removing file, ignore")
                            break
                    break

        elif op == "change":
            _handle_change(node, req)
            if req.reply_code != httpstatus.HTTP_OK and req.reply_code != httpstatus.HTTP_MOVED_TEMPORARILY:
                if req.reply_code is httpstatus.HTTP_NOT_ACCEPTABLE:
                    update_error_extension = True
                else:
                    update_error = True

        elif op == "addthumb":  # create new thumbanil from uploaded file
            uploadfile = req.params.get("updatefile")

            if uploadfile:
                thumbname = os.path.join(getImportDir(), hashlib.md5(ustr(random.random())).hexdigest()[0:8]) + ".thumb"

                file = importFile(thumbname, uploadfile.tempname)  # add new file
                make_thumbnail_image(file.abspath, thumbname)
                make_presentation_image(file.abspath, thumbname + "2")

                if os.path.exists(file.abspath):  # remove uploaded original
                    os.remove(file.abspath)

                for f in node.files:
                    if f.type in ["thumb", "presentation"]:
                        if os.path.exists(f.abspath):
                            os.remove(f.abspath)
                        node.files.remove(f)

                node.files.append(File(thumbname, "thumb", "image/jpeg"))
                node.files.append(File(thumbname + "2", "presentation", "image/jpeg"))
                logg.info("%s changed thumbnail of node %s", user.login_name, node.id)

        elif op == "postprocess":
                try:
                    node.event_files_changed()
                    logg.info("%s postprocesses node %s", user.login_name, node.id)
                except:
                    update_error = True

    db.session.commit()

    v = {"id": req.params.get("id", "0"),
         "tab": req.params.get("tab", ""),
         "node": node,
         "update_error": update_error,
         "update_error_extension": update_error_extension,
         "user": user,
         "files": filter(lambda x: x.type != 'statistic', node.files),
         "statfiles": filter(lambda x: x.type == 'statistic', node.files),
         "attfiles": filter(lambda x: x.type == 'attachment', node.files),
         "att": [],
         "nodes": [node],
         "csrf": req.csrf_token.current_token
        }

    for f in v["attfiles"]:  # collect all files in attachment directory
        if f.mimetype == "inode/directory":
            for root, dirs, files in os.walk(f.abspath):
                for name in files:
                    af = File(root + "/" + name, "attachmentfile", getMimeType(name)[0])
                    v["att"].append(af)

    return req.getTAL("web/edit/modules/files.html", v, macro="edit_files_file")