Beispiel #1
0
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)
Beispiel #2
0
def search(searchtype,
           searchquery,
           readable_query,
           paths,
           req,
           container_id=None):
    from web.frontend.content import ContentList
    if not container_id:
        container_id = req.args.get("id", type=int)
    container = q(Container).get(container_id) if container_id else None

    # if the current node is not a Container or not accessible by the user, use the collections root instead
    if container is None or not container.has_read_access():
        # XXX: The collections root is always searchable. Could there be situations in which we don't want to allow this?
        # XXX: We could check the read permission for Collections to decide if search is allowed.
        container = get_collections_node()

    try:
        result = container.search(searchquery).filter_read_access()
    except SearchQueryException as e:
        # query parsing went wrong or the search backend complained about something
        return NoSearchResult(readable_query,
                              container,
                              readable_query,
                              error=True)

    content_list = ContentList(result,
                               container,
                               paths,
                               words=readable_query,
                               show_sidebar=False)
    try:
        content_list.feedback(req)
    except Exception as e:
        # that should not happen, but is somewhat likely (db failures, illegal search queries that slipped through...),
        # just show 0 result view and don't confuse the user with unhelpful error messages ;)
        logg.exception(
            "exception executing %(searchtype)s search for query %(readable_query)s",
            dict(searchtype=searchtype,
                 readable_query=readable_query,
                 error=True))
        db.session.rollback()
        return NoSearchResult(readable_query,
                              container,
                              searchtype,
                              error=True)

    language = lang(req)
    content_list.linkname = u"{}: {} \"{}\"".format(
        container.getLabel(language), translate("search_for",
                                                language=language),
        readable_query)
    content_list.linktarget = ""

    if content_list.has_elements:
        logg.info("%s search with query '%s' on container %s produced results",
                  searchtype, readable_query, container_id)
        return content_list
    else:
        logg.info(
            "%s search with query '%s' on container %s produced no results",
            searchtype, readable_query, container_id)
        return NoSearchResult(readable_query, container, searchtype)
Beispiel #3
0
def extended_search(req):
    from web.frontend.content import ContentList
    max = 3
    if req.params.get("searchmode") == "extendedsuper":
        max = 10
    sfields = []
    access = AccessData(req)
    metatype = None

    collectionid = req.params.get("collection", tree.getRoot().id)
    try:
        collection = tree.getNode(collectionid)
    except:
        for coll in tree.getRoot("collections").getChildren():
            collection = tree.getNode(coll.id)
            break

    q_str = ''
    q_user = ''
    first2 = 1
    for i in range(1, max + 1):
        f = u(req.params.get("field" + str(i), "").strip())
        q = u(req.params.get("query" + str(i), "").strip())

        if not q and "query" + str(i) + "-from" not in req.params:
            continue

        if not first2:
            q_str += " and "
            q_user += " %s " % (translate("search_and", request=req))

        first2 = 0

        if not f.isdigit():
            q = u(req.params.get("query" + str(i), "").strip())
            q_str += f + '=' + protect(q)
            q_user += f + '=' + protect(q)
        else:
            masknode = tree.getNode(f)
            assert masknode.type == "searchmaskitem"
            first = 1
            q_str += "("
            for metatype in masknode.getChildren():
                if not first:
                    q_str += " or "
                    q_user += " %s " % (translate("search_or", request=req))
                first = 0
                if "query" + str(
                        i) + "-from" in req.params and metatype.getFieldtype(
                        ) == "date":
                    date_from = "0000-00-00T00:00:00"
                    date_to = "0000-00-00T00:00:00"
                    fld = metatype
                    if str(req.params["query" + str(i) + "-from"]) != "":
                        date_from = date.format_date(
                            date.parse_date(
                                str(req.params["query" + str(i) + "-from"]),
                                fld.getValues()), "%Y-%m-%dT%H:%M:%S")
                    if str(req.params["query" + str(i) + "-to"]) != "":
                        date_to = date.format_date(
                            date.parse_date(
                                str(req.params["query" + str(i) + "-to"]),
                                fld.getValues()), "%Y-%m-%dT%H:%M:%S")

                    if date_from == "0000-00-00T00:00:00" and date_to != date_from:  # from value
                        q_str += metatype.getName() + ' <= ' + date_to
                        q_user += "%s &le; \"%s\"" % (
                            metatype.getName(),
                            str(req.params["query" + str(i) + "-to"]))

                    elif date_to == "0000-00-00T00:00:00" and date_to != date_from:  # to value
                        q_str += metatype.getName() + ' >= ' + date_from
                        q_user += "%s &ge; \"%s\"" % (
                            metatype.getName(),
                            str(req.params["query" + str(i) + "-from"]))
                    else:
                        q_str += '({} >= {} and {} <= {})'.format(
                            metatype.getName(), date_from, metatype.getName(),
                            date_to)

                        q_user += "(%s %s \"%s\" %s \"%s\")" % (
                            metatype.getName(),
                            translate("search_between", request=req),
                            str(req.params["query" + str(i) + "-from"]),
                            translate("search_and", request=req),
                            str(req.params["query" + str(i) + "-to"]))
                else:
                    q = u(req.params.get("query" + str(i), "").strip())
                    q_str += metatype.getName() + '=' + protect(q)
                    if metatype.getLabel() != "":
                        q_user += "%s = %s" % (metatype.getLabel(), protect(q))
                    else:
                        q_user += "%s = %s" % (metatype.getName(), protect(q))

            q_str += ")"
    try:
        if req.params.get(
                "act_node",
                "") and req.params.get("act_node") != str(collection.id):
            result = tree.getNode(req.params.get("act_node")).search(q_str)
        else:
            result = collection.search(q_str)
        result = access.filter(result)
        logging.getLogger('usertracing').info(access.user.name +
                                              " xsearch for '" + q_user +
                                              "', " + str(len(result)) +
                                              " results")
        if len(result) > 0:
            cl = ContentList(result, collection, q_user.strip())
            cl.feedback(req)
            cl.linkname = ""
            cl.linktarget = ""
            return cl
        return SearchResult([], q_user.strip())
    except:
        return SearchResult(None, q_user.strip())
