def getIDs(req): # update nodelist, if necessary if "nodelist" in req.params: nodelist = [] for id in req.params.get("nodelist").split(","): nodelist.append(tree.getNode(id)) req.session["nodelist"] = EditorNodeList(nodelist) # look for one "id" parameter, containing an id or a list of ids id = req.params.get("id") try: id = req.params["id"] except KeyError: pass else: idlist = id.split(",") if idlist != [""]: return idlist # look for a pattern, a source folder and an id list ids = req.params.get("ids", "") try: srcid = req.params.get("src") if srcid == "": raise KeyError src = tree.getNode(srcid) except KeyError: src = None idlist = ids.split(",") if idlist == [""]: idlist = [] return idlist
def show_help(req): if req.params.get("maskid", "") != "": field = getNode(req.params.get("maskid", "")) else: field = getNode(req.params.get("id", "")) req.writeTAL(theme.getTemplate("popups.html"), {"field": field}, macro="show_help")
def getParentInformation(self, req): '''sets diffrent used Information to a dict object :param req: request object :return: dict parentInformation ''' parentInformation = {} pid = req.params.get('pid', self.id) sid = req.params.get('id', self.id) if len(self.getChildren()) > 0 and self.type != 'directory' and self.type != 'collection': parentInformation['parent_node_id'] = req.params.get('pid', self.id) parentInformation['children_list'] = [child for child in self.getChildren() if not child.get('system.next_id') != ''] else: parentInformation['parent_node_id'] = re.split('\D', req.split_uri()[2].split('pid=')[-1])[0] parentInformation['children_list'] = [] if len([sib.id for sib in filter(lambda itm: str(itm.id) == parentInformation['parent_node_id'], self.getParents())]) != 0: parentInformation['parent_condition'] = True parentInformation['siblings_list'] = [c for c in tree.getNode(pid).getChildren() if c.id != self.id ] else: parentInformation['parent_condition'] = False parentInformation['siblings_list'] = tree.getNode(sid).getParents()[0].getChildren() if parentInformation['parent_node_id']: pass else: parentInformation['parent_node_id'] = re.split('\D', req.split_uri()[2].split('pid=')[-1])[0] parentInformation['paren_ex_dir&col'] = [id for id in self.getParents() if id.type != 'directory' and id.type != 'collection'] parentInformation['details_condition'] = self.getDetailsCondition() parentInformation['further_details'] = self.getFurtherDetailsCondition(req) return parentInformation
def simple_search(req): from web.frontend.content import ContentList res = [] words = [] collections = [] collection_ids = {} access = AccessData(req) q = u(req.params.get("query", "")) # test whether this query is restricted to a number of collections for key, value in req.params.items(): if key.startswith("c_"): collection_ids[key[2:]] = 1 # no collection means: all collections if len(collection_ids) == 0 or 1 in collection_ids.keys(): for collection in access.filter(tree.getRoot("collections").getChildren()): collection_ids[collection.id] = 1 # now retrieve all results in all collections for collection in getAllCollections(): if collection.id in collection_ids: collections.append(collection) num = 0 logging.getLogger('usertracing').info(access.user.name + " search for '" + q + "', " + str(num) + " results") try: if req.params.get("act_node", None) and tree.getNode(req.params.get("act_node")).getContentType() != "collections": # actual node is a collection or directory result = tree.getNode(req.params.get("act_node")).search('full=' + q) result = access.filter(result) num += len(result) if len(result) > 0: cl = ContentList(result, collection, words) cl.feedback(req) cl.linkname = "Suchergebnis" cl.linktarget = "" res.append(cl) else: # actual node is collections-node for collection in collections: result = collection.search('full=' + q) result = access.filter(result) num += len(result) if len(result) > 0: cl = ContentList(result, collection, words) cl.feedback(req) cl.linkname = "Suchergebnis" cl.linktarget = "" res.append(cl) if len(res) == 1: return res[0] else: return SearchResult(res, q, collections) except: return SearchResult(None, q, collections)
def getContent(req, ids): user = users.getUserFromRequest(req) access = AccessData(req) language = lang(req) node = tree.getNode(ids[0]) if "sort" in users.getHideMenusForUser(user) or not access.hasWriteAccess(node): req.setStatus(httpstatus.HTTP_FORBIDDEN) return req.getTAL("web/edit/edit.html", {}, macro="access_error") msg_t = (user.getName(), node.id, node.name, node.type, req.params) msg = "%s sorting subfolders of node %r (%r, %r): %r" % msg_t logger.info(msg) if "order" in req.params: # do reorder ids = req.params.get('order').split(',') children = [] for n in ids: child = tree.getNode(n) child.setOrderPos(ids.index(n)) children.append(child) req.writeTAL('web/edit/modules/subfolder.html', {'nodelist': children, "language": language}, macro="ordered_list") return "" elif "sortdirection" in req.params: # do automatic re-order i = 0 sort_dir = "" if req.params.get("sortdirection", "up") == "up" else "-" sorted_children = node.getContainerChildren().sort_by_fields(sort_dir + req.params.get("sortattribute")) for child in sorted_children: child.setOrderPos(i) i += 1 req.writeTAL('web/edit/modules/subfolder.html', {'nodelist': sorted_children, "language": language}, macro="ordered_list") return "" nodelist = [] attributes = [] fields = {} i = 0 for child in list(node.getContainerChildren().sort_by_orderpos()): i += 1 # count container children nodelist.append(child) if getMetaType(child.getSchema()): for field in getMetaType(child.getSchema()).getMetaFields(): if not field in fields.keys(): fields[field] = 0 fields[field] += 1 for field in fields: if i == fields[field]: attributes.append(field) ctx = { "node": node, "nodelist": nodelist, "sortattributes": sorted(attributes, lambda x, y: cmp(x.getLabel().lower(), y.getLabel().lower())), "language": language, } return req.getTAL("web/edit/modules/subfolder.html", ctx, macro="edit_subfolder")
def editItem(self, req): for key in req.params.keys(): # edit field if key.startswith("edit_"): item = tree.getNode(req.params.get("edit", "")) t = getMetadataType(item.get("type")) return '<form method="post" name="myform">%s</form>' % (t.getMetaEditor(item, req)) if (req.params.get("op", "") == "new" and req.params.get("type", "") != "") or ( self.getMasktype() == "export" and req.params.get("op", "") in ["newdetail", "new"]): # add field item = tree.Node(name="", type="maskitem") if self.getMasktype() == "export": # export mask has no selection of fieldtype -> only field t = getMetadataType("field") req.params["op"] = "new" else: t = getMetadataType(req.params.get("type")) if req.params.get("type", "") in ["hgroup", "vgroup"]: req.params["edit"] = item else: req.params["edit"] = item.id return '<form method="post" name="myform">%s</form>' % (t.getMetaEditor(item, req)) if (req.params.get("type", "") == "" and self.getMasktype() != "export") or req.params.get('op') == 'newdetail': # type selection for new field ret = """ <form method="post" name="myform"> <div class="back"><h3 i18n:translate="mask_editor_add_choose_fieldtype">Feldtyp wählen</h3> <div class="label" i18n:translate="mask_editor_field_selection">Feldtypauswahl:</div> <select name="type"> <option value="field" i18n:translate="mask_editor_normal_field">Normales Feld</option> <option value="vgroup" i18n:translate="mask_editor_vert_group">Vertikale Feldgruppe</option> <option value="hgroup" i18n:translate="mask_editor_hor_group">Horizontale Feldgruppe</option> <option value="label" i18n:translate="mask_editor_label">Label</option> </select> <br/> <br/> <span i18n:translate="mask_editor_msg1">Für Maskenfelder stehen verschiedene Typen zur Verfügung, die jeweils unterschiedliche Darstellungen erlauben. Standard ist das 'Normale Feld'</span> <br/> <br/> <input type="hidden" name="op" value="new"/>""" ret += '<input type="hidden" name="pid" value="' + str(req.params.get("pid")) + '"/>' ret += '<div class="label"> </div><button type="submit" name="new_" style="width:100px" i18n:translate="mask_editor_ok"> OK </button>' ret += ' <button type="submit" onclick="setCancel(document.myform.op)" i18n:translate="mask_editor_cancel">Abbrechen</button><br/>' ret += '</div></form>' return req.getTALstr(ret, {}) if req.params.get("edit", " ") == " " and req.params.get("op", "") != "new": # create new node item = tree.getNode(req.params.get("id")) t = getMetadataType(req.params.get("type")) return '<form method="post" name="myform">%s</form>' % (t.getMetaEditor(item, req))
def updateMappingField(parentid, name, description="", exportformat="", mandatory=False, default="", id=0): mapping = tree.getNode(parentid) if id != "" and int(id) > 0: mappingfield = tree.getNode(id) else: mappingfield = tree.Node(name=name, type="mappingfield") mapping.addChild(mappingfield) mappingfield.setName(name) mappingfield.setDescription(description) mappingfield.setExportFormat(exportformat) mappingfield.setMandatory(mandatory) mappingfield.setDefault(default)
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 str(req.params.get("edit")) == str("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(getNode(id).get("type")) try: details += f.getMetaHTML(item, i, False, itemlist=req.params.get("sel_id") [:-1].split(";"), ptype="vgroup", fieldlist=fieldlist) except TypeError: pass i += 1 fields = [] metadatatype = req.params.get("metadatatype") if req.params.get("op", "") == "new": pidnode = getNode(req.params.get("pid")) if pidnode.get("type") in ("vgroup", "hgroup"): for field in pidnode.getAllChildren(): if field.getType().getName() == "maskitem" and field.id != pidnode.id: fields.append(field) else: for m in metadatatype.getMasks(): if str(m.id) == str(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")
def mkContentNode(req): access = AccessData(req) id = req.params.get("id", tree.getRoot("collections").id) try: node = tree.getNode(id) except tree.NoSuchNodeError: return ContentError("No such node", 404) if not access.hasReadAccess(node): return ContentError("Permission denied", 403) if node.type in ["directory", "collection"]: if "files" not in req.params and len(filter(None, node.getStartpageDict().values())) > 0: for f in node.getFiles(): if f.type == "content" and f.mimetype == "text/html" and os.path.isfile( f.retrieveFile()) and fileIsNotEmpty(f.retrieveFile()): return ContentNode(node) ids = access.filter(list(set(tree.getAllContainerChildrenAbs(node, [])))) node.ccount = len(ids) #ids = access.filter(node.getAllChildren()) c = ContentList(tree.NodeList(ids), getCollection(node)) c.feedback(req) c.node = node return c else: return ContentNode(node)
def getContent(req, ids): user = users.getUserFromRequest(req) access = AccessData(req) node = tree.getNode(ids[0]) if "sortfiles" in users.getHideMenusForUser(user) or not access.hasWriteAccess(node): req.setStatus(httpstatus.HTTP_FORBIDDEN) return req.getTAL("web/edit/edit.html", {}, macro="access_error") c = getCollection(node) if "globalsort" in req.params: c.set("sortfield", req.params["globalsort"]) collection_sortfield = c.get("sortfield") class SortChoice: def __init__(self, label, value): self.label = label self.value = value sortfields = [SortChoice(t(req, "off"), "")] for ntype, num in c.getAllOccurences(AccessData(req)).items(): if ntype.getSortFields(): for sortfield in ntype.getSortFields(): sortfields += [SortChoice(sortfield.getLabel(), sortfield.getName())] sortfields += [SortChoice(sortfield.getLabel() + t(req, "descending"), "-" + sortfield.getName())] break return req.getTAL("web/edit/modules/sortfiles.html", {"node": node, "collection_sortfield": collection_sortfield, "sortchoices": sortfields, "name": c.getName()}, macro="edit_sortfiles")
def GetRecord(req): access = acl.AccessData(req) if "identifier" in req.params: id = identifier2id(req.params.get("identifier")) else: return writeError(req, "badArgument") metadataformat = req.params.get("metadataPrefix", None) if not checkMetaDataFormat(metadataformat): return writeError(req, "badArgument") try: node = tree.getNode(id) except (TypeError, KeyError, tree.NoSuchNodeError): return writeError(req, "idDoesNotExist") if metadataformat and (metadataformat.lower() in FORMAT_FILTERS.keys()) and not filterFormat(node, metadataformat.lower()): return writeError(req, "noPermission") if parentIsMedia(node): return writeError(req, "noPermission") if not access.hasReadAccess(node): return writeError(req, "noPermission") req.write('<GetRecord>') writeRecord(req, node, metadataformat) req.write('</GetRecord>') if DEBUG: timetable_update(req, "leaving GetRecord")
def export(req): p = req.path[1:].split("/") access = AccessData(req) if len(p) != 2: req.error(404, "Object not found") return if p[0].isdigit(): try: node = tree.getNode(p[0]) except: return req.error(404, "Object not found") else: return req.error(404, "Object not found") if not access.hasAccess(node, "read"): req.write(t(req, "permission_denied")) return mask = getMetaType(node.getSchema()).getMask(p[1]) if mask: try: req.reply_headers['Content-Type'] = "text/plain; charset=utf-8" req.write(mask.getViewHTML([node], flags=8)) # flags =8 -> export type except tree.NoSuchNodeError: return req.error(404, "Object not found") else: req.error(404, "Object not found") return
def popup_thumbbig(req): #access = AccessData(req) try: node = getNode(req.params["id"]) except tree.NoSuchNodeError: return 404 node.popup_thumbbig(req)
def getEditorHTML(self, field, value="", width=400, lock=0, language=None, required=None): ns = "" if field.get("fieldtype") == "mapping": field = tree.getNode(field.get("mappingfield")) ns = field.getMapping().getNamespace() if ns != "": ns += ":" format = field.getExportFormat() field_value = ns + field.getName() else: format = field.get("mappingfield") field_value = field.getName() format = esc(format) for var in re.findall(r"\[(.+?)\]", format): if var.startswith("att:"): format = format.replace("[" + var + "]", "<i>{attribute:" + var[4:] + "}</i>") elif var == "field": format = format.replace("[field]", field_value) elif var == "value": format = format.replace("[value]", "<i>{" + value + "}</i>") elif var == "ns": format = format.replace("[value]", "<i>{namspaces}</i>") format = format.replace("\\t", "") return format
def send_nodefile_tal(req): if "file" in req.params: return upload_for_html(req) id = req.params.get("id") node = tree.getNode(id) access = AccessData(req) if not (access.hasAccess(node, 'read') and access.hasAccess(node, 'write') and access.hasAccess(node, 'data') and node.type in ["directory", "collections", "collection"]): return "" def fit(imagefile, cn): # fits the image into a box with dimensions cn, returning new width and # height try: sz = PIL.Image.open(imagefile).size (x, y) = (sz[0], sz[1]) if x > cn[0]: y = (y * cn[0]) / x x = (x * cn[0]) / x if y > cn[1]: x = (x * cn[1]) / y y = (y * cn[1]) / y return (x, y) except: return cn # only pass images to the file browser files = [f for f in node.getFiles() if f.mimetype.startswith("image")] # this flag may switch the display of a "delete" button in the customs # file browser in web/edit/modules/startpages.html showdelbutton = True return req.getTAL("web/edit/modules/startpages.html", {"id": id, "node": node, "files": files, "fit": fit, "logoname": node.get("system.logo"), "delbutton": True}, macro="fckeditor_customs_filemanager")
def runAction(self, node, op=""): name = "" func = start = end = None attr = self.get('destination_attr') if attr != "" and "|" in attr: attr, func = attr.split("|") if attr != "": # name of subnode name = node.get(attr) if func and func.startswith('substring'): # check for function start, end = func[10:].split(",") if end and isNumeric(end): name = name[:int(end)] if start and isNumeric(start): name = name[int(start):] for nid in self.get('destination').split(";"): try: pnode = tree.getNode(nid) cnode = None if name != "": try: cnode = pnode.getChild(name) except tree.NoSuchNodeError: cnode = tree.Node(name, type="directory") pnode.addChild(cnode) if cnode: # add node to child given by attributename cnode.addChild(node) if self.get('only_sub') != '1': # add to node (no hierarchy) pnode.addChild(node) except tree.NoSuchNodeError: pass
def getExternalUser(name, type="intern"): """ returns user object from db """ users = getExternalUserFolder() if name.isdigit(): try: user = tree.getNode(name) if user.type == "user": return user return None except tree.NoSuchNodeError as e: try: user = users.getChild(name) if user: return user # try identificator for n in users.getChildren(): if ('%s@' % (name)) in n.get('identificator') or name in n.get('identificator'): return n except tree.NoSuchNodeError: return None else: for n in getExternalUsers(type): if n.getName() == name: return n # try identificator elif ('%s@' % (name)) in n.get('identificator') or name in n.get('identificator'): return n
def getUser(id): """ returns user object from db """ users = tree.getRoot("users") if id.isdigit(): try: user = tree.getNode(id) if user.type == "user": return user return None except tree.NoSuchNodeError as e: return None else: try: user = users.getChild(id) return user except tree.NoSuchNodeError as e: for key in getExternalAuthentificators(): u = getExternalUser(id, type=key) if u: # u.setUserType(key) return u for u in tree.getRoot("users").getChildren(): if u.get('service.userkey') == id: return u return None
def handletabs(req, ids, tabs): user = users.getUserFromRequest(req) language = lang(req) n = tree.getNode(ids[0]) if n.type.startswith("workflow"): n = tree.getRoot() menu = filterMenu(getEditMenuString(n.getContentType()), user) spc = [Menu("sub_header_frontend", "../", target="_parent")] if user.isAdmin(): spc.append(Menu("sub_header_administration", "../admin", target="_parent")) if user.isWorkflowEditor(): spc.append(Menu("sub_header_workflow", "../publish", target="_parent")) spc.append(Menu("sub_header_logout", "../logout", target="_parent")) # a html snippet may be inserted in the editor header help_link = tree.getRoot("collections").get("system.editor.help.link." + language).strip() ctx = { "user": user, "ids": ids, "idstr": ",".join(ids), "menu": menu, "hashelp": help.getHelpPath(["edit", "modules", req.params.get("tab") or tabs]), "breadcrumbs": getBreadcrumbs(menu, req.params.get("tab", tabs)), "spc": spc, "system_editor_help_link": help_link, } return req.getTAL("web/edit/edit.html", ctx, macro="edit_tabs")
def deleteSchedule(schedule_id, access=None): errors = [] if schedule_id: try: schedule = tree.getNode(schedule_id) if access and not access.hasWriteAccess(schedule): errors.append("edit_schedule_no_write_access_to_schedule") except: errors.append("edit_schedule_unexpected_no_such_schedule_node") else: errors.append("edit_schedule_unexpected_no_such_schedule_node") if schedule.type not in ['schedule']: errors.append("edit_schedule_unexpected_no_such_schedule_node") if errors: return errors schedule_locked = schedule.get('system.lock') if schedule_locked.lower() in ['true', '1']: errors.append("edit_schedule_locked_schedule") try: schedules_root = tree.getRoot("schedules") schedules_root.removeChild(schedule) except: errors.append("edit_schedule_error_removing_schedule_node") return errors
def send_thumbnail2(req): try: n = tree.getNode(splitpath(req.path)[0]) except tree.NoSuchNodeError: return 404 for f in n.getFiles(): if f.getType().startswith("presentat"): if os.path.isfile(f.retrieveFile()): return req.sendFile(f.retrieveFile(), f.getMimeType()) # fallback for f in n.getFiles(): if f.getType() == "image": if os.path.isfile(f.retrieveFile()): return req.sendFile(f.retrieveFile(), f.getMimeType()) # fallback2 for p in athana.getFileStorePaths("/img/"): for test in [ "default_thumb_%s_%s.*" % (n.getContentType(), n.getSchema()), "default_thumb_%s.*" % (n.getSchema()), "default_thumb_%s.*" % (n.getContentType())]: #fps = glob.glob(os.path.join(config.basedir, theme.getImagePath(), "img", test)) fps = glob.glob(os.path.join(config.basedir, p[2:], test)) if fps: thumb_mimetype, thumb_type = utils.utils.getMimeType(fps[0]) return req.sendFile(fps[0], thumb_mimetype, force=1) return 404
def archive_thread(self): if not time: return while True: time.sleep(int(config.get("archive.interval", 60))) archive_nodes_3 = db.getNodeIdByAttribute("archive_state", "3") archive_nodes_2 = [] date_now = format_date(now(), "yyymmddhhmmss") for manager in self.manager: # search for nodes to archive after access over period (state 2) for n in db.getNodeIdByAttribute("archive_state", "2"): try: node = tree.getNode(n) if node.get("archive_date"): date_archive = format_date(parse_date(node.get("archive_date"), "%Y-%m-%dT%H:%M:%S"), "yyymmddhhmmss") if date_now >= date_archive: archive_nodes_2.append(long(node.id)) except: pass # union to get all nodes with state 3 and 2 with over period archive_nodes = union((archive_nodes_3, archive_nodes_2)) nodes = intersection((db.getNodeIdByAttribute("archive_type", str(manager)), archive_nodes)) # run action defined in manager try: self.manager[manager].actionArchive(nodes) except: pass
def get_archived(req): print "send archived" id, filename = splitpath(req.path) node = tree.getNode(id) node.set("archive_state", "1") if not archivemanager: req.write("-no archive module loaded-") return archiveclass = "" for item in config.get("archive.class").split(";"): if item.endswith(node.get("archive_type")): archiveclass = item + ".py" break if archiveclass: # start process from archive os.chdir(config.basedir) os.system("python %s %s" % (archiveclass, node.id)) st = "" while True: # test if process is still running attrs = tree.db.getAttributes(id) if "archive_state" in attrs.keys(): st = attrs['archive_state'] time.sleep(1) if st == "2": break for n in node.getAllChildren(): tree.remove_from_nodecaches(n) req.write('done')
def format_records(self, start, count, res_set, prefsyn): """ Format a 'present' response for the requested query result subset. """ #encode_charset = self.charset_name map_node = medmarc.MarcMapper() l = [] for i in xrange(start - 1, min(len(res_set), start + count - 1)): try: node = tree.getNode(res_set[i]) except tree.NoSuchNodeError: logg.debug("request for non-existant node %s", res_set[i]) continue marc21_node = map_node(node) if marc21_node is None: # no mapping => no node continue elt_external = asn1.EXTERNAL() elt_external.direct_reference = z3950.Z3950_RECSYN_USMARC_ov elt_external.encoding = ('octet-aligned', marc21_node) n = z3950.NamePlusRecord() n.name = str(node.id) n.record = ('retrievalRecord', elt_external) l.append(n) return l
def deleteNodeIDsFromSchedule(node_id_list, schedule_id, access=None): errors = [] if schedule_id: try: schedule = tree.getNode(schedule_id) if access and not access.hasWriteAccess(schedule): errors.append("edit_schedule_no_write_access_to_schedule") except: errors.append("edit_schedule_unexpected_no_such_schedule_node") else: errors.append("edit_schedule_unexpected_no_such_schedule_node") if errors: return errors nodelist = schedule.get('nodelist').strip().split(',') nodelist = [nid.strip() for nid in nodelist if nid.strip()] new_nodelist = [] for nid in nodelist: if nid not in node_id_list: new_nodelist.append(nid) new_nodelist = ",".join(new_nodelist) schedule.set('nodelist', new_nodelist) return errors
def getParentInformation(self, req): '''sets diffrent used Information to a dict object :param req: request object :return: dict parentInformation ''' parentInformation = {} pid = req.params.get('pid', self.id) parentInformation['parent_node_id'] = pid if len(self.getChildren()) > 0 and self.isContainer() != 1: parentInformation['children_list'] = [child for child in self.getChildren() if not child.get('system.next_id') != ''] else: parentInformation['children_list'] = [] if len([sib.id for sib in filter(lambda itm: str(itm.id) == pid, self.getParents())]) != 0: parentInformation['parent_condition'] = True parentInformation['siblings_list'] = [c for c in tree.getNode(pid).getChildren() if c.id != self.id] else: parentInformation['parent_condition'] = False if len(self.getParents()) > 0: parentInformation['siblings_list'] = self.getParents()[0].getChildren() else: parentInformation['siblings_list'] = [] parentInformation['display_siblings'] = pid != self.id parentInformation['parent_is_container'] = [id for id in self.getParents() if self.isContainer() != 1] parentInformation['details_condition'] = self.getDetailsCondition() parentInformation['further_details'] = self.getFurtherDetailsCondition(req) return parentInformation
def getImage(nid, preprocess=0): node = tree.getNode(nid) img = ZoomImage(node, preprocess) import os if "MEDIATUM_EMBED_IPYTHON" in os.environ: import IPython IPython.embed() return img
def __init__(self, data, period, id, language): self.styleSheet = getSampleStyleSheet() self.stats = data self.period = period self.language = language self._pages = 1 self.data = [] self.collection = tree.getNode(id)
def getMask(self, name): try: if name.isdigit(): return tree.getNode(name) else: return self.get_child_with_type(name, "mask") except tree.NoSuchNodeError: return None
def deleteMaskitem(self, itemid): item = tree.getNode(itemid) for parent in item.getParents(): parent.removeChild(item) i = 0 for child in parent.getChildren().sort_by_orderpos(): child.setOrderPos(i) i += 1
def getContent(req, ids): user = users.getUserFromRequest(req) node = tree.getNode(ids[0]) access = AccessData(req) if not access.hasWriteAccess( node) or "searchmask" in users.getHideMenusForUser(user): req.setStatus(httpstatus.HTTP_FORBIDDEN) return req.getTAL("web/edit/edit.html", {}, macro="access_error") p2 = {} for k, v in req.params.items(): if k.endswith(".x") or k.endswith(".y"): p2[k[:-2]] = v else: p2[k] = v req.params = p2 openfield = None delfield = None delsubfield = None for k, v in req.params.items(): if k.startswith("open_"): openfield = k[5:] if k.startswith("del_"): delfield = k[4:] if k.startswith("delsub_"): delsubfield = k[7:] try: root = tree.getRoot("searchmasks") except tree.NoSuchNodeError: root = tree.Node("searchmasks", type="searchmasks") tree.getRoot().addChild(root) searchtype = req.params.get("searchtype", None) if not searchtype: searchtype = node.get("searchtype") if not searchtype: searchtype = "none" # if a parent has a search mask, use 'inherit' n = node while len(n.getParents()): n = n.getParents()[0] if n.get("searchtype") == "own": searchtype = "parent" node.set("searchtype", searchtype) try: myschema = tree.getNode(req.params.get("schema", None)) except tree.NoSuchNodeError: if req.params.get("schema", None) and req.params.get("schema").endswith(";"): myschema = tree.getNode(req.params.get("schema")[:-1]) else: myschema = None try: schemafield = tree.getNode(req.params.get("schemafield", None)) except tree.NoSuchNodeError: if req.params.get( "schemafield", None) and req.params.get("schemafield").endswith(";"): schemafield = tree.getNode(req.params.get("schemafield")[:-1]) else: schemafield = None if myschema and schemafield and schemafield not in myschema.getChildren(): schemafield = None if schemafield and schemafield.type != "metafield": schemafield = None fields = None selectedfield = None isnewfield = False createsub = False closefield = False if searchtype == "own": maskname = node.get("searchmaskname") if not maskname or root.hasChild(maskname) == 0: mask = searchmask.generateMask(node) else: mask = root.getChild(maskname) selectedfieldid = req.params.get("selectedfield", None) if selectedfieldid: # edit selectedfield = tree.getNode(selectedfieldid) assert selectedfield in mask.getChildren() selectedfield.setName(req.params["fieldname"]) if "createsub" in req.params and schemafield: createsub = True selectedfield.addChild(schemafield) if delsubfield: selectedfield.removeChild(tree.getNode(delsubfield)) if req.params.get("isnewfield", "") == "yes": # create a new field isnewfield = True l = mask.getNumChildren() mask.addChild(tree.Node("Suchfeld %s" % l, type="searchmaskitem")) elif delfield: # del a field delfield = tree.getNode(delfield) assert delfield in mask.getChildren() mask.removeChild(delfield) elif openfield: # unfold a new field selectedfieldid = openfield elif "close" in req.params: # fold a field closefield = True selectedfieldid = None if selectedfieldid: selectedfield = tree.getNode(selectedfieldid) if selectedfield not in mask.getChildren( ): # this usually happens if the field was just deleted selectedfield = None else: selectedfield = None if mask is None: print "no parent searchmask found, empty mask created" mask = tree.Node(name=maskname, type="searchmask") fields = mask.getChildren() data = { "idstr": ",".join(ids), "node": node, "searchtype": searchtype, "schemas": schema.loadTypesFromDB(), "searchfields": fields, "selectedfield": selectedfield, "newfieldlink": "edit_content?id=%s&tab=searchmask" % node.id, "defaultschemaid": None, "defaultfieldid": None, "id": req.params.get("id") } if myschema: data["defaultschemaid"] = myschema.id if schemafield: data["defaultfieldid"] = schemafield.id data["schema"] = myschema def display(schemafield): if not schemafield or schemafield.type != 'metafield': return 0 if not schemafield.Searchfield(): return 0 if schemafield.get('type') == 'union': return 0 return 1 data["display"] = display searchtypechanged = False if req.params.get("searchtypechanged", "") == "true": searchtypechanged = True if any([ openfield, isnewfield, delfield, delsubfield, createsub, myschema, searchtypechanged, closefield ]): content = req.getTAL("web/edit/modules/searchmask.html", data, macro="edit_search") s = json.dumps({'content': content}) req.write(s) return None return req.getTAL("web/edit/modules/searchmask.html", data, macro="edit_search")
def upload_new_node(req, path, params, data): try: uploadfile = params['data'] del params['data'] except KeyError: uploadfile = None # get the user and verify the signature if params.get('user'): # user=users.getUser(params.get('user')) #userAccess = AccessData(user=user) _user = users.getUser(params.get('user')) if not _user: # user of dynamic class dummyuser: # dummy user class # return all groups with given dynamic user def getGroups(self): return [ g.name for g in tree.getRoot('usergroups').getChildren() if g.get('allow_dynamic') == '1' and params.get('user') in g.get('dynamic_users') ] def getName(self): return params.get('user') def getDirID(self): # unique identifier return params.get('user') def isAdmin(self): return 0 _user = dummyuser() userAccess = AccessData(user=_user) if userAccess.user: user = userAccess.user if not userAccess.verify_request_signature(req.fullpath + '?', params): userAccess = None else: userAccess = None else: user = users.getUser(config.get('user.guestuser')) userAccess = AccessData(user=user) parent = tree.getNode(params.get('parent')) # check user access if userAccess and userAccess.hasAccess(parent, "write"): pass else: s = "No Access" req.write(s) d = { 'status': 'fail', 'html_response_code': '403', 'errormessage': 'no access' } logger.error("user has no edit permission for node %s" % parent) return d['html_response_code'], len(s), d datatype = params.get('type') uploaddir = users.getUploadDir(user) n = tree.Node(name=params.get('name'), type=datatype) if isinstance(uploadfile, types.InstanceType): # file object used nfile = importFile(uploadfile.filename, uploadfile.tempname) else: # string used nfile = importFileFromData('uploadTest.jpg', base64.b64decode(uploadfile)) if nfile: n.addFile(nfile) else: logger.error("error in file uploadservice") try: # test metadata metadata = json.loads(params.get('metadata')) except ValueError: metadata = dict() # set provided metadata for key, value in metadata.iteritems(): n.set(u(key), u(value)) # service flags n.set("creator", user.getName()) n.set("creationtime", format_date()) parent.addChild(n) # process the file, we've added to the new node if hasattr(n, "event_files_changed"): try: n.event_files_changed() except OperationException as e: for file in n.getFiles(): if os.path.exists(file.retrieveFile()): os.remove(file.retrieveFile()) raise OperationException(e.value) # make sure the new node is visible immediately from the web service and # the search index gets updated n.setDirty() tree.remove_from_nodecaches(parent) d = { 'status': 'Created', 'html_response_code': '201', 'build_response_end': time.time() } s = "Created" # provide the uploader with the new node ID req.reply_headers['NodeID'] = n.id # we need to write in case of POST request, send as buffer will not work req.write(s) return d['html_response_code'], len(s), d
def send_file(req, download=0): access = AccessData(req) id, filename = splitpath(req.path) if id.endswith("_transfer.zip"): id = id[:-13] try: n = tree.getNode(id) except tree.NoSuchNodeError: return 404 if not access.hasAccess(n, "data") and n.type not in [ "directory", "collections", "collection" ]: return 403 file = None if filename is None and n: # build zip-file and return it zipfilepath, files_written = build_transferzip(n) if files_written == 0: return 404 send_result = req.sendFile(zipfilepath, "application/zip") if os.sep == '/': # Unix? os.unlink( zipfilepath ) # unlinking files while still reading them only works on Unix/Linux return send_result # try full filename for f in n.getFiles(): if f.getName() == filename: incUsage(n) file = f break # try only extension if not file and n.get("archive_type") == "": file_ext = os.path.splitext(filename)[1] for f in n.getFiles(): if os.path.splitext( f.getName())[1] == file_ext and f.getType() in [ 'doc', 'document', 'original', 'mp3' ]: incUsage(n) file = f break if existMetaField(n.getSchema(), 'nodename'): display_file_name = '{}.{}'.format( os.path.splitext(os.path.basename(n.name))[0], os.path.splitext(filename)[-1].strip('.')) else: display_file_name = filename # try file from archivemanager if not file and n.get("archive_type") != "": am = archivemanager.getManager(n.get("archive_type")) req.reply_headers[ "Content-Disposition"] = 'attachment; filename="{}"'.format( display_file_name) return req.sendFile(am.getArchivedFileStream(n.get("archive_path")), "application/x-download") if not file: return 404 req.reply_headers[ "Content-Disposition"] = 'attachment; filename="{}"'.format( display_file_name) return req.sendFile(file.retrieveFile(), f.getMimeType())
def frameset(req): id = req.params.get("id", tree.getRoot("collections").id) tab = req.params.get("tab", None) language = lang(req) access = AccessData(req) if not access.getUser().isEditor(): req.writeTAL("web/edit/edit.html", {}, macro="error") req.writeTAL("web/edit/edit.html", {"id": id, "tab": (tab and "&tab=" + tab) or ""}, macro="edit_notree_permission") req.setStatus(httpstatus.HTTP_FORBIDDEN) return try: currentdir = tree.getNode(id) except tree.NoSuchNodeError: currentdir = tree.getRoot("collections") req.params["id"] = currentdir.id id = req.params.get("id") nodepath = [] n = currentdir while n: nodepath = [n] + nodepath p = n.getParents() if p: n = p[0] else: n = None path = nidpath = ['%s' % p.id for p in nodepath] containerpath = [('%s' % p.id) for p in nodepath if p.isContainer()] user = users.getUserFromRequest(req) menu = filterMenu(getEditMenuString(currentdir.getContentType()), user) spc = [Menu("sub_header_frontend", "../", target="_parent")] if user.isAdmin(): spc.append( Menu("sub_header_administration", "../admin", target="_parent")) if user.isWorkflowEditor(): spc.append(Menu("sub_header_workflow", "../publish", target="_parent")) spc.append(Menu("sub_header_logout", "../logout", target="_parent")) def getPathToFolder(node): n = node path = [] while n: path = ['/%s' % (n.id)] + path p = n.getParents() if p: n = p[0] else: n = None return (node, "".join(path[2:])) def _getIDPath(nid, sep="/", containers_only=True): res = getIDPaths(nid, access, sep=sep, containers_only=containers_only) return res folders = {'homedir': getPathToFolder(users.getHomeDir(user)), 'trashdir': getPathToFolder(users.getSpecialDir( user, 'trash')), 'uploaddir': getPathToFolder(users.getSpecialDir(user, 'upload')), 'importdir': getPathToFolder(users.getSpecialDir(user, 'import'))} cmenu = sorted(getContainerTreeTypes(req), key=lambda x: x.getName()) cmenu_iconpaths = [] for ct in cmenu: ct_name = ct.getName() _n = tree.Node("", type=ct_name) # translations of ct_name will be offered in editor tree context menu cmenu_iconpaths.append( [ct, getEditorIconPath(_n), ct_name, translation_t(language, ct_name)]) # a html snippet may be inserted in the editor header header_insert = tree.getRoot('collections').get('system.editor.header.insert.' + language).strip() help_link = tree.getRoot('collections').get('system.editor.help.link.' + language).strip() homenodefilter = req.params.get('homenodefilter', '') v = { "id": id, "tab": (tab and "&tab=" + tab) or "", 'user': user, 'spc': spc, 'folders': folders, 'collectionsid': tree.getRoot('collections').id, "basedirs": [tree.getRoot('home'), tree.getRoot('collections')], 'cmenu': cmenu, 'cmenu_iconpaths': cmenu_iconpaths, 'path': path, 'containerpath': containerpath, 'language': lang(req), 't': translation_t, '_getIDPath': _getIDPath, 'system_editor_header_insert': header_insert, 'system_editor_help_link': help_link, 'homenodefilter': homenodefilter, } req.writeTAL("web/edit/edit.html", v, macro="edit_main")
# deliver schemes for given contenttype if req.params.get('action') == 'getschemes': ret = [] for scheme in getSchemesforType(access, req.params.get('contenttype')): ret.append({'id': scheme.getName(), 'name': scheme.getLongName()}) req.write(json.dumps({'schemes': ret})) return None # create node with given type/schema if req.params.get('action') == "createobject": schema = req.params.get('schema') ctype = req.params.get('contenttype') n = tree.Node('', type=ctype + '/' + schema) basenode = tree.getNode(req.params.get('id')) basenode.addChild(n) n.set("creator", user.getName()) n.set("creationtime", str(time.strftime('%Y-%m-%dT%H:%M:%S', time.localtime(time.time())))) clearFromCache(n) msg_t = (user.getName(), n.id, n.name, n.type, basenode.id, basenode.name, basenode.type) msg = "%s created new node %r (%r, %r) using upload metadata form, node is child of base node %r (%r, %r)" % msg_t logger.info(msg) req.write(json.dumps({'newid': n.id, 'id': req.params.get('id')})) return None # create node using given identifier (doi, ...) if req.params.get('action') == "obj_from_identifier": identifier_importer = req.params.get('identifier_importer') identifier = req.params.get('identifier')
def content(req): user = users.getUserFromRequest(req) access = AccessData(req) language = lang(req) if not access.user.isEditor(): return req.writeTAL("web/edit/edit.html", {}, macro="error") if 'id' in req.params and len(req.params) == 1: nid = req.params.get('id') try: node = tree.getNode(nid) except: node = None if node: cmd = "cd (%s %r, %r)" % (nid, node.name, node.type) logger.info("%s: %s" % (user.getName(), cmd)) #logging.getLogger("usertracing").info("%s: in editor %s" % (user.getName(), cmd)) else: cmd = "ERROR-cd to non-existing id=%r" % nid logger.error("%s: %s") % (user.getName(), 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 access.user.isEditor(): 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"]: try: del req.session[sessionkey] except: pass ids = getIDs(req) if req.params.get("type", "") == "help" and req.params.get("tab", "") == "upload": return upload_help(req) if len(ids) > 0: node = tree.getNode(ids[0]) tabs = "content" if node.type == "root": tabs = "content" elif node.id == users.getUploadDir(access.getUser()).id: tabs = "upload" elif node.id == users.getImportDir(access.getUser()).id: tabs = "imports" elif hasattr(node, "getDefaultEditTab"): tabs = node.getDefaultEditTab() v["notdirectory"] = 0 current = req.params.get("tab", tabs) logger.debug("... %s inside %s.%s: -> !!! current = %r !!!" % (get_user_id(req), __name__, funcname(), current)) msg = "%s selected editor module is %r" % (user.getName(), current) jsfunc = req.params.get("func", "") if jsfunc: msg = msg + (', js-function: %r' % jsfunc) logger.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 tree.getNode(ids[0]).isContainer(): v["notdirectory"] = 1 items = [] if current != "view": for id in ids: node = tree.getNode(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 logger.debug("... %s inside %s.%s: -> display current images: items: %r" % (get_user_id(req), __name__, funcname(), [_t[0] for _t in items])) try: n = tree.getNode(req.params.get('src', req.params.get('id'))) if current == 'metadata' and 'save' in req.params: pass s = [] while n: try: s = ['<a onClick="activateEditorTreeNode(%r); return false;" href="/edit/edit_content?id=%s">%s</a>' % (n.id, n.id, n.getLabel(lang=language))] + s except: s = ['<a onClick="activateEditorTreeNode(%r); return false;" href="/edit/edit_content?id=%s">%s</a>' % (n.id, n.id, n.name)] + s p = n.getParents() if p: n = p[0] else: n = None v["dircontent"] = ' <b>»</b> '.join(s[1:]) except: logger.exception('ERROR displaying current images') else: # or current directory n = tree.getNode(ids[0]) s = [] while n: if len(s) == 0: try: s = ['%s' % (n.getLabel(lang=language))] except: s = ['%s' % (n.name)] else: try: s = ['<a onClick="activateEditorTreeNode(%r); return false;" href="/edit/edit_content?id=%s">%s</a>' % (n.id, n.id, n.getLabel(lang=language))] + s except: s = ['<a onClick="activateEditorTreeNode(%r); return false;" href="/edit/edit_content?id=%s">%s</a>' % (n.id, n.id, n.name)] + s p = n.getParents() if p: n = p[0] else: n = None v["dircontent"] = ' <b>»</b> '.join(s[1:]) 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: d = node.getStartpageDict() if d and lang(req) in d: file_to_edit = d[lang(req)] found = False for f in node.getFiles(): if f.mimetype == 'text/html': filepath = f.retrieveFile().replace(basedir, '') if file_to_edit == filepath: found = True result = edit_editor(req, node, f) if result == "error": logger.error("error editing %r" % f.retrieveFile()) break if not found: edit_editor(req, node, None) elif current == "tab_metadata": edit_metadata(req, ids) # undefined elif current == "tab_upload": edit_upload(req, ids) # undefined elif current == "tab_import": edit_import(req, ids) # undefined elif current == "tab_globals": req.write("") elif current == "tab_lza": edit_lza(req, ids) # undefined elif current == "tab_logo": edit_logo(req, ids) # undefined else: t = current.split("_")[-1] if t in editModules.keys(): c = editModules[t].getContent(req, ids) if c: content["body"] += c # use standard method of module else: logger.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") user = users.getUserFromRequest(req) v['user'] = user v['language'] = lang(req) v['t'] = translation_t v['spc'] = [Menu("sub_header_frontend", "../", target="_parent")] if user.isAdmin(): v['spc'].append(Menu("sub_header_administration", "../admin", target="_parent")) if user.isWorkflowEditor(): v['spc'].append(Menu("sub_header_workflow", "../publish", target="_parent")) v['spc'].append(Menu("sub_header_logout", "../logout", target="_parent")) # add icons to breadcrumbs ipath = 'webtree/directory.gif' if node and node.isContainer(): if node.name == 'home' or 'Arbeitsverzeichnis' in node.name: ipath = 'webtree/homeicon.gif' elif node.name == 'Uploads': ipath = 'webtree/uploadicon.gif' elif node.name == 'Importe': ipath = 'webtree/importicon.gif' elif node.name == 'Inkonsistente Daten': ipath = 'webtree/faultyicon.gif' elif node.name == 'Papierkorb': ipath = 'webtree/trashicon.gif' else: ipath = getEditorIconPath(node) v["dircontent"] += ' <img src="' + '/img/' + ipath + '" />' return req.writeTAL("web/edit/edit.html", v, macro="frame_content")
def create_sitemap(self, nodes, p_num): """ Creates a sitemap.xml from the nodes which were passed in @param nodes: A list of nodes from the mediatum tree in the format '#*' """ def check_date_format(date): """ Checks to see whether dates are in proper datetime format and converts times in ##/##/#### format to datetime or raises an error when it encounters a different format """ # check if date is already in the proper format datetime_pattern = re.compile( r'\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$') # regex and its accompanying strptime format misc_date_formats = ( (re.compile( r'\d{2}/\d{2}/\d{4}\+\d{2}:\d{2}T\d{2}:\d{2}:\d{2}$'), '%m/%d/%Y+%H:%MT%H:%M:%S'), (re.compile(r'\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$'), '%Y-%m-%dT%H:%M:%S'), (re.compile(r'\d{4}/\d{2}/\d{2}$'), '%d/%m/%YT%H:%M:%S'), (re.compile(r'\d{4}/\d{2}/\d{2}$'), '%/%d/%YT%H:%M:%S'), (re.compile(r'\d{2}/\d{2}/\d{4}\+\d{2}:\d{2}$'), '%m/%d/%Y+%H:%M'), (re.compile(r'\d{4}-\d{2}-\d{2}$'), '%Y-%m-%d'), (re.compile(r'\d{2}/\d{2}/\d{4}$'), '%d/%m/%Y'), (re.compile(r'\d{2}/\d{2}/\d{4}$'), '%m/%d/%Y')) matched = re.search(datetime_pattern, date) if matched: return date else: for date_format_tuple in misc_date_formats: matched = re.search(date_format_tuple[0], date) if matched: try: timestruct = time.strptime(date, date_format_tuple[1]) timedatetime = datetime.datetime.fromtimestamp( time.mktime(timestruct)) return timedatetime.strftime('%Y-%m-%dT%H:%M:%S') except ValueError: continue else: raise TypeError('unknown date format given: %s' % date) if os.path.isfile(self.path): logging.getLogger('everything').info( 'Sitemap already exists at %s' % self.path) else: root = etree.Element( 'urlset', xmlns="http://www.sitemaps.org/schemas/sitemap/0.9") # Doesn't create a sitemap if no nodes to place in it if not nodes: pass else: for id_lastmod_tuple in nodes: url = etree.SubElement(root, 'url') loc = etree.SubElement(url, 'loc') if 'system.aliascol' in tree.getNode( id_lastmod_tuple[0]).attributes and USE_ALIASES: loc.text = ''.join([ 'http://', self.host, '/', tree.getNode(id_lastmod_tuple[0]). attributes['system.aliascol'] ]) else: loc.text = ''.join([ 'http://', self.host, '/node?id=', id_lastmod_tuple[0] ]) lastmod = etree.SubElement(url, 'lastmod') #remove preexisting time zone indicator stripped_date = id_lastmod_tuple[1].replace('+02:00', '') if stripped_date == '': lastmod.text = ''.join([ datetime.datetime.now().strftime( '%Y-%m-%dT%H:%M:%S'), '+02:00' ]) else: lastmod.text = ''.join( [check_date_format(stripped_date), '+02:00']) changefreq = etree.SubElement(url, 'changefreq') changefreq.text = 'monthly' priority = etree.SubElement(url, 'priority') priority.text = p_num try: with open(self.path, 'w') as f: f.write('''<?xml version="1.0" encoding="UTF-8"?>\n''') f.write(etree.tostring(root)) f.close() except IOError: logging.getLogger('error').error('Error creating %s' % self.path)
def export_shoppingbag_zip(req): from web.frontend.streams import sendZipFile from utils.utils import join_paths import core.config as config import random import os access = AccessData(req) items = [] for key in req.params.keys(): if key.startswith("select_"): _nid = key[7:] _n = tree.getNode(_nid) if access.hasAccess(_n, 'read'): items.append(_nid) dest = join_paths(config.get("paths.tempdir"), str(random.random())) + "/" # images if req.params.get("type") == "image": if req.params.get("metadata") in ["no", "yes"]: format_type = req.params.get("format_type") processtype = "" processvalue = "" if format_type == "perc": processtype = "percentage" _perc = req.params.get("img_perc", ";").split(";") if _perc[0] != "": processvalue = _perc[0] else: processvalue = int(_perc[1]) elif format_type == "pix": processtype = "pixels" _pix = req.params.get("img_pix", ";;").split(";") if _pix[0] != "": processvalue = _pix[0] else: processvalue = int(_pix[1]) elif format_type == "std": processtype = "standard" processvalue = req.params.get("img_pix", ";;").split(";")[2] for item in items: node = tree.getNode(item) if not access.hasAccess(node, 'data'): continue if node.processImage(processtype, processvalue, dest) == 0: print "image not found" # documenttypes if req.params.get("type") == "document": if req.params.get("metadata") in ["no", "yes"]: if not os.path.isdir(dest): os.mkdir(dest) for item in items: node = tree.getNode(item) if not access.hasAccess(node, 'data'): continue if node.processDocument(dest) == 0: print "document not found" # documenttypes if req.params.get("type") == "media": if req.params.get("metadata") in ["no", "yes"]: if not os.path.isdir(dest): os.mkdir(dest) for item in items: node = tree.getNode(item) if not access.hasAccess(node, 'data'): continue if node.processMediaFile(dest) == 0: print "file not found" # metadata def flatten(arr): return sum( map(lambda a: flatten(a) if (a and isinstance(a[0], list) and a != "") else [a], [a for a in arr if a not in['', []]]), []) if req.params.get("metadata") in ["yes", "meta"]: for item in items: node = tree.getNode(item) if not access.hasAccess(node, 'read'): continue if not os.path.isdir(dest): os.mkdir(dest) content = {"header": [], "content": []} for c in flatten(node.getFullView(lang(req)).getViewHTML([node], VIEW_DATA_ONLY)): content["header"].append(c[0]) content["content"].append(c[1]) f = open(dest + item + ".txt", "w") f.write("\t".join(content["header"]) + "\n") f.write("\t".join(content["content"]) + "\n") f.close() if len(items) > 0: sendZipFile(req, dest) for root, dirs, files in os.walk(dest, topdown=False): for name in files: os.remove(os.path.join(root, name)) for name in dirs: os.rmdir(os.path.join(root, name)) if os.path.isdir(dest): os.rmdir(dest)
def getContent(req, ids): user = users.getUserFromRequest(req) publishdir = tree.getNode(ids[0]) explicit = tree.getNodesByAttribute("writeaccess", user.getName()) ret = "" actionerror = [] changes = [] if "dopublish" in req.params.keys(): access = AccessData(req) objlist = [] for key in req.params.keys(): if key.isdigit(): objlist.append(key) src = tree.getNode(req.params.get("id")) for obj_id in objlist: faultylist = [] obj = tree.getNode(obj_id) for mask in obj.getType().getMasks( type="edit"): # check required fields if access.hasReadAccess(mask) and mask.getName() == obj.get( "edit.lastmask"): for f in mask.validateNodelist([obj]): faultylist.append(f) if len(faultylist) > 0: # object faulty actionerror.append(obj_id) continue for dest_id in req.params.get("destination", "").split(","): if dest_id == "": # no destination given continue dest = tree.getNode(dest_id) if dest != src and access.hasReadAccess( src) and access.hasWriteAccess( dest) and access.hasWriteAccess( obj) and isDirectory(dest): if not nodeIsChildOfNode(dest, obj): dest.addChild(obj) src.removeChild(obj) if dest.id not in changes: changes.append(dest.id) if src.id not in changes: changes.append(src.id) log.info( "%s published %s (%r, %r) from src %s (%r, %r) to dest %s (%r, %r)" % ( user.getName(), obj.id, obj.name, obj.type, src.id, src.name, src.type, dest.id, dest.name, dest.type, )) else: actionerror.append(obj.id) log.error( "Error in publishing of node %r: Destination node %r is child of node." % (obj_id, dest.id)) if not access.hasReadAccess(src): log.error( "Error in publishing of node %r: source position %r has no read access." % (obj.id, src.id)) if not access.hasWriteAccess(dest): log.error( "Error in publishing of node %r: destination %r has no write access." % (obj.id, dest.id)) if not access.hasWriteAccess(obj): log.error( "Error in publishing of node %r: object has no write access." % obj.id) if not isDirectory(dest): log.error( "Error in publishing of node %r: destination %r is not a directory." % (obj.id, dest.id)) v = {} v["id"] = publishdir.id v["change"] = changes ret += req.getTAL("web/edit/modules/publish.html", v, macro="reload") # build normal window stddir = "" stdname = "" l = [] for n in explicit: if str(getHomeDir(user).id) != str(n): l.append(n) if len(l) == 1: stddir = str(l[0]) + "," stdname = "- " + tree.getNode(l[0]).getName() #v = {"id":publishdir.id,"stddir":stddir, "stdname":stdname, "showdir":showdir(req, publishdir, publishwarn=0, markunpublished=1, nodes=[])} v = { "id": publishdir.id, "stddir": stddir, "stdname": stdname, "showdir": showdir(req, publishdir, publishwarn=None, markunpublished=1, nodes=[]) } v["basedir"] = tree.getRoot('collections') v["script"] = "var currentitem = '%s';\nvar currentfolder = '%s'" % ( publishdir.id, publishdir.id) v["idstr"] = ids v["faultylist"] = actionerror ret += req.getTAL("web/edit/modules/publish.html", v, macro="publish_form") return ret
def getContent(req, ids): user = users.getUserFromRequest(req) if "lza" in users.getHideMenusForUser(user): req.setStatus(httpstatus.HTTP_FORBIDDEN) return req.getTAL("web/edit/edit.html", {}, macro="access_error") v = {} v['error'] = "" nodes = [] for id in ids: node = tree.getNode(id) access = acl.AccessData(req) if not access.hasWriteAccess(node): req.setStatus(httpstatus.HTTP_FORBIDDEN) return req.getTAL("web/edit/edit.html", {}, macro="access_error") nodes.append(node) if "createlza" in req.params: # remove old file if existing for f in node.getFiles(): if f.getType() == "lza": node.removeFile(f) # create new file for f in node.getFiles(): if f.getType() in ("original", "document"): try: archive = l.LZA(f.retrieveFile()) schema = node.getSchema() # test for lza export mask if (getMetaType(schema).getMask("lza")): m = getMetaType(schema).getMask("lza") meta = l.LZAMetadata(m.getViewHTML([node], 8)) else: # generate error message meta = l.LZAMetadata(""" <?xpacket begin="\xef\xbb\xbf" id="mediatum_metadata"?> <lza:data> <lza:error>-definition missing-</lza:error> </lza:data><?xpacket end="w"?> """) archive.writeMediatumData(meta) node.addFile( tree.FileNode(archive.buildLZAName(), "lza", f.getMimeType())) except l.FiletypeNotSupported: v['error'] = "edit_lza_wrongfiletype" elif "removelza" in req.params: for f in node.getFiles(): if f.getType() == "lza": node.removeFile(f) v['id'] = req.params.get("id", "0") v['tab'] = req.params.get("tab", "") v['ids'] = ids v['nodes'] = nodes v['t'] = t v['language'] = lang(req) meta = {} for id in ids: node = tree.getNode(id) for f in node.getFiles(): if f.getType() == "lza": try: archive = l.LZA(f.retrieveFile(), f.getMimeType()) meta[id] = archive.getMediatumData() except IOError: v['error'] = "edit_lza_ioerror" v['meta'] = meta return req.getTAL("web/edit/modules/lza.html", v, macro="edit_lza")
def ln(nodeid): global node node.addChild(tree.getNode(nodeid))
def getContent(req, ids): ret = "" user = users.getUserFromRequest(req) access = acl.AccessData(req) for id in ids: if not access.hasWriteAccess( tree.getNode(id)) or "acls" in users.getHideMenusForUser(user): req.setStatus(httpstatus.HTTP_FORBIDDEN) return req.getTAL("web/edit/edit.html", {}, macro="access_error") idstr = ",".join(ids) if "save" in req.params: # save acl level userdir = users.getHomeDir(user) logging.getLogger('usertracing').info(access.user.name + " change access " + idstr) if req.params.get("type") == "acl": for type in acl_types: rights = req.params.get("left" + type, "").replace(";", ",") for id in ids: node = tree.getNode(id) error = 0 if access.hasWriteAccess(node) and userdir.id != node.id: node.setAccess(type, rights) else: error = 1 if error: req.setStatus(httpstatus.HTTP_FORBIDDEN) return req.getTAL("web/edit/edit.html", {}, macro="access_error") # save userlevel elif req.params.get("type") == "user": for type in acl_types: for id in ids: node = tree.getNode(id) error = 0 if access.hasWriteAccess(node) and userdir.id != node.id: r = [] r_acls = [] if req.params.get("leftuser" + type, "") != "": for right in req.params.get("leftuser" + type, "").split(";"): if len(right.split(": ")) == 2: r.append("(user " + right.split(": ")[1] + ")") else: r_acls.append(right) if len(r) > 0: rstr = "{" + " OR ".join(r) + "}" else: rstr = req.params.get( "leftuser" + type, "") if len(rstr) > 0: rstr += "," for x in r_acls: rstr += x + "," rstr = rstr[:-1] else: rstr = "" node.setAccess(type, rstr) else: error = 1 if error: req.setStatus(httpstatus.HTTP_FORBIDDEN) return req.getTAL("web/edit/edit.html", {}, macro="access_error") runsubmit = "\nfunction runsubmit(){\n" retacl = "" rights = {} parent_rights = {} overload = {} for type in acl_types: s = None parent_rights[type] = {} overload[type] = 0 runsubmit += "\tmark(document.myform.left" + type + ");\n" runsubmit += "\tmark(document.myform.leftuser" + type + ");\n" if type in ("read", "data"): overload[type] = 1 for id in ids: node = tree.getNode(id) r = node.getAccess(type) if r is None: r = "" log.debug(node.name + " " + type + " " + r) if not s or r == s: s = r else: s = "" def addNode(node): for p in node.getParents(): aclright = p.getAccess(type) for right in removeEmptyStrings((aclright or "").split(",")): parent_rights[type][right] = None if aclright and overload[type]: return else: addNode(p) addNode(node) rights[type] = removeEmptyStrings(s.split(",")) for type in acl_types: retacl += req.getTAL("web/edit/modules/acls.html", makeList(req, type, rights[type], parent_rights[type].keys(), overload[type], type=type), macro="edit_acls_selectbox") if "action" in req.params.keys(): # load additional rights by ajax retuser = "" for type in acl_types: retuser += req.getTAL("web/edit/modules/acls.html", makeUserList(req, type, rights[type], parent_rights[type].keys(), overload[type], type=type), macro="edit_acls_userselectbox") req.write(retuser) return "" runsubmit += "\tdocument.myform.submit();\n}\n" return req.getTAL("web/edit/modules/acls.html", { "runsubmit": runsubmit, "idstr": idstr, "contentacl": retacl, "adminuser": access.getUser().isAdmin() }, macro="edit_acls")
def getContent(req, ids): node = tree.getNode(ids[0]) user = users.getUserFromRequest(req) access = AccessData(req) if not access.hasWriteAccess( node) or "editor" in users.getHideMenusForUser(user): 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.getFiles() if f.mimetype == "text/html"]: filepath = f.retrieveFile().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, "r") as fil: data = fil.read() msg = "%s opened startpage %r for node %s (%r, %r)" % ( user.name, filepath, node.id, node.name, node.type) logger.info(msg) break req.write(json.dumps({'filecontent': data})) 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.getFiles() if f.type == "content"]: try: if int(f.retrieveFile()[:-5].split("_")[-1]) >= maxid: maxid = int( f.retrieveFile()[:-5].split("_")[-1]) + 1 except ValueError: pass 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 open(config.get("paths.datadir") + filename, "w") as fil: fil.write(req.params.get('data')) node.addFile(FileNode(filename, "content", "text/html")) req.write(json.dumps({'filename': '', 'state': 'ok'})) msg = "%s added startpage %r for node %s (%r, %r)" % ( user.name, filename, node.id, node.name, node.type) logger.info(msg) return None else: for f in [ f for f in node.getFiles() if f.mimetype == "text/html" ]: filepath = f.retrieveFile().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: fil.write(req.params.get('data')) req.write( json.dumps({ 'filesize': format_filesize( os.path.getsize( config.get("paths.datadir") + filepath)), 'filename': req.params.get('filename'), 'state': 'ok' })) msg = "%s saved startpage %r for node %s (%r, %r)" % ( user.name, filepath, node.id, node.name, node.type) logger.info(msg) break return None if "option" in req.params: if req.params.get("option") == "filebrowser": # open filebrowser msg = "%s opening ckeditor filebrowser for node %s (%r, %r)" % ( user.name, node.id, node.name, node.type) logger.info(msg) req.write(send_nodefile_tal(req)) return "" if req.params.get("option") == "htmlupload": # use fileupload msg = "%s going to use ckeditor fileupload (htmlupload) for node %s (%r, %r)" % ( user.name, node.id, node.name, node.type) logger.info(msg) req.write(upload_for_html(req)) return "" if "delete" in req.params: # delete file via CKeditor for f in node.getFiles(): if f.retrieveFile().endswith(req.params.get('option')): filepath = f.retrieveFile().replace( config.get("paths.datadir"), '') msg = "%s going to delete ckeditor filebrowser file %r for node %s (%r, %r)" % ( user.name, filepath, node.id, node.name, node.type) logger.info(msg) if os.path.exists(f.retrieveFile()): os.remove(f.retrieveFile()) node.removeFile(f) break 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) logger.info("%s removed file %r from disk" % (user.name, fullpath)) else: logger.warning( "%s could not remove file %r from disk: not existing" % (user.name, fullpath)) filenode = FileNode(page, "", "text/html") node.removeAttribute("startpagedescr." + file_shortpath) node.set( "startpage.selector", node.get("startpage.selector").replace(file_shortpath, "")) node.removeFile(filenode) logger.info( user.name + " - startpages - deleted FileNode and file for node %s (%s): %s, %s, %s, %s" % (node.id, node.name, page, filenode.getName(), filenode.type, filenode.mimetype)) except: logger.error( user.name + " - startpages - error while delete FileNode and file for " + page) logger.error("%s - %s" % (sys.exc_info()[0], sys.exc_info()[1])) 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") 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.getFiles(): if f.mimetype == 'text/html' and f.getType() in ['content']: filelist.append(f) languages = [ language.strip() for language in config.get("i18n.languages").split(",") ] if "startpages_save" in req.params.keys( ): # user saves startpage configuration msg = "%s going to save startpage configuration for node %s (%r, %r): %r" % ( user.name, node.id, node.name, node.type, req.params) logger.info(msg) 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.set('startpage' + k, req.params[k]) # build startpage_selector startpage_selector = "" for language in languages: startpage_selector += "%s:%s;" % ( language, req.params.get('radio_' + language)) node.set('startpage.selector', startpage_selector[0:-1]) named_filelist = [] for f in filelist: long_path = f.retrieveFile() 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 languages: spn = node.getStartpageFileNode(language) if spn and spn.retrieveFile() == long_path: langlist.append(language) if node.get('system.sidebar').find(language + ":" + short_path) >= 0: sidebar.append(language) named_filelist.append( (short_path, node.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 attribute startpage.selector # build startpage_selector and wriote back to node startpage_selector = "" for language in languages: if initial: lang2file[language] = named_filelist[0][0] else: lang2file[language] = lang2file.setdefault(language, '') startpage_selector += "%s:%s;" % (language, lang2file[language]) node.set('startpage.selector', startpage_selector[0:-1]) v = { "id": req.params.get("id", "0"), "tab": req.params.get("tab", ""), "node": node, "named_filelist": named_filelist, "languages": languages, "lang2file": lang2file, "types": ['content'], "d": lang2file and True } return req.getTAL("web/edit/modules/startpages.html", v, macro="edit_startpages")
def getContent(req, ids): user = users.getUserFromRequest(req) if "ftp" in users.getHideMenusForUser(user): req.setStatus(httpstatus.HTTP_FORBIDDEN) return req.getTAL("web/edit/edit.html", {}, macro="access_error") ids = ids[0] # use only first selected node node = tree.getNode(ids) error = "" def processFile(node, file, ftype): nname = file.retrieveFile().split("/") nname = "/".join(nname[:-1]) + "/" + nname[-1][4:] try: os.rename(file.retrieveFile(), nname) except: nname = file.retrieveFile() fnode = tree.Node(nname.split("/")[-1], ftype) node.removeFile(file) file._path = file._path.replace(config.get("paths.datadir"), "") file._path = "/".join( file._path.split("/")[:-1]) + "/" + fnode.getName() fnode.addFile(file) fnode.event_files_changed() node.addChild(fnode) return fnode for key in req.params.keys(): if key.startswith("process|"): # process selected file (single) fname = key[:-2].split("|")[-1] ftype = req.params.get("schema").replace(";", "") if ftype != "": for f in node.getFiles(): if f.getName() == fname: processFile(node, f, ftype) break break else: error = "edit_ftp_error1" elif key.startswith("del|"): for f in node.getFiles(): if f.getName() == key[4:-2]: node.removeFile(f) break break elif key.startswith("delall"): # delete all selected files delfiles = [ f.split("|")[-1] for f in req.params.get("selfiles").split(";") ] for f in node.getFiles(): if f.getName() in delfiles: node.removeFile(f) break elif key.startswith("processall"): # process all selected files for file in req.params.get("selfiles", "").split(";"): if file: ftype, fname = file.split("|") if "multschema|" + ftype in req.params and req.params.get( "multschema|" + ftype) != "": for f in node.getFiles(): if f.getName() == fname: print "use", ftype + "/" + req.params.get( "multschema|" + ftype) processFile( node, f, ftype + "/" + req.params.get("multschema|" + ftype)) break else: error = "edit_ftp_error2" break break files = filter(lambda x: x.getName().startswith("ftp_"), node.getFiles()) types = [] for f in files: if f.getType() not in types: if f.getType() != "other": types.append(f.getType()) dtypes = {} for scheme in filter(lambda x: x.isActive(), acl.AccessData(req).filter(loadTypesFromDB())): for dtype in scheme.getDatatypes(): if dtype not in dtypes.keys(): dtypes[dtype] = [] if scheme not in dtypes[dtype]: dtypes[dtype].append(scheme) for t in dtypes: dtypes[t].sort(lambda x, y: cmp( translate(x.getLongName(), request=req).lower(), translate(y.getLongName(), request=req).lower())) access = acl.AccessData(req) if not access.hasWriteAccess(node): req.setStatus(httpstatus.HTTP_FORBIDDEN) return req.getTAL("web/edit/edit.html", {}, macro="access_error") v = {} v['error'] = error v['files'] = files v['node'] = node v['schemes'] = dtypes # schemes v['usedtypes'] = types v['tab'] = req.params.get("tab", "") v['ids'] = ids v["script"] = "<script> parent.reloadTree('" + req.params.get( "id") + "');</script>" return req.getTAL("web/edit/modules/ftp.html", v, macro="edit_ftp")
def create(): """ Creates the sitemap files and the sitemap index files which are located at /web/root/ """ logging.getLogger('everything').info( 'Creating Sitemaps and Sitemap Index...') base_dir = os.path.abspath( os.path.join(os.path.dirname(__file__), os.pardir)) hostname = config.get('host.name') root = tree.getRoot('collections') all_nodes = root.getAllChildren() user = users.getUser('Gast') access = acl.AccessData(user=user) sitemaps = [] node_dict = { 'collection': [], 'directory': [], 'document': [], 'dissertation': [], 'image': [], 'video': [], 'audio': [], } for node in all_nodes: # Arkitekt had a guest field that is actually not visible if access.hasAccess(node, 'read'): for node_type in node_dict.keys(): if node_type in tree.getNode(node.id).type: node_dict[node_type].append( (node.id, tree.getNode(node.id).get('updatetime'))) # Reassign node_dict to a dict where empty values were removed node_dict = dict((k, v) for k, v in node_dict.iteritems() if v) # Sitemap can have at most 50k entries for key in node_dict.keys(): if key in ('dissertation', 'document', 'image'): priority_level = '1.0' elif key == 'videos': priority_level = '0.8' else: priority_level = '0.5' # Create multiple sitemaps for node lists > 50k if len(node_dict[key]) > 50000: partitions = int(ceil((len(node_dict[key]) / 50000.))) for partition_number in range(partitions): sitemap = Sitemap( base_dir, ''.join( ['sitemap-', str(key), str(partition_number), '.xml']), hostname) sitemaps.append(sitemap.name) sitemap.create_sitemap( node_dict[key][partition_number * 50000:(partition_number + 1) * 50000], priority_level) else: sitemap = Sitemap(base_dir, ''.join(['sitemap-', key, '.xml']), hostname) sitemaps.append(sitemap.name) sitemap.create_sitemap(node_dict[key], priority_level) siteindex = SitemapIndex(base_dir, 'sitemap-index.xml', hostname) now = '+'.join( [datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S'), '02:00']) siteindex.create_sitemap_index(sitemaps, now) logging.getLogger('everything').info( 'Generation of Sitemaps and SitemapIndex Complete')
def getOAIExportFormatsForIdentifier(identifier): try: node = tree.getNode(identifier2id(identifier)) except: return [] return getOAIExportFormatsForSchema(node.getSchema())
def feedback(self, req): Portlet.feedback(self, req) self.lang = lang(req) if "dir" in req.params or "id" in req.params: id = req.params.get("id", req.params.get("dir")) try: node = tree.getNode(id) if isCollection(node): self.collection = node self.directory = node else: if isDirectory(node): self.directory = node else: if not isDirectory(self.directory) or not isParentOf(node, self.directory): self.directory = getDirectory(node) if self.collection.type == "collections" or not isParentOf(node, self.collection): self.collection = getCollection(node) except tree.NoSuchNodeError: pass try: self.hide_empty = self.collection.get("style_hide_empty") == "1" except: self.hide_empty = False access = AccessData(req) # open all parents, so we see that node opened = {} parents = [self.directory] counter = 0 while parents: counter += 1 if counter > 50: raise RecursionException p = parents.pop() opened[p.id] = 1 parents += p.getParents() m = {} def f(m, node, indent, hide_empty): if indent > 15: raise RecursionException if not access.hasReadAccess(node): return count = -1 m[node.id] = e = NavTreeEntry(self, node, indent, node.type == "directory", hide_empty=hide_empty, lang=self.lang) if node.id in opened or e.defaultopen: m[node.id].folded = 0 for c in node.getContainerChildren(): if c.get("style_hide_empty") == "1": hide_empty = 1 f(m, c, indent + 1, hide_empty) f(m, tree.getRoot("collections"), 0, self.hide_empty) if "cunfold" in req.params: id = req.params["cunfold"] if id in m: m[id].folded = 0 if self.directory.id in m: m[self.directory.id].folded = 0 m[self.directory.id].active = 1 if self.collection.id in m: m[self.collection.id].active = 1 col_data = [] def f(col_data, node, indent): if indent > 15: raise RecursionException if node.id not in m: return data = m[node.id] col_data += [data] if not data.folded or data.defaultopen: for c in node.getContainerChildren().sort_by_orderpos(): f(col_data, c, indent + 1) f(col_data, tree.getRoot("collections"), 0) self.col_data = col_data
def getData(req): access = AccessData(req) pid = req.params.get("parentId") style = req.params.get("style", "edittree") ret = [] for c in tree.getNode(pid).getChildren().sort_by_orderpos(): if not access.hasReadAccess(c): continue try: if c.isContainer(): cnum = len(c.getContainerChildren()) inum = len(c.getContentChildren()) label = c.getLabel() title = c.getLabel() + " (" + str(c.id) + ")" cls = "folder" itemcls = "" if not access.hasWriteAccess(c): itemcls = "read" if c.type == "collection": # or "collection" in c.type: cls = "collection" if hasattr(c, 'treeiconclass'): cls = c.treeiconclass() if c.getName().startswith(translate('user_trash', request=req)): cls = "trashicon" elif c.getName().startswith(translate('user_upload', request=req)): cls = "uploadicon" elif c.getName().startswith(translate('user_import', request=req)): cls = "importicon" elif c.getName().startswith(translate('user_faulty', request=req)): cls = "faultyicon" elif c.getName().startswith(translate('user_directory', request=req)): cls = "homeicon" if style == "edittree": # standard tree for edit area if inum > 0: label += " <small>(" + str(inum) + ")</small>" ret.append('<li class="' + cls + '.gif" id="Node' + c.id + '">') ret.append('<a href="#" title="' + title + '" id="' + c.id + '" class="' + itemcls + '">' + label + '</a>') if cnum > 0: ret.append('<ul><li parentId="' + c.id + '" class="spinner.gif"><a href="#"> </a></li></ul>') ret.append('</li>') elif style == "classification": # style for classification ret.append('<li class="' + cls + '.gif" id="Node' + c.id + '">') ret.append('<a href="#" title="' + title + '" id="' + c.id + '" class="' + itemcls + '">' + label + ' <input type="image" src="/img/ftree/uncheck.gif"/></a>') if cnum > 0: ret.append('<ul><li parentId="' + c.id + '" class="spinner.gif"><a href="#"> </a></li></ul>') ret.append('</li>') except: pass req.write("\n".join(ret)) return
def action(req): global editModules access = AccessData(req) language = lang(req) user = users.getUserFromRequest(req) trashdir = users.getTrashDir(user) uploaddir = users.getUploadDir(user) faultydir = users.getFaultyDir(user) importdir = users.getImportDir(user) trashdir_parents = trashdir.getParents() action = req.params.get("action", "") changednodes = {} if not access.user.isEditor(): req.write("""permission denied""") req.setStatus(httpstatus.HTTP_FORBIDDEN) return if "tab" in req.params: tab = req.params.get("tab").split("_")[-1] return editModules[tab].getContent(req, [req.params.get("id")]) if action == "getlabels": nids = req.params.get('ids', []) nids = [nid.strip() for nid in nids.split(',') if nid.strip()] for nid in set(nids + [_n.id for _n in [trashdir, uploaddir, importdir, faultydir]]): try: changednodes[nid] = getTreeLabel( tree.getNode(nid), lang=language) except: msg = "could not make fancytree label for node %r" % nid logger.error(msg) res_dict = {'changednodes': changednodes} req.write(json.dumps(res_dict, indent=4)) return else: # all 'action's except 'getlabels' require a base dir (src) srcid = req.params.get("src") try: src = tree.getNode(srcid) except: req.writeTAL( "web/edit/edit.html", {"edit_action_error": srcid}, macro="edit_action_error") return if req.params.get('action') == 'addcontainer': node = tree.getNode(srcid) if not access.hasWriteAccess(node): # deliver errorlabel req.writeTALstr( '<tal:block i18n:translate="edit_nopermission"/>', {}) return # create new container newnode_type = req.params.get('type') if newnode_type in ['bare_collection', 'bare_directory']: newnode_type = newnode_type.replace('bare_', '') translated_label = t(lang(req), 'edit_add_' + newnode_type) if translated_label.startswith('edit_add_'): translated_label = t( lang(req), 'edit_add_container_default') + newnode_type newnode = node.addChild( tree.Node(name=translated_label, type=newnode_type)) newnode.set("creator", user.getName()) newnode.set("creationtime", str( time.strftime('%Y-%m-%dT%H:%M:%S', time.localtime(time.time())))) clearFromCache(node) req.params["dest"] = newnode.id # try: # label = newnode.getLabel() # except: # label = newnode.getName() # # c = len(newnode.getContentChildren()) # if c>0: # label += ' <small>(%s)</small>' %(c) label = getTreeLabel(newnode, lang=language) fancytree_nodedata = { 'title': label, 'key': newnode.id, 'isLazy': False, 'isFolder': True, 'icon': getEditorIconPath(newnode), 'readonly': 0, 'tooltip': '%s (%s)' % (label, newnode.id), 'children': [], } req.write(json.dumps(fancytree_nodedata)) msg = "%s adding new container %r (%r) to %r (%r, %r)" % ( access.user.name, newnode.id, newnode.type, node.id, node.name, node.type) logging.getLogger('usertracing').info(msg) logger.info(msg) return try: destid = req.params.get("dest", None) dest = tree.getNode(destid) folderid = destid except: destid = None dest = None folderid = srcid idlist = getIDs(req) mysrc = None errorobj = None # try: if action == "clear_trash": for n in trashdir.getChildren(): # if trashdir is it's sole parent, remove file from disk # attn: this will not touch files from children of deleted # containers if len(n.getParents()) == 1: logger.info("%s going to remove files from disk for node %r (%r, %r)" % ( user.getName(), n.id, n.name, n.type)) for f in n.getFiles(): # dangerous ??? check this f_path = f.retrieveFile() if os.path.exists(f_path): logger.info( "%s going to remove file %r from disk" % (user.getName(), f_path)) os.remove(f_path) trashdir.removeChild(n) dest = trashdir clearFromCache(trashdir) changednodes[trashdir.id] = 1 _parent_descr = [(p.name, p.id, p.type) for p in trashdir_parents] msg = "%s cleared trash folder with id %s, child of %r" % ( user.getName(), trashdir.id, _parent_descr) logger.info(msg) logging.getLogger('usertracing').info(msg) # return else: for id in idlist: obj = tree.getNode(id) mysrc = src if isDirectory(obj): mysrc = obj.getParents()[0] if action == "delete": if access.hasWriteAccess(mysrc) and access.hasWriteAccess(obj): if mysrc.id != trashdir.id: mysrc.removeChild(obj) changednodes[mysrc.id] = 1 trashdir.addChild(obj) changednodes[trashdir.id] = 1 clearFromCache(mysrc) logger.info("%s moved to trash bin %s (%r, %r) from %s (%r, %r)" % ( user.getName(), obj.id, obj.name, obj.type, mysrc.id, mysrc.name, mysrc.type)) logging.getLogger('usertracing').info("%s removed %s (%r, %r) from %s (%r, %r)" % ( user.getName(), obj.id, obj.name, obj.type, mysrc.id, mysrc.name, mysrc.type)) dest = mysrc else: logger.info( "%s has no write access for node %s" % (user.getName(), mysrc.id)) req.writeTALstr( '<tal:block i18n:translate="edit_nopermission"/>', {}) dest = mysrc elif action in ["move", "copy"]: if dest != mysrc and \ access.hasWriteAccess(mysrc) and \ access.hasWriteAccess(dest) and \ access.hasWriteAccess(obj) and \ isDirectory(dest): if not nodeIsChildOfNode(dest, obj): if action == "move": mysrc.removeChild(obj) changednodes[mysrc.id] = 1 # getLabel(mysrc) dest.addChild(obj) changednodes[dest.id] = 1 # getLabel(dest) clearFromCache(dest) _what = "%s %s %r (%r, %r) " % ( access.user.name, action, obj.id, obj.name, obj.type) _from = "from %r (%r, %r) " % ( mysrc.id, mysrc.name, mysrc.type) _to = "to %r (%r, %r)" % ( dest.id, dest.name, dest.type) msg = _what + _from + _to logging.getLogger('usertracing').info(msg) logger.info(msg) else: logger.error("%s could not %s %s from %s to %s" % ( user.getName(), action, obj.id, mysrc.id, dest.id)) else: return mysrc = None if not mysrc: mysrc = src if action in ["move", "copy", "delete", "clear_trash"]: for nid in changednodes: try: changednodes[nid] = getTreeLabel( tree.getNode(nid), lang=language) except: msg = "could not make fancytree label for node %r" % nid logger.error(msg) res_dict = {'changednodes': changednodes} req.write(json.dumps(res_dict, indent=4)) else: try: req.write(dest.id) except: req.write('no-node-id-specified (web.edit.edit.action)') logger.warning('no-node-id-specified (web.edit.edit.action)') return
def import_csl(record, target=None, name=None, testing=False): """Import data from a CSL record into a new node :param record: CSL record :type record: dict :param: target :type target: tree.Node :param name: name for the new node. If none, try to get a unique id from the record (DOI) or generate an UUID. :raises: NoMappingFound if no mapping defined for the given type """ typ = record["type"] if not typ: logg.warn("no type given in CSL import, using _null") typ = "_null" if typ not in TYPE_SET: logg.warn("unknown type %s given in CSL import, using _default", typ) typ = "_default" metatype_name = importbase.get_import_mapping("citeproc", typ) if not metatype_name: # no mapping, found, try fallback mapping metatype_name = importbase.get_import_mapping("citeproc", "_default") if not metatype_name: # no _default defined, fail raise NoMappingFound("No citeproc schema mapping could be found", typ) logg.warn("no mapping found for type %s, using _default fallback mapping", typ) metatype = getMetaType(metatype_name) mask = metatype.getMask("citeproc") if not mask: raise NoMappingFound("target schema does not have a citeproc mask", typ) contenttype = "document" if name is None: name = record.get("DOI") or "".join(random.choice('0123456789abcdef') for _ in xrange(16)) node = tree.Node(name, contenttype + "/" + metatype_name) def get_converted_from_csl_record(key): value = record.get(key) if value is None: return None try: if FIELDS[key].fieldtype == "date": return convert_csl_date(value) elif FIELDS[key].fieldtype == "name": return convert_csl_names(value) elif FIELDS[key].fieldtype == "number": if not check_number(value): logg.warn("field '%s' is of type number and contains an illegal value: '%s'!" "See http://citationstyles.org/downloads/specification.html#number" .key, value) # for number and standard fields return value.encode("utf8") except: # all malformed input will be ignored # XXX: improve this when we know better what can happen... logg.error("error while converting CSL field '%s' with value '%s', ignored", key, value, exc_info=1) return "" for f in mask.getMaskFields(): try: csl_name = "not defined" mfield = "not defined" med_name = "not defined" csl_name = tree.getNode(f.get("mappingfield")).getName() mfield = tree.getNode(f.get("attribute")) med_name = mfield.getName() except tree.NoSuchNodeError: msg = "citeproc import name='{}': field error for citeproc mask for type '{}' and " \ "csl-type '{}' csl_name='{}', mfield='{}', med_name='{}'".format(name, metatype_name, typ, csl_name, mfield, med_name) logg.error(msg, exc_info=1) continue value = get_converted_from_csl_record(csl_name) # fixes for special fields mfield_type = mfield.get("type") if mfield_type == "url": value += ";Link" if value is not None: node.set(med_name, value) if target: target.addChild(node) if not testing: node.setDirty() return node
def getContent(req, ids): user = users.getUserFromRequest(req) access = AccessData(user=user) language = lang(req) if "action" in req.params: state = 'ok' if req.params.get('action') == "removefiles": basenode = tree.getNode(req.params.get('id')) for f in basenode.getFiles(): try: os.remove(f.retrieveFile()) pass except: state = "error" for f in basenode.getFiles(): basenode.removeFile(f) req.write(json.dumps({'state': state})) return None if req.params.get('action') == "buildnode": # create nodes basenode = tree.getNode(req.params.get('id')) newnodes = [] errornodes = [] basenodefiles_processed = [] if req.params.get('uploader', '') == 'plupload': filename2scheme = {} for k in req.params: if k.startswith("scheme_"): filename2scheme[ k.replace('scheme_', '', 1)] = req.params.get(k) for f in basenode.getFiles(): filename = f.getName() if filename in filename2scheme: _m = getMimeType(filename) if _m[1] == "bibtex": # bibtex import handler try: nn = importBibTeX(f.retrieveFile(), basenode) newnodes.append(nn.id) basenodefiles_processed.append(f) except ValueError, e: errornodes.append((filename, str(e))) logger.debug("filename: %r, mimetype: %r" % (filename, _m)) logger.debug("__name__=%r, func=%r; _m=%r, _m[1]=%r" % (__name__, funcname(), _m, _m[1])) node_type = '%s/%s' % (_m[1], filename2scheme[filename]) n = tree.Node(filename, type=node_type) basenode.addChild(n) n.set("creator", user.getName()) n.set("creationtime", str(time.strftime('%Y-%m-%dT%H:%M:%S', time.localtime(time.time())))) n.addFile(f) n.event_files_changed() clearFromCache(n) newnodes.append(n.id) basenodefiles_processed.append(f) basenode.removeFile(f) msg_t = (user.getName(), n.id, n.name, n.type,filename, basenode.id, basenode.name, basenode.type) msg = "%s created new node id=%r (name=%r, type=%r) by uploading file %r, node is child of base node id=%r (name=%r, type=%r)" % msg_t logger.info(msg) logging.getLogger('usertracing').info(msg) else: for filename in req.params.get('files').split('|'): _m = getMimeType(filename) logger.debug("... in %s.%s: getMimeType(filename=%r)=%r" % (__name__, funcname(), filename, _m)) fs = basenode.getFiles() logger.debug("... in %s.%s: basenode.id=%r, basenode_files: %r" % (__name__, funcname(), basenode.id, [(x.getName(), x.retrieveFile()) for x in fs])) if _m[1] == req.params.get('type') or req.params.get('type') == 'file': for f in basenode.getFiles(): # ambiguity here ? if f.retrieveFile().endswith(filename): # bibtex import handler if _m[1] == "bibtex" and not req.params.get('type') == 'file': try: nn = importBibTeX(f.retrieveFile(), basenode) newnodes.append(nn.id) basenodefiles_processed.append(f) except ValueError, e: errornodes.append((filename, str(e))) else: logger.debug("creating new node: filename: %r" % filename) logger.debug("files at basenode: %r" % [(x.getName(), x.retrieveFile()) for x in basenode.getFiles()]) n = tree.Node(filename, type='%s/%s' % (req.params.get('type'), req.params.get('value'))) basenode.addChild(n) n.set("creator", user.getName()) n.set("creationtime", str(time.strftime('%Y-%m-%dT%H:%M:%S', time.localtime(time.time())))) # clones to a file with random name cloned_file = f.clone(None) n.addFile(cloned_file) if hasattr(n, 'event_files_changed'): n.event_files_changed() clearFromCache(n) newnodes.append(n.id) basenodefiles_processed.append(f) msg_t = (user.getName(), n.id, n.name, n.type, filename, basenode.id, basenode.name, basenode.type) msg = "%s created new node id=%r (name=%r, type=%r) by uploading file %r, node is child of base node id=%r (name=%r, type=%r)" % msg_t logger.info(msg) logging.getLogger('usertracing').info(msg) break # filename may not be unique new_tree_labels = [{'id': basenode.id, 'label': getTreeLabel(basenode, lang=language)}] for f in basenodefiles_processed: basenode.removeFile(f) f_path = f.retrieveFile() if os.path.exists(f_path): logger.debug("%s going to remove file %r from disk" % (user.getName(), f_path)) os.remove(f_path) mime = getMimeType(filename) scheme_type = {mime[1]: []} for scheme in getSchemes(req): if mime[1] in scheme.getDatatypes(): scheme_type[mime[1]].append(scheme) # break # standard file content = req.getTAL('web/edit/modules/upload.html', {'files': [filename], 'schemes': scheme_type}, macro="uploadfileok") res = json.dumps({'state': state, 'newnodes': newnodes, 'errornodes': errornodes, 'new_tree_labels': new_tree_labels, 'ret': content}) req.write(res) return None
def getContent(req, ids): """ The standard method, which has to be implemented by every module. It's called in edit.py, where all the modules will be identified. """ user = users.getUserFromRequest(req) access = acl.AccessData(req) node = tree.getNode(ids[0]) access_nobody = 'nicht Jeder' # first prove if the user has the required rights to call this module if 'sortfiles' in users.getHideMenusForUser( user) or not access.hasWriteAccess(node): req.setStatus(httpstatus.HTTP_FORBIDDEN) return req.getTAL('web/edit/edit.html', {}, macro='access_error') if node.isContainer(): nodes = ', '.join(node.getChildren().getIDs()) else: nodes = node.get('node.id') v = { 'msg': '', 'urn_institutionid': config.get('urn.institutionid'), 'urn_pubtypes': config.get('urn.pubtypes').split(';'), 'namespaces': config.get('urn.namespace').split(';'), 'user': user, 'nodes': nodes, 'type': req.params.get('id_type'), 'show_form': True, 'namespace': req.params.get('namespace'), 'urn_type': req.params.get('urn_type'), 'host': config.get('host.name'), 'creator': users.getUser(node.get('creator')) } if user.isAdmin(): if 'id_type' in req.params: if req.params.get('id_type') == 'hash': createHash(node) if req.params.get('id_type') == 'urn': createUrn(node, req.params.get('namespace'), req.params.get('urn_type')) if req.params.get('id_type') == 'doi': try: createDOI(node) except: return req.error(500, "doi was not successfully registered") if any(identifier in node.attributes for identifier in ('hash', 'urn', 'doi')): if not node.get('system.identifierdate'): node.set('system.identifierdate', date.now()) if node.get('system.identifierstate') != '2': node.set('system.identifierstate', '2') # add nobody rule if not set if node.getAccess('write') is None: node.setAccess('write', access_nobody) else: if access_nobody not in node.getAccess('write'): node.setAccess( 'write', ','.join( [node.getAccess('write'), access_nobody])) try: mailtext = req.getTAL( 'web/edit/modules/identifier.html', v, macro='generate_identifier_usr_mail_2') mail.sendmail( config.get('email.admin'), users.getUser(node.get('creator')).get('email'), 'Vergabe eines Idektifikators / Generation of an Identifier', mailtext) except mail.SocketError: logging.getLogger('backend').error( 'failed to send Autorenvertrag mail to user %s' % node.get('creator')) v['msg'] = t(lang(req), 'edit_identifier_mail_fail') if node.get('system.identifierstate') != '2': v['msg'] = t(lang(req), 'edit_identifier_state_0_1_admin') else: v['msg'] = t(lang(req), 'edit_identifier_state_2_admin') else: if pathutils.isDescendantOf(node, tree.getRoot('collections')): if not node.get('system.identifierstate'): if 'id_type' in req.params: try: # fetch autorenvertrag attachment = [] autorenvertrag_name = 'formular_autorenvertrag.pdf' autorenvertrag_path = os.path.join( config.get('paths.tempdir'), autorenvertrag_name) if not os.path.isfile(autorenvertrag_path): logging.getLogger('backend').error( "Unable to attach Autorenvergrag. Attachment file not found: '%s'" % autorenvertrag_path) raise IOError( 'Autorenvertrag was not located on disk at %s. Please send this error message to %s' % (autorenvertrag_path, config.get('email.admin'))) else: attachment.append( (autorenvertrag_path, 'Autorenvertrag.pdf')) # notify user mailtext_user = req.getTAL( 'web/edit/modules/identifier.html', v, macro='generate_identifier_usr_mail_1_' + lang(req)) mail.sendmail( config.get('email.admin'), user.get('email'), t(lang(req), 'edit_identifier_mail_title_usr_1'), mailtext_user, attachments_paths_and_filenames=attachment) # notify admin mailtext_admin = req.getTAL( 'web/edit/modules/identifier.html', v, macro='generate_identifier_admin_mail') mail.sendmail( config.get('email.admin'), config.get('email.admin'), 'Antrag auf Vergabe eines Identifikators', mailtext_admin) node.set('system.identifierstate', '1') # add nobody rule print node.getAccess('write') if node.getAccess('write') is None: node.setAccess('write', access_nobody) else: if access_nobody not in node.getAccess('write'): node.setAccess( 'write', ','.join([ node.getAccess('write'), access_nobody ])) except mail.SocketError: logging.getLogger('backend').error( 'failed to send identifier request mail') v['msg'] = t(lang(req), 'edit_identifier_mail_fail') else: v['msg'] = t(lang(req), 'edit_identifier_state_0_usr') if node.get('system.identifierstate') == '1': v['show_form'] = False v['msg'] = t(lang(req), 'edit_identifier_state_1_usr') else: v['show_form'] = False v['msg'] = t(lang(req), 'edit_identifier_state_published') v['hash_val'] = node.get('hash') v['urn_val'] = node.get('urn') v['doi_val'] = node.get('doi') # hides form if all identifier types are already set if all(idents != '' for idents in (v['hash_val'], v['urn_val'], v['doi_val'])): v['show_form'] = False v['msg'] = t(lang(req), 'edit_identifier_all_types_set') return req.getTAL('web/edit/modules/identifier.html', v, macro='set_identifier')
def getContent(req, ids): user = users.getUserFromRequest(req) node = tree.getNode(ids[0]) access = acl.AccessData(req) if not access.hasWriteAccess( node) or "changeschema" in users.getHideMenusForUser(user): req.setStatus(httpstatus.HTTP_FORBIDDEN) return req.getTAL("web/edit/edit.html", {}, macro="access_error") error = req.params.get("error") currentContentType = node.getContentType() try: currentSchema = node.type.split('/')[1] # string except: currentSchema = '' currentCategoryName = node.getCategoryName() currentTypeAlias = node.getTypeAlias() schemes = AccessData(req).filter(loadTypesFromDB()) _schemes = [] for scheme in schemes: if scheme.isActive(): _schemes.append(scheme) schemes = _schemes schemeNames2LongNames = {'': ''} for s in schemes: schemeNames2LongNames[s.getName()] = s.getLongName() try: currentSchemaLongName = schemeNames2LongNames[currentSchema] except KeyError: currentSchemaLongName = '' # find out which schema allows which datatype, and hence, # which overall data types we should display dtypes = [] datatypes = loadAllDatatypes() for scheme in schemes: for dtype in scheme.getDatatypes(): if dtype not in dtypes: for t in datatypes: if t.getName() == dtype and not elemInList( dtypes, t.getName()): dtypes.append(t) dtypes.sort(lambda x, y: cmp( translate(x.getLongName(), request=req).lower(), translate(y.getLongName(), request=req).lower())) admissible_objtypes = getTypes(datatypes) admissible_datatypes = [ n for n in admissible_objtypes if tree.Node('', n.name).getCategoryName() in ['document', 'image', 'video', 'audio'] ] admissible_containers = [ n for n in admissible_objtypes if tree.Node('', n.name).getCategoryName() in ['container'] ] admissible_objtypes.sort(lambda x, y: cmp( translate(x.getLongName(), request=req).lower(), translate(y.getLongName(), request=req).lower())) admissible_datatypes.sort(lambda x, y: cmp( translate(x.getLongName(), request=req).lower(), translate(y.getLongName(), request=req).lower())) admissible_containers.sort(lambda x, y: cmp( translate(x.getLongName(), request=req).lower(), translate(y.getLongName(), request=req).lower())) available_schemes = [ s for s in schemes if currentContentType in s.getDatatypes() ] # filter schemes for special datatypes if req.params.get("objtype", "") != "": _schemes = [] for scheme in schemes: if req.params.get("objtype", "") in scheme.getDatatypes(): _schemes.append(scheme) schemes = _schemes schemes.sort(lambda x, y: cmp( translate(x.getLongName(), request=req).lower(), translate(y.getLongName(), request=req).lower())) newObjectType = req.params.get("objtype") newSchema = req.params.get("schema") if not newSchema: newSchema = '' newType = newObjectType if newSchema: newType += '/' + newSchema oldType = currentContentType if currentSchema: oldType = oldType + '/' + currentSchema if newType != oldType: node.setTypeName(newType) msg = "%s changed node schema for node %s '%s' from '%s' to '%s'" % ( user.name, node.id, node.name, oldType, newType) logger.info(msg) logging.getLogger('usertracing').info(msg) node.setDirty() # cache clean / reload because object type changed tree.remove_from_nodecaches(node) node = tree.getNode(node.id) currentContentType = node.getContentType() currentSchema = newSchema currentSchemaLongName = schemeNames2LongNames[currentSchema] currentCategoryName = node.getCategoryName() currentTypeAlias = node.getTypeAlias() available_schemes = [ s for s in schemes if newObjectType in s.getDatatypes() ] isContainer = False if hasattr(node, "isContainer"): isContainer = node.isContainer() if "action" in req.params.keys(): if req.params.get("action").startswith("get_schemes_for_"): newObjectType = req.params.get("action").replace( "get_schemes_for_", "") available_schemes = [ s for s in schemes if newObjectType in s.getDatatypes() ] req.writeTAL("web/edit/modules/changeschema.html", { 'schemes': available_schemes, 'currentSchema': currentSchema }, macro="changeschema_selectscheme") return "" containers = getContainers(datatypes) d = {"id": req.params.get("id"), "error": error, "node": node} d['currentContentType'] = currentContentType d['currentSchema'] = currentSchema d['currentSchemaLongName'] = currentSchemaLongName d['currentCategoryName'] = currentCategoryName d['currentTypeAlias'] = currentTypeAlias d['isContainer'] = int(isContainer) d['nodes'] = [node] if currentContentType in [dtype.name for dtype in containers]: d['schemes'] = [] d['datatypes'] = admissible_containers # containers else: d['schemes'] = available_schemes d['datatypes'] = admissible_datatypes # dtypes return req.getTAL("web/edit/modules/changeschema.html", d, macro="changeschema_popup")
def search_form(req, id, message=None): node = tree.getNode(id) occur = node.getAllOccurences(AccessData(req)) ret = "" objtype = req.params.get("objtype", None) if objtype: req.session["lastobjtype"] = objtype else: try: objtype = req.session["lastobjtype"] except: pass if message: ret += req.getTAL("web/edit/modules/search.html", {"message": message}, macro="write_message") if "Reset" in req.params: try: del req.session["esearchvals"] except: pass try: searchvalues = req.session["esearchvals"] except: req.session["esearchvals"] = searchvalues = {} f = [] otype = None mtypelist = [] itemlist = {} for mtype, num in occur.items(): if num > 0 and mtype.getDescription(): if otype is None: otype = mtype if mtype.getSchema() not in itemlist and mtype.type.find( "directory") == -1: itemlist[mtype.getSchema()] = None mtypelist.append(mtype) if objtype == mtype.getSchema(): otype = mtype else: log.warning("Warning: Unknown metadatatype: " + mtype.getName()) formlist = [] if otype: for field in otype.getMetaFields(): if field.Searchfield() and field.getFieldtype() != "date": value = searchvalues.get( otype.getSchema() + "." + field.getName(), "") collection = tree.getRoot("collections") c = Context(field, value, width=640, name=field.getName(), collection=node, language=lang(req), user=getUserFromRequest(req)) field.searchitem = field.getSearchHTML(c) formlist.append([field, value]) if field.getFieldtype() == "list" or field.getFieldtype( ) == "mlist": f.append(field.getName()) script = '<script>\n l = Array("' + string.join(f, '", "') + '");' script += """ for (var i = 0; i < l.length; ++i) { obj = document.getElementById(l[i]); if (obj){ obj.selectedIndex=-1; } } </script>""" try: indexdate = date.format_date( date.parse_date(tree.getRoot().get("lastindexerrun")), "%Y-%m-%d %H:%M:%S") except: indexdate = None ctx = { "nodes": [node], "node": node, "occur": occur, "mtypelist": mtypelist, "objtype": objtype, "searchvalues": searchvalues.get(str(otype) + ".full", ""), "script": script, "indexdate": indexdate, "formlist": formlist } ret += req.getTAL("web/edit/modules/search.html", ctx, macro="search_form") return ret
def getContent(req, ids): if len(ids) > 0: ids = ids[0] node = tree.getNode(ids) access = acl.AccessData(req) if not node or node.getContentType() != "collections" or not access.hasWriteAccess(node): req.setStatus(httpstatus.HTTP_FORBIDDEN) return req.getTAL("web/edit/edit.html", {}, macro="access_error") if "action" in req.params: if req.params.get("action") == "translate": # translation req.writeTALstr('<tal:block tal:content="key" i18n:translate=""/>', {"key": req.params.get("key")}) if req.params.get("action") == "nodeselection": # tree popup for node selection def f(req, node, objnum, link, indent, type): access = acl.AccessData(req) indent *= 10 nodename = node.name try: nodename = node.getLabel() except: log.logException() if type == 1: link = req.makeSelfLink({"tree_unfold": "", "tree_fold": node.id}) + "#node{}".format(node.id) elif type == 2: link = req.makeSelfLink({"tree_unfold": node.id, "tree_fold": ""}) + "#node{}".format(node.id) v = {} v["id"] = str(node.id) v["type"] = type v["link1"] = link v["indent"] = indent + 10 v["nodename"] = nodename v["writeaccess"] = access.hasWriteAccess(node) return req.getTAL("web/edit/modules/frontendparts.html", v, macro="edit_frontendparts_nodeselection") content = writetree(req, tree.getRoot("collections"), f, "", openednodes=[], sessionkey="nodetree", omitroot=0) req.writeTAL("web/edit/modules/frontendparts.html", {"content": content}, macro="edit_frontendparts_nodepopup") if req.params.get("action") == "iconselection": # image popup for image selection icons = [] for p in athana.getFileStorePaths("/img/"): for root, dirs, files in os.walk(os.path.join(config.basedir, p)): for name in [f for f in files if (f.endswith(".gif") or f.endswith(".png") or f.endswith(".jpg"))]: if "CVS" not in root and not "web/admin/img" in root and not "web/edit/img" in root: try: pic = Image.open(root + name) dimension = "%sx%spx" % (pic.size) icons.append((name, dimension)) except: pass req.writeTAL("web/edit/modules/frontendparts.html", {"icons": icons}, macro="edit_frontendparts_iconpopup") return "" if "do_action" in req.params: c = do_action(req, node) if c != "": return c v = {} v["id"] = node.id v["header_content"] = req.getTAL( "web/edit/modules/frontendparts.html", {"items": node.getCustomItems("header"), "type": "header"}, macro="frontendparts_section") v["footer_left_content"] = req.getTAL("web/edit/modules/frontendparts.html", {"items": node.getCustomItems("footer_left"), "type": "footer_left"}, macro="frontendparts_section") v["footer_right_content"] = req.getTAL("web/edit/modules/frontendparts.html", {"items": node.getCustomItems("footer_right"), "type": "footer_right"}, macro="frontendparts_section") return req.getTAL("web/edit/modules/frontendparts.html", v, macro="edit_frontendparts")
def main(): """ This script attempts to give an overview of the completeness and correctness of the search databases. The report will be generated by the script and sent to the emails which are set in the EMAIL_RECIPIENTS variable. """ t = time.time() published_nodes = set([ int(node.id) for node in tree.getNode(604993).getAllChildren() if hasattr(node, 'isContainer') and not node.isContainer() ]) for schema in searcher.schemas: published_schema = published_nodes.intersection( set([ int(nid[0]) for nid in tree.db.runQuery( """select distinct(id) from node where type like "%{}%" """ .format(schema)) ])) if len(published_schema) == 0: search_full_ratio = 0 search_text_ratio = 0 search_ext_ratio = 0 search_ext_field_ratio = 0 else: search_full_ratio = len( published_schema.intersection( set([ int(nid[0]) for nid in searcher.execute( get_search_sql('full_indexed'), schema, 'full') ]))) / len(published_schema) search_text_ratio = len( published_schema.intersection( set([ int(nid[0]) for nid in searcher.execute( get_search_sql('text_indexed'), schema, 'text') ]))) / len(published_schema) ext_content = searcher.execute( get_search_sql('ext_content') % (', '.join(['id'] + [ 'field' + db_field.position for db_field in get_schema_fields(schema) ])), schema, 'ext') if not ext_content: ext_processed = {} else: ext_processed = { node[0]: node[1:] for node in ext_content if int(node[0]) in published_schema } search_ext_ratio = len(ext_processed) / len(published_schema) ext_field_ratios = [] for nid in ext_processed: ext_field_ratios.append( get_extended_field_ratio(schema, tree.getNode(nid), ext_processed[nid])) if len(ext_field_ratios) == 0: search_ext_field_ratio = 0 else: search_ext_field_ratio = sum(ext_field_ratios) / len( ext_field_ratios) schema_summary[schema] = { 'ext': { 'ratio': search_ext_ratio, 'field_ratio': search_ext_field_ratio }, 'full': search_full_ratio, 'text': search_text_ratio } result = all_schemas_summary() print '%s sec to complete' % (time.time() - t) print result try: mailtext = result mail.sendmail('*****@*****.**', MAIL_RECIPIENTS, 'Schema Analysis is Finished', mailtext) except mail.SocketError: logging.getLogger('backend').error( 'failed to send Schema Analysis Results')
def deleteMappingField(name): node = tree.getNode(name) for p in node.getParents(): if p.type == "mapping": p.removeChild(node) return
def getGroup(id): if id.isdigit(): return tree.getNode(id) else: groups = tree.getRoot("usergroups") return groups.getChild(id)
def getContent(req, ids): user = users.getUserFromRequest(req) node = tree.getNode(ids[0]) access = acl.AccessData(req) if not access.hasWriteAccess(node) or "admin" in users.getHideMenusForUser( user): req.setStatus(httpstatus.HTTP_FORBIDDEN) return req.getTAL("web/edit/edit.html", {}, macro="access_error") if req.params.get('action') == 'getsearchdata': req.writeTAL("web/edit/modules/admin.html", { 'searchdata': node.search('searchcontent=%s' % node.id), 'node': node }, macro="searchdata") return '' if req.params.get("type", "") == "addattr" and req.params.get( "new_name", "") != "" and req.params.get("new_value", "") != "": node.set(req.params.get("new_name", ""), req.params.get("new_value", "")) logging.getLogger('editor').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() logging.getLogger('editor').info( "localread attribute of node %s updated" % node.id) break # set current node 'dirty' (reindex for search) if key.startswith("set_dirty"): node.setDirty() logging.getLogger('editor').info("set node %s dirty" % node.id) if node.isContainer(): for child_node in node.getChildren(): child_node.setDirty() logging.getLogger('editor').info("set node %s dirty" % child_node.id) break # delete node from cache (e.g. after changes in db) if key.startswith("del_cache"): for n in node.getAllChildren(): remove_from_nodecaches(n) break # remove attribute if key.startswith("attr_"): node.removeAttribute(key[5:-2]) logging.getLogger('editor').info( "attribute %s of node %s removed" % (key[5:-2], node.id)) break fields = node.getType().getMetaFields() fieldnames = [] for field in fields: fieldnames += [field.name] attrs = node.items() metafields = {} technfields = {} obsoletefields = {} tattr = {} try: tattr = node.getTechnAttributes() except AttributeError: pass tattr = formatTechAttrs(tattr) for key, value in attrs: if key in fieldnames: metafields[key] = formatdate(value, getFormat(fields, key)) elif key in tattr.keys(): technfields[key] = formatdate(value) else: obsoletefields[key] = value # remove all technical attributes if req.params.get("type", "") == "technical": for key in technfields: node.removeAttribute(key) technfields = {} logging.getLogger('editor').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, "fields": fields, "technfields": technfields, "tattr": tattr, "fd": formatdate, "gf": getFormat, "adminuser": user.isAdmin(), "canedit": access.hasWriteAccess(node) }, macro="edit_admin_file")
def handle_request(req): errors = [] user = users.getUserFromRequest(req) access = AccessData(req) if not PYPDF_MODULE_PRESENT: return if req.path.startswith("/serve_page/"): node_id = req.params.get("node_id", None) if node_id: try: node = tree.getNode(node_id) except: return 404 # not found else: return 404 # not found current_workflow = getNodeWorkflow(node) current_workflow_step = getNodeWorkflowStep(node) if not current_workflow_step: return 404 # not found current_workflow_step_children_ids = [n.id for n in current_workflow_step.getChildren()] if node.id not in current_workflow_step_children_ids: return 403 # forbidden if False: # and not access.hasAccess(node, "read"): req.params["addpic2pdf_error"] = "%s: %s" % ( format_date().replace('T', ' - '), t(lang(req), "admin_wfstep_addpic2pdf_no_access")) msg = "workflow step addpic2pdf(%s): no access to node %s for request from user '%s' (%s)" % ( current_workflow_step.id, str(node.id), user.name, str(req.ip)) logging.getLogger("backend").info(msg) return 403 # forbidden if req.path == '/serve_page/document.pdf': filepath = [f.retrieveFile() for f in node.getFiles() if f.getType().startswith('document')][0] return_code, file_size, abspath = serve_file(req, filepath) return return_code if req.path == '/serve_page/p_document.pdf': filepath = ( [f.retrieveFile() for f in node.getFiles() if f.getType().startswith('p_document') and f.getName().startswith( 'addpic2pdf_%s_node_%s_' % (str(current_workflow_step.id), str(node.id), )) and f.type.startswith('p_document')] + [f.retrieveFile() for f in node.getFiles() if f.getType().startswith('document')] )[0] return_code, file_size, abspath = serve_file(req, filepath) return return_code pageno = req.path.replace("/serve_page/", "") pageno = pageno.split('?')[0] pdf_in_filepath = getPdfFilepathForProcessing(current_workflow_step, node) pdf_page_image_fullpath = get_pdf_page_image(pdf_in_filepath, pageno) return_code, file_size, abspath = read_serve_file(req, pdf_page_image_fullpath, remove_after_sending=True) return return_code if req.path.startswith("/grid"): pdf_w = float(req.params.get('pdf_w', 595.275)) pdf_h = float(req.params.get('pdf_h', 841.890)) thumb_w = float(req.params.get('thumb_w', 424)) thumb_h = float(req.params.get('thumb_h', 600)) dpi_w = float(req.params.get('dpi_w', 72.0)) dpi_h = float(req.params.get('dpi_h', 72.0)) thick = int(req.params.get('thick', 5)) orig = req.params.get('orig', "bottom_left") rotate = int(req.params.get('rotate', "0")) pdf_size = (pdf_w, pdf_h) thumb_size = (thumb_w, thumb_h) dpi = (dpi_w, dpi_h) orig_message = t(lang(req), "admin_wfstep_addpic2pdf_grid_origin_label") f = getGridBuffer(pdf_size, thumb_size, dpi, thick=5, orig=["top_left", "bottom_left"][1], orig_message=orig_message, rotate=rotate) s = f.getvalue() req.write(s) req.write('') req.reply_headers['Content-Type'] = "image/png" return 200 # part handle_request not matched by "/serve_page/" and "/grid" nodeid = req.params.get("selection_name", None) node = None if nodeid: nodeid = nodeid.replace("pdfpage_select_for_node_", "") try: node = tree.getNode(nodeid) except: msg = "workflowstep addpic2pdf: nodeid='%s' for non-existant node for upload from '%s'" % (str(nodeid), str(req.ip)) errors.append(msg) logging.getLogger("backend").error(msg) return 404 # not found else: msg = "workflowstep addpic2pdf: could not find 'nodeid' for upload from '%s'" % str(req.ip) errors.append(msg) logging.getLogger("backend").error(msg) return 404 # not found try: current_workflow = getNodeWorkflow(node) current_workflow_step = getNodeWorkflowStep(node) except: return 403 # forbidden if False: # not access.hasAccess(node, "read"): req.params["addpic2pdf_error"] = "%s: %s" % (format_date().replace('T', ' - '), t(lang(req), "admin_wfstep_addpic2pdf_no_access")) msg = "workflow step addpic2pdf(%s): no access to node %s for request from user '%s' (%s)" % ( current_workflow_step.id, str(node.id), user.name, str(req.ip)) logging.getLogger("backend").info(msg) return 403 # forbidden pdf_in_filepath = getPdfFilepathForProcessing(current_workflow_step, node) pdf_page_image_url = get_pdf_page_image(pdf_in_filepath, req.params.get("pageno", "0"), path_only=True) s = {'pdf_page_image_url': pdf_page_image_url} req.write(req.params.get("jsoncallback") + "(%s)" % json.dumps(s, indent=4)) return 200