def showdir(req, node, publishwarn="auto", markunpublished=0, nodes=[], sortfield_from_req=None): if publishwarn == "auto": user = users.getUserFromRequest(req) homedir = getHomeDir(user) homedirs = getAllSubDirs(homedir) publishwarn = node in homedirs if not nodes: nodes = node.getChildren() if sortfield_from_req and sortfield_from_req is not None: msg = "%r, %r, sorted by sortfield_from_req=%r" % ( __name__, funcname(), sortfield_from_req) logger.debug(msg) elif sortfield_from_req is None: collection_sortfield = node.get("sortfield") if collection_sortfield: nodes = tree.NodeList([n.id for n in nodes ]).sort_by_fields([collection_sortfield]) msg = "%r, %r, sorted by collection_sortfield=%r" % ( __name__, funcname(), collection_sortfield) logger.debug(msg) else: msg = "%r, %r, *not* sorted" % (__name__, funcname()) logger.debug(msg) nodes = tree.NodeList([n.id for n in nodes if not n.type == 'shoppingbag']) return shownodelist(req, nodes, publishwarn=publishwarn, markunpublished=markunpublished, dir=node)
def filter(self, nodelist, accesstype="read"): if accesstype != "read": return self.filter_old(nodelist, accesstype) if self.user.isAdmin(): return nodelist if len(nodelist) and isinstance(nodelist[0], type("")): # convert list of ids to list of nodes nodelist = tree.NodeList(nodelist) t1 = time.time() print "filtering..." newlist = [] for node in nodelist: l = node.getLocalRead() for clause in l.split(","): if clause not in self.allowed_rules: rule = getRule(clause) self.allowed_rules[clause] = rule.getParsedRule().has_access(self, node) if self.allowed_rules[clause]: newlist += [node.id] break t2 = time.time() print "done, %.4f seconds" % (t2 - t1) return tree.NodeList(newlist)
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 search(query): global node nodelist = tree.NodeList(node.search(query)) print "%s matches" % len(nodelist) i = 0 for n in nodelist: print "Node %s %s" % (n.id, n.getName()) i += 1 if i > 10: print "..." break
def getNodesForSetSpec(self, setspec, schemata): if self.func_getNodesForSetSpec: return self.func_getNodesForSetSpec(self, setspec, schemata) elif setspec in self.d_queries: from .oaisearchparser import OAISearchParser osp = OAISearchParser() res = osp.parse(self.d_queries[setspec]).execute() if setspec in self.d_filters: nodefilter = self.d_filters[setspec] nodelist = tree.NodeList(res) # for i, n in enumerate(nodelist): # print i, n.id, n.name, n.type, n res = [n.id for n in nodelist if nodefilter(setspec, n)] return res else: logging.getLogger('oai').error( "OAI: Error: no function 'getNodesForSetSpec' found for setSpec='%s', returning empty list" % setspec) return []
def build_container_group(): # sets configured with container attributes node_list = tree.NodeList(tree.getNodesByAttribute('oai.setname', '*')) node_list = node_list.sort_by_fields(field="oai.setname") node_list = [ node for node in node_list if node.type in ['collection', 'directory'] ] node_list = [node for node in node_list if node.get('oai.setname').strip()] node_list = [node for node in node_list if node.get('oai.formats').strip()] d_names = OrderedDict() for col_node in node_list: d_names[str(col_node.id)] = esc(col_node.get('oai.setname')) g = OAISetGroup(d_names, descr='group of %d container sets' % len(d_names)) g.func_getNodesForSetSpec = func_getNodesForSetSpec g.func_getSetSpecsForNode = func_getSetSpecsForNode g.sortorder = '040' g.group_identifier = 'oaigroup_containers' return g
def getNodes(req): global tokenpositions, CHUNKSIZE access = acl.AccessData(req) nodes = None if "resumptionToken" in req.params: token = req.params.get("resumptionToken") if token in tokenpositions: pos, nodes, metadataformat = tokenpositions[token] else: return None, "badResumptionToken", None if not checkParams(req, ["verb", "resumptionToken"]): OUT("OAI: getNodes: additional arguments (only verb and resumptionToken allowed)" ) return None, "badArgument", None else: token, metadataformat = new_token(req) if not checkMetaDataFormat(metadataformat): OUT('OAI: ListRecords: metadataPrefix missing', 'error') return None, "badArgument", None pos = 0 if not nodes: string_from, string_to = None, None try: string_from = req.params["from"] date_from = parseDate(string_from) if date_from.year < EARLIEST_YEAR: date_from = date.DateTime(0, 0, 0, 0, 0, 0) except: if "from" in req.params: return None, "badArgument", None date_from = None try: date_to = parseDate(req.params["until"]) string_to = req.params.get("until") if not date_to.has_time: date_to.hour = 23 date_to.minute = 59 date_to.second = 59 if date_to.year < EARLIEST_YEAR - 1: raise except: if "until" in req.params: return None, "badArgument", None date_to = None setspec = None if "set" in req.params: setspec = req.params.get("set") if string_from and string_to and (string_from > string_to or len(string_from) != len(string_to)): return None, "badArgument", None try: nodes = retrieveNodes(req, access, setspec, date_from, date_to, metadataformat) nodes = [n for n in nodes if not parentIsMedia(n)] # filter out nodes that are inactive or older versions of other nodes nodes = [n for n in nodes if n.isActiveVersion()] except tree.NoSuchNodeError: # collection doesn't exist return None, "badArgument", None with token_lock: tokenpositions[token] = pos + CHUNKSIZE, nodes, metadataformat tokenstring = '<resumptionToken expirationDate="' + ISO8601(date.now().add(3600 * 24)) + '" ' + \ 'completeListSize="' + str(len(nodes)) + '" cursor="' + str(pos) + '">' + token + '</resumptionToken>' if pos + CHUNKSIZE >= len(nodes): tokenstring = None with token_lock: del tokenpositions[token] OUT( req.params.get('verb') + ": set=" + str(req.params.get('set')) + ", " + str(len(nodes)) + " objects, format=" + metadataformat) res = tree.NodeList(nodes[pos:pos + CHUNKSIZE]) if DEBUG: timetable_update( req, "leaving getNodes: returning %d nodes, tokenstring='%s', metadataformat='%s'" % (len(res), tokenstring, metadataformat)) return res, tokenstring, metadataformat
def retrieveNodes(req, access, setspec, date_from=None, date_to=None, metadataformat=None): schemata = [] if metadataformat == 'mediatum': metadatatypes = tree.getRoot('metadatatypes').getChildren() schemata = [ m.name for m in metadatatypes if m.type == 'metadatatype' and m.name not in ['directory', 'collection'] ] elif metadataformat: mdt_masks_list = getExportMasks("oai_" + metadataformat.lower() + "$") # check exact name schemata = [x[0][1] for x in mdt_masks_list if x[1]] if DEBUG: timetable_update( req, "in retrieveNodes: find schemata with export mask for metadata type %s (%d found: '%s')" % (metadataformat.lower(), len(schemata), str([x for x in schemata]))) if setspec: res = oaisets.getNodes(setspec, schemata) else: osp = OAISearchParser() query = " or ".join(["schema=%s" % schema for schema in schemata]) res = osp.parse(query).execute() res = tree.NodeList(res) if DEBUG: timetable_update( req, "in retrieveNodes: after building NodeList for %d nodes" % (len(res))) if date_from: res = [n for n in res if n.get(DATEFIELD) >= str(date_from)] if DEBUG: timetable_update( req, "in retrieveNodes: after filtering date_from --> %d nodes" % (len(res))) if date_to: res = [n for n in res if n.get(DATEFIELD) <= str(date_to)] if DEBUG: timetable_update( req, "in retrieveNodes: after filtering date_to --> %d nodes" % (len(res))) if access: res = access.filter(res) if DEBUG: timetable_update( req, "in retrieveNodes: after access filter --> %d nodes" % (len(res))) collections = tree.getRoot('collections') res = [n for n in res if isDescendantOf(n, collections)] if DEBUG: timetable_update( req, "in retrieveNodes: after checking descendance from basenode --> %d nodes" % (len(res))) if schemata: res = [n for n in res if n.getSchema() in schemata] if DEBUG: timetable_update( req, "in retrieveNodes: after schemata (%s) filter --> %d nodes" % (str(schemata), len(res))) if metadataformat and metadataformat.lower() in FORMAT_FILTERS.keys(): format_string = metadataformat.lower() res = [n for n in res if filterFormat(n, format_string)] if DEBUG: timetable_update( req, "in retrieveNodes: after format (%s) filter --> %d nodes" % (format_string, len(res))) return res
def concatNodeLists(nodelist1, nodelist2): try: return tree.NodeList(list(set(nodelist1.getIDs() + nodelist2.getIDs()))) except: return tree.NodeList(list(set(nodelist1.getIDs() + list(nodelist2))))
def edit_tree(req): access = AccessData(req) language = lang(req) user = users.getUserFromRequest(req) home_dir = users.getHomeDir(user) match_result = '' match_error = False if req.params.get('key') == 'root': nodes = core.tree.getRoot( 'collections').getContainerChildren().sort_by_orderpos() elif req.params.get('key') == 'home': if not user.isAdmin(): nodes = [home_dir] else: homenodefilter = req.params.get('homenodefilter', '') if homenodefilter: nodes = [] try: pattern = re.compile(homenodefilter) nodes = tree.getRoot('home').getContainerChildren().sort_by_orderpos() # filter out shoppingbags etc. nodes = [n for n in nodes if n.isContainer()] # filter user name - after first "(" nodes = filter(lambda n: re.match(homenodefilter, n.getLabel(language).split('(', 1)[-1]), nodes) match_result = '#=%d' % len(nodes) except Exception as e: logger.warning('pattern matching for home nodes: %r' % e) match_result = '<span style="color:red">Error: %r</span>' % str(e) match_error = True if home_dir not in nodes: if not match_error: match_result = '#=%d+1' % len(nodes) nodes.append(home_dir) nodes = tree.NodeList(nodes).sort_by_orderpos() else: nodes = [home_dir] else: nodes = core.tree.getNode( req.params.get('key')).getContainerChildren().sort_by_orderpos() # filter out shoppingbags etc. nodes = [n for n in nodes if n.isContainer()] data = [] # wn 2014-03-14 # special directories may be handled in a special way by the editor special_dir_ids = {} special_dir_ids[home_dir.id] = 'userhomedir' for dir_type in ['upload', 'import', 'faulty', 'trash']: special_dir_ids[users.getSpecialDir(user, dir_type).id] = dir_type spec_dirs = ['userhomedir', 'upload', 'import', 'faulty', 'trash'] spec_dir_icons = ["homeicon.gif", "uploadicon.gif", "importicon.gif", "faultyicon.gif", "trashicon.gif"] for node in nodes: if not access.hasReadAccess(node): continue # try: # label = node.getLabel() # except: # label = node.getName() # # c = len(node.getContentChildren()) # if c>0: # label += ' <small>(%s)</small>' %(c) label = getTreeLabel(node, lang=language) nodedata = {'title': label, 'key': node.id, 'lazy': True, 'folder': True, 'readonly': 0, 'tooltip': '%s (%s)' % (node.getLabel(lang=language), node.id)} nodedata['icon'] = getEditorIconPath(node, req) if len(node.getContainerChildren()) == 0: nodedata['lazy'] = False nodedata['children'] = [] if not access.hasWriteAccess(node): if req.params.get('key') == 'home': continue nodedata['readonly'] = 1 nodedata['noLink'] = True nodedata['extraClasses'] = 'readonly' # fancytree else: nodedata['readonly'] = 0 nodedata['this_node_is_special'] = [] if node.id in special_dir_ids: nodedata['this_node_is_special'] = nodedata[ 'this_node_is_special'] + [special_dir_ids[node.id]] if node.id == home_dir.id: nodedata['match_result'] = match_result data.append(nodedata) return req.write(json.dumps(data, indent=4))
def show_shoppingbag(req, msg=""): """open shoppingbag and show content""" img = False doc = False media = False (width, height) = calculate_dimensions(req) v = {"width": width, "height": height} f = [] # deliver image dimensions of original def calc_dim(file): ret = "" try: w = int(file.get("width")) h = int(file.get("height")) except: return "padding:0px 0px;width:90px;height:90px;" if w > h: factor = 90.0 / w h = h * 90.0 / w w = 90 ret += 'padding:%spx 0px;' % str(int((90 - h) / 2)) else: w = w * 90.0 / h h = 90 ret += 'padding:0px %spx;' % str(int((90 - w) / 2)) return ret + 'width:%spx;height:%spx;' % (str(int(w)), str(int(h))) # deliver document file size def calc_size(file): for f in file.getFiles(): if f.getType() == "document": return format_filesize(f.getSize()) return "" def calc_length(file): try: return file.getDuration() except: return "" access = AccessData(req) sb = req.session.get("shoppingbag", []) sb = [nid for nid in sb if nid.strip()] for node in access.filter(tree.NodeList(sb)): if node.getCategoryName() == "image": img = True if node.getCategoryName() == "document": doc = True if node.getCategoryName() in ["audio", "video"]: media = True f.append(node) if len(f) != len(req.session.get("shoppingbag", [])) and msg == "": msg = "popup_shoppingbag_items_filtered" v["files"] = f v["image"] = img v["document"] = doc v["media"] = media v["img_perc_range"] = range(1, 11) v["img_pix_sizes"] = ["1600x1200", "1280x960", "1024x768", "800x600"] v["calc_dim"] = calc_dim v["calc_size"] = calc_size v["calc_length"] = calc_length user = users.getUserFromRequest(req) v["shoppingbags"] = user.getShoppingBag() v["user"] = user v["msg"] = msg req.writeTAL(theme.getTemplate("shoppingbag.html"), v, macro="shoppingbag") return httpstatus.HTTP_OK
def getContent(req, ids): def getSchemes(req): schemes = AccessData(req).filter(loadTypesFromDB()) return filter(lambda x: x.isActive(), schemes) ret = "" v = {"message": ""} if len(ids) >= 0: ids = ids[0] v["id"] = ids if "do_action" in req.params.keys(): # process nodes fieldname = req.params.get("fields") old_values = u(req.params.get("old_values", "")).split(";") new_value = u(req.params.get("new_value")) basenode = tree.getNode(ids) entries = getAllAttributeValues(fieldname, req.params.get("schema")) c = 0 for old_val in old_values: for n in AccessData(req).filter(tree.NodeList(entries[old_val])): try: n.set( fieldname, replaceValue(n.get(fieldname), u(old_val), u(new_value))) n.setDirty() c += 1 except: pass v["message"] = req.getTAL("web/edit/modules/manageindex.html", {"number": c}, macro="operationinfo") if "style" in req.params.keys(): # load schemes if req.params.get("action", "") == "schemes": v["schemes"] = getSchemes(req) req.writeTAL("web/edit/modules/manageindex.html", v, macro="schemes_dropdown") return "" elif req.params.get( "action", "").startswith("indexfields__"): # load index fields schema = getMetaType(req.params.get("action", "")[13:]) fields = [] for field in schema.getMetaFields(): if field.getFieldtype() == "ilist": fields.append(field) v["fields"] = fields v["schemaname"] = schema.getName() req.writeTAL("web/edit/modules/manageindex.html", v, macro="fields_dropdown") return "" elif req.params.get("action", "").startswith( "indexvalues__"): # load values of selected indexfield node = tree.getNode(ids) fieldname = req.params.get("action").split("__")[-2] schema = req.params.get("action").split("__")[-1] v["entries"] = [] if node: v["entries"] = getAllAttributeValues(fieldname, schema) v["keys"] = v["entries"].keys() v["keys"].sort(lambda x, y: cmp(x.lower(), y.lower())) req.writeTAL("web/edit/modules/manageindex.html", v, macro="fieldvalues") return "" elif req.params.get("action", "").startswith( "children__"): # search for children of current collection scheme = req.params.get("action", "").split("__")[1] fieldname = req.params.get("action", "").split("__")[2] values = req.params.get("action", "").split("__")[3].split(";")[:-1] all_values = getAllAttributeValues(fieldname, scheme) def isChildOf(access, node, basenodeid): for ls in getPaths(node, access): if str(basenodeid) in tree.NodeList(ls).getIDs(): return 1 return 0 subitems = {} for value in values: value = u(value) if value in all_values: subitems[value] = [] for l in all_values[value]: if isChildOf(AccessData(req), tree.getNode(l), ids): subitems[value].append(l) v["items"] = subitems v["keys"] = subitems.keys() v["keys"].sort() req.writeTAL("web/edit/modules/manageindex.html", v, macro="valueinfo") return "" else: return req.getTAL("web/edit/modules/manageindex.html", v, macro="manageform")
def isParent__(basenode, node, access): return 1 for ls in getPaths(node, access): if str(basenode.id) in tree.NodeList(ls).getIDs(): return 1 return 0
def isChildOf(access, node, basenodeid): for ls in getPaths(node, access): if str(basenodeid) in tree.NodeList(ls).getIDs(): return 1 return 0