Beispiel #4
0
def extended_search(req):
    from web.frontend.content import ContentList
    max = 3
    if req.params.get("searchmode") == "extendedsuper":
        max = 10
    sfields = []
    access = AccessData(req)
    metatype = None

    collectionid = req.params.get("collection", tree.getRoot().id)
    try:
        collection = tree.getNode(collectionid)
    except:
        for coll in tree.getRoot("collections").getChildren():
            collection = tree.getNode(coll.id)
            break

    q_str = ''
    q_user = ''
    first2 = 1
    for i in range(1, max + 1):
        f = u(req.params.get("field" + str(i), "").strip())
        q = u(req.params.get("query" + str(i), "").strip())

        if not q and "query" + str(i) + "-from" not in req.params:
            continue

        if not first2:
            q_str += " and "
            q_user += " %s " % (translate("search_and", request=req))

        first2 = 0

        if not f.isdigit():
            q = u(req.params.get("query" + str(i), "").strip())
            q_str += f + '=' + protect(q)
            q_user += f + '=' + protect(q)
        else:
            masknode = tree.getNode(f)
            assert masknode.type == "searchmaskitem"
            first = 1
            q_str += "("
            for metatype in masknode.getChildren():
                if not first:
                    q_str += " or "
                    q_user += " %s " % (translate("search_or", request=req))
                first = 0
                if "query" + str(i) + "-from" in req.params and metatype.getFieldtype() == "date":
                    date_from = "0000-00-00T00:00:00"
                    date_to = "0000-00-00T00:00:00"
                    fld = metatype
                    if str(req.params["query" + str(i) + "-from"]) != "":
                        date_from = date.format_date(
                            date.parse_date(str(req.params["query" + str(i) + "-from"]), fld.getValues()), "%Y-%m-%dT%H:%M:%S")
                    if str(req.params["query" + str(i) + "-to"]) != "":
                        date_to = date.format_date(
                            date.parse_date(str(req.params["query" + str(i) + "-to"]), fld.getValues()), "%Y-%m-%dT%H:%M:%S")

                    if date_from == "0000-00-00T00:00:00" and date_to != date_from:  # from value
                        q_str += metatype.getName() + ' <= ' + date_to
                        q_user += "%s &le; \"%s\"" % (metatype.getName(), str(req.params["query" + str(i) + "-to"]))

                    elif date_to == "0000-00-00T00:00:00" and date_to != date_from:  # to value
                        q_str += metatype.getName() + ' >= ' + date_from
                        q_user += "%s &ge; \"%s\"" % (metatype.getName(), str(req.params["query" + str(i) + "-from"]))
                    else:
                        q_str += '({} >= {} and {} <= {})'.format(metatype.getName(), date_from, metatype.getName(), date_to)

                        q_user += "(%s %s \"%s\" %s \"%s\")" % (metatype.getName(),
                                                                translate("search_between",
                                                                          request=req),
                                                                str(req.params["query" + str(i) + "-from"]),
                                                                translate("search_and",
                                                                          request=req),
                                                                str(req.params["query" + str(i) + "-to"]))
                else:
                    q = u(req.params.get("query" + str(i), "").strip())
                    q_str += metatype.getName() + '=' + protect(q)
                    if metatype.getLabel() != "":
                        q_user += "%s = %s" % (metatype.getLabel(), protect(q))
                    else:
                        q_user += "%s = %s" % (metatype.getName(), protect(q))

            q_str += ")"
    try:
        if req.params.get("act_node", "") and req.params.get("act_node") != str(collection.id):
            result = tree.getNode(req.params.get("act_node")).search(q_str)
        else:
            result = collection.search(q_str)
        result = access.filter(result)
        logging.getLogger('usertracing').info(access.user.name + " xsearch for '" + q_user + "', " + str(len(result)) + " results")
        if len(result) > 0:
            cl = ContentList(result, collection, q_user.strip())
            cl.feedback(req)
            cl.linkname = ""
            cl.linktarget = ""
            return cl
        return SearchResult([], q_user.strip())
    except:
        return SearchResult(None, q_user.strip())
Beispiel #5
0
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)