def search_nodes(query, mapping_prefix='Z3950_search_'): """ Search nodes that match the query. 'query' is a tree of QueryBoolNode and QueryMatchNode objects. Query root nodes are configured by a naming convention. The names of mappings that starting with the given 'mapping_prefix' must end with a node ID, which is then used as root node for the search based on that field mapping. """ # find root nodes and their mappings roots_and_mappings = [] for mapping_node in mapping.getMappings(): name = mapping_node.getName() if not name.startswith(mapping_prefix): continue try: node_id = name[len(mapping_prefix):] roots_and_mappings.append((tree.getNode(node_id), mapping_node)) except tree.NoSuchNodeError: logg.error( "Configuration problem detected: Z39.50 search mapping '%s' found, " "but no matching root node with ID '%s'", name, node_id) if not roots_and_mappings: logg.info('no mappings configured, skipping search') return [] logg.debug('using mapping roots: %s', [(n1.id, n2.id) for (n1, n2) in roots_and_mappings]) # run one search per root node node_ids = [] guestaccess = acl.AccessData( user=users.getUser(config.get('user.guestuser'))) for root_node, mapping_node in roots_and_mappings: # map query fields to node attributes field_mapping = {} for field in mapping_node.getChildren(): field_mapping[field.getName()] = field.getDescription().split(';') # FIXME: this is redundant - why build an infix query string # just to parse it afterwards? query_string = query.build_query_string(field_mapping) if query_string is None: logg.info('unable to map query: [%r] using mapping %s', query, field_mapping) continue logg.info('executing query: %s', query_string) for n in root_node.search(query_string): if guestaccess.hasReadAccess(n): node_ids.append(n.id) #node_ids.append( root_node.search(query_string).getIDs() ) # use a round-robin algorithm to merge the separate query results # in order to produce maximally diverse results in the first hits # return merge_ids_as_round_robin(node_ids) return node_ids
def search_nodes(query, mapping_prefix='Z3950_search_'): """ Search nodes that match the query. 'query' is a tree of QueryBoolNode and QueryMatchNode objects. Query root nodes are configured by a naming convention. The names of mappings that starting with the given 'mapping_prefix' must end with a node ID, which is then used as root node for the search based on that field mapping. """ # find root nodes and their mappings roots_and_mappings = [] for mapping_node in mapping.getMappings(): name = mapping_node.getName() if not name.startswith(mapping_prefix): continue try: node_id = name[len(mapping_prefix):] roots_and_mappings.append((tree.getNode(node_id), mapping_node)) except tree.NoSuchNodeError: logg.error("Configuration problem detected: Z39.50 search mapping '%s' found, " "but no matching root node with ID '%s'", name, node_id) if not roots_and_mappings: logg.info('no mappings configured, skipping search') return [] logg.debug('using mapping roots: %s', [(n1.id, n2.id) for (n1, n2) in roots_and_mappings]) # run one search per root node node_ids = [] guestaccess = acl.AccessData(user=users.getUser(config.get('user.guestuser'))) for root_node, mapping_node in roots_and_mappings: # map query fields to node attributes field_mapping = {} for field in mapping_node.getChildren(): field_mapping[field.getName()] = field.getDescription().split(';') # FIXME: this is redundant - why build an infix query string # just to parse it afterwards? query_string = query.build_query_string(field_mapping) if query_string is None: logg.info('unable to map query: [%r] using mapping %s', query, field_mapping) continue logg.info('executing query: %s', query_string) for n in root_node.search(query_string): if guestaccess.hasReadAccess(n): node_ids.append(n.id) #node_ids.append( root_node.search(query_string).getIDs() ) # use a round-robin algorithm to merge the separate query results # in order to produce maximally diverse results in the first hits # return merge_ids_as_round_robin(node_ids) return node_ids
def MaskDetails(req, pid, id, err=0): mtype = getMetaType(pid) if err == 0 and id == "": # new mask mask = Mask(u"") db.session.commit() elif id != "" and err == 0: # edit mask if id.isdigit(): mask = q(Mask).get(id) db.session.commit() else: mask = mtype.getMask(id) else: # error filling values mask = Mask(req.params.get("mname", "")) mask.setDescription(req.params.get("mdescription", "")) mask.setMasktype(req.params.get("mtype")) mask.setLanguage(req.params.get("mlanguage", "")) mask.setDefaultMask(req.params.get("mdefault", False)) db.session.commit() v = getAdminStdVars(req) v["mask"] = mask v["mappings"] = getMappings() v["mtype"] = mtype v["error"] = err v["pid"] = pid v["masktypes"] = getMaskTypes() v["id"] = id v["langs"] = config.languages v["actpage"] = req.params.get("actpage") try: rules = [r.ruleset_name for r in mask.access_ruleset_assocs.filter_by(ruletype=u'read')] except: rules = [] v["acl"] = makeList(req, "read", removeEmptyStrings(rules), {}, overload=0, type=u"read") v["csrf"] = req.csrf_token.current_token return req.getTAL("web/admin/modules/metatype_mask.html", v, macro="modify_mask")
def MaskDetails(req, pid, id, err=0): mtype = getMetaType(pid) if err == 0 and id == "": # new mask mask = tree.Node("", type="mask") elif id != "" and err == 0: # edit mask if id.isdigit(): mask = tree.getNode(id) else: mask = mtype.getMask(id) else: # error filling values mask = tree.Node(req.params.get("mname", ""), type="mask") mask.setDescription(req.params.get("mdescription", "")) mask.setMasktype(req.params.get("mtype")) mask.setLanguage(req.params.get("mlanguage", "")) mask.setDefaultMask(req.params.get("mdefault", False)) v = getAdminStdVars(req) v["mask"] = mask v["mappings"] = getMappings() v["mtype"] = mtype v["error"] = err v["pid"] = pid v["masktypes"] = getMaskTypes() v["id"] = id v["langs"] = config.get("i18n.languages").split(",") v["actpage"] = req.params.get("actpage") rule = mask.getAccess("read") if rule: rule = rule.split(",") else: rule = [] rights = removeEmptyStrings(rule) v["acl"] = makeList(req, "read", rights, {}, overload=0, type="read") return req.getTAL("web/admin/modules/metatype_mask.html", v, macro="modify_mask")
def view(req): mappings = list(getMappings()) order = getSortCol(req) actfilter = getFilter(req) # filter if actfilter != "": if actfilter in ("all", "*", t(lang(req), "admin_filter_all")): None # all users elif actfilter == "0-9": num = re.compile(r'([0-9])') mappings = filter(lambda x: num.match(x.getName()), mappings) elif actfilter == "else" or actfilter == t(lang(req), "admin_filter_else"): all = re.compile(r'([a-z]|[A-Z]|[0-9])') mappings = filter(lambda x: not all.match(x.getName()), mappings) else: mappings = filter(lambda x: x.getName().lower().startswith(actfilter), mappings) pages = Overview(req, mappings) # sorting if order != "": if int(order[0:1]) == 0: mappings.sort(lambda x, y: cmp(x.getName().lower(), y.getName().lower())) elif int(order[0:1]) == 1: mappings.sort(lambda x, y: cmp(x.getNamespace().lower(), y.getNamespace().lower())) elif int(order[0:1]) == 2: mappings.sort(lambda x, y: cmp(x.getNamespaceUrl().lower(), y.getNamespaceUrl().lower())) elif int(order[0:1]) == 3: mappings.sort(lambda x, y: cmp(x.getDescription().lower(), y.getDescription().lower())) elif int(order[0:1]) == 4: mappings.sort(lambda x, y: cmp(len(x.getFields()), len(y.getFields()))) elif int(order[0:1]) == 5: mappings.sort(lambda x, y: cmp(x.getMappingType(), y.getMappingType())) elif int(order[0:1]) == 6: mappings.sort(lambda x, y: cmp(x.getActive(), y.getActive())) if int(order[1:]) == 1: mappings.reverse() else: mappings.sort(lambda x, y: cmp(x.getName().lower(), y.getName().lower())) v = getAdminStdVars(req) v["sortcol"] = pages.OrderColHeader( [ t( lang(req), "admin_mapping_col_1"), t( lang(req), "admin_mapping_col_2"), t( lang(req), "admin_mapping_col_3"), t( lang(req), "admin_mapping_col_4"), t( lang(req), "admin_mapping_col_5"), t( lang(req), "admin_mapping_col_6"), t( lang(req), "admin_mapping_col_7")]) v["mappings"] = mappings v["options"] = [] v["pages"] = pages v["actfilter"] = actfilter v["mappingtypes"] = "\n".join(getMappingTypes()) return req.getTAL("web/admin/modules/mapping.html", v, macro="view")
def view(req): mappings = list(getMappings()) order = getSortCol(req) actfilter = getFilter(req) # filter if actfilter != "": if actfilter in ("all", "*", t(lang(req), "admin_filter_all")): None # all users elif actfilter == "0-9": num = re.compile(r'([0-9])') mappings = filter(lambda x: num.match(x.getName()), mappings) elif actfilter == "else" or actfilter == t(lang(req), "admin_filter_else"): all = re.compile(r'([a-z]|[A-Z]|[0-9])') mappings = filter(lambda x: not all.match(x.getName()), mappings) else: mappings = filter( lambda x: x.getName().lower().startswith(actfilter), mappings) pages = Overview(req, mappings) # sorting if order != "": if int(order[0:1]) == 0: mappings.sort(lambda x, y: cmp(x.getName().lower(), y.getName().lower())) elif int(order[0:1]) == 1: mappings.sort(lambda x, y: cmp(x.getNamespace().lower(), y.getNamespace().lower())) elif int(order[0:1]) == 2: mappings.sort(lambda x, y: cmp(x.getNamespaceUrl().lower(), y.getNamespaceUrl().lower())) elif int(order[0:1]) == 3: mappings.sort(lambda x, y: cmp(x.getDescription().lower(), y.getDescription().lower())) elif int(order[0:1]) == 4: mappings.sort( lambda x, y: cmp(len(x.getFields()), len(y.getFields()))) elif int(order[0:1]) == 5: mappings.sort( lambda x, y: cmp(x.getMappingType(), y.getMappingType())) elif int(order[0:1]) == 6: mappings.sort(lambda x, y: cmp(x.getActive(), y.getActive())) if int(order[1:]) == 1: mappings.reverse() else: mappings.sort(lambda x, y: cmp(x.getName().lower(), y.getName().lower())) v = getAdminStdVars(req) v["sortcol"] = pages.OrderColHeader([ t(lang(req), "admin_mapping_col_1"), t(lang(req), "admin_mapping_col_2"), t(lang(req), "admin_mapping_col_3"), t(lang(req), "admin_mapping_col_4"), t(lang(req), "admin_mapping_col_5"), t(lang(req), "admin_mapping_col_6"), t(lang(req), "admin_mapping_col_7") ]) v["mappings"] = mappings v["options"] = [] v["pages"] = pages v["actfilter"] = actfilter v["mappingtypes"] = "\n".join(getMappingTypes()) return req.getTAL("web/admin/modules/mapping.html", v, macro="view")