def fragment(frag_id):
    cfg = app.config.get("FRAGMENTS", {}).get(frag_id)
    fname = cfg.get("function")
    if fname is not None:
        fn = plugin.load_function(fname)
        return fn()
    abort(404)
def fragment(frag_id):
    cfg = app.config.get("FRAGMENTS", {}).get(frag_id)
    fname = cfg.get("function")
    if fname is not None:
        fn = plugin.load_function(fname)
        return fn()
    abort(404)
Exemple #3
0
def fragment(frag_id):
    cfg = app.config.get("FRAGMENTS", {}).get(frag_id)
    if cfg is None:
        abort(404)

    # see if we want to render from a function
    fname = cfg.get("function")
    if fname is not None:
        fn = plugin.load_function(fname)
        return fn()

    # see if we want to render from a template
    temp = cfg.get("template")
    if temp is not None:
        return render_template(temp)

    abort(404)
def search():
    # get the values for the 3 key bits of search info: the query, the page number and the page size
    q = request.values.get("q")
    page = request.values.get("page", 1)
    psize = request.values.get("pageSize", 10)

    # check that we have been given a query
    if q is None or q == "":
        abort(400)

    # check the page is an integer greater than 0
    try:
        page = int(page)
    except:
        abort(400)
    if page < 1:
        page = 1

    # limit the page size as per the configuration
    try:
        psize = int(psize)
    except:
        abort(400)
    if psize > app.config.get("SEARCH_MAX_PAGE_SIZE", 100):
        psize = app.config.get("SEARCH_MAX_PAGE_SIZE", 100)
    elif psize < 1:
        psize = 10

    # calculate the position of the from cursor in the document set
    fro = (page - 1) * psize

    # assemble the query
    query = dao.QueryStringQuery(q, fro, psize)

    # load the DAO class and send the query through it
    klazz = plugin.load_class(app.config.get("SEARCH_DAO"))
    res = klazz.query(q=query.query())

    # check to see if there was a search error
    if res.get("error") is not None:
        abort(400)

    # unpack the results and pull out the search metadata
    obs = esprit.raw.unpack_json_result(res)
    total = res.get("hits", {}).get("total", 0)

    # optionally filter the result objects as per the config
    filter = app.config.get("SEARCH_RESULT_FILTER")
    if filter is not None:
        fn = plugin.load_function(filter)
        obs = [fn(o) for o in obs]

    # build the response object
    response = {
        "total" : total,
        "page" : page,
        "pageSize" : psize,
        "timestamp" : datetime.utcnow().strftime("%Y-%m%dT%H:%M:%SZ"),
        "query" : q,
        "results" : obs
    }

    resp = make_response(json.dumps(response))
    resp.mimetype = "application/json"
    return resp
Exemple #5
0
def query(path=None):
    # get the bits out of the path, and ensure that we have at least some parts to work with
    pathparts = path.strip('/').split('/')
    if len(pathparts) == 0:
        abort(400)

    # load the query route config and the path we are being requested for
    qrs = app.config.get("QUERY_ROUTE", {})
    frag = request.path

    # get the configuration for this url route
    route_cfg = None
    for key in qrs:
        if frag.startswith("/" + key):
            route_cfg = qrs.get(key)
            break

    # if no route cfg is found this is not authorised
    if route_cfg is None:
        abort(401)

    # get the configuration for the specific index being queried
    index = pathparts[0]
    cfg = route_cfg.get(index)
    if cfg is None:
        abort(401)

    # does the user have to be authenticated
    if cfg.get("auth"):
        if current_user is None or current_user.is_anonymous():
            abort(401)

        # if so, does the user require a role
        role = cfg.get("roles")
        if role is not None and not current_user.has_role(role):
            abort(401)

    # get the name of the model that will handle this query, and then look up
    # the class that will handle it
    dao_name = cfg.get("dao")
    dao_klass = plugin.load_class(dao_name)
    if dao_klass is None:
        abort(404)

    # now work out what kind of operation is being asked for
    # if _search is specified, then this is a normal query
    search = False
    by_id = None
    if len(pathparts) == 1 or (len(pathparts) == 2 and pathparts[1] == "_search"):
        search = True
    elif len(pathparts) == 2:
        if request.method == "POST":
            abort(401)
        by_id = pathparts[1]
    else:
        abort(400)

    resp = None
    if by_id is not None:
        rec = dao_klass.pull(by_id, wrap=False)
        resp = make_response(rec)
    elif search:
        q = Query()

        # if this is a POST, read the contents out of the body
        if request.method == "POST":
            q = Query(request.json) if request.json else Query(dict(request.form).keys()[-1]) # FIXME: does this actually work?

        # if there is a q param, make it into a query string query
        elif 'q' in request.values:
            s = request.values['q']
            op = request.values.get('default_operator')
            q.query_string(s, op)

        # if there is a source param, load the json from it
        elif 'source' in request.values:
            q = Query(json.loads(urllib2.unquote(request.values['source'])))

        # now run the query through the filters
        filters = app.config.get("QUERY_FILTERS", {})
        filter_names = cfg.get("filters", [])
        for filter_name in filter_names:
            # because of back-compat, we have to do a few tricky things here...
            # filter may be the name of a filter in the list of query filters
            fn = filters.get(filter_name)
            if fn is None:
                # filter may be the path to a function
                fn = plugin.load_function(filter_name)
            if fn is None:
                app.logger.info("Unable to load query filter for {x}".format(x=filter_name))
                abort(500)
            fn(q)

        # finally send the query and return the response
        res = dao_klass.query(q=q.as_dict())
        resp = make_response(json.dumps(res))
    else:
        abort(400)

    resp.mimetype = "application/json"
    return resp
from octopus.modules.oag.oagr import JobRunner
from octopus.core import initialise, app
from octopus.lib import plugin, error_handler

import logging

if __name__ == "__main__":
    initialise()
    error_handler.setup_error_logging(app, "OAGR Runner Error", stdout_logging_level=logging.INFO)

    closure_path = app.config.get("OAGR_RUNNER_CALLBACK_CLOSURE")
    if closure_path is None:
        print "ERROR: cannot start job runner without OAGR_RUNNER_CALLBACK_CLOSURE defined"
        exit(0)

    fn = plugin.load_function(closure_path)
    if fn is None:
        print "ERROR: callback closure function not defined: " + closure_path + " - sert OAGR_RUNNER_CALLBACK_CLOSURE correctly"
        exit(0)

    print "Using closure " + closure_path + " to generate callback"

    cb = fn()
    if cb is None:
        print "ERROR: closure did not return anything.  Check your function at " + closure_path
        exit(0)

    print "Using " + str(cb) + " as OAGR callback"

    jr = JobRunner(callback=fn())
    jr.run()
from octopus.lib import plugin, error_handler

import logging

if __name__ == "__main__":
    initialise()
    error_handler.setup_error_logging(app,
                                      "OAGR Runner Error",
                                      stdout_logging_level=logging.INFO)

    closure_path = app.config.get("OAGR_RUNNER_CALLBACK_CLOSURE")
    if closure_path is None:
        print "ERROR: cannot start job runner without OAGR_RUNNER_CALLBACK_CLOSURE defined"
        exit(0)

    fn = plugin.load_function(closure_path)
    if fn is None:
        print "ERROR: callback closure function not defined: " + closure_path + " - sert OAGR_RUNNER_CALLBACK_CLOSURE correctly"
        exit(0)

    print "Using closure " + closure_path + " to generate callback"

    cb = fn()
    if cb is None:
        print "ERROR: closure did not return anything.  Check your function at " + closure_path
        exit(0)

    print "Using " + str(cb) + " as OAGR callback"

    jr = JobRunner(callback=fn())
    jr.run()
def search():
    # get the values for the 3 key bits of search info: the query, the page number and the page size
    q = request.values.get("q")
    page = request.values.get("page", 1)
    psize = request.values.get("pageSize", 10)

    # check that we have been given a query
    if q is None or q == "":
        abort(400)

    # check the page is an integer greater than 0
    try:
        page = int(page)
    except:
        abort(400)
    if page < 1:
        page = 1

    # limit the page size as per the configuration
    try:
        psize = int(psize)
    except:
        abort(400)
    if psize > app.config.get("SEARCH_MAX_PAGE_SIZE", 100):
        psize = app.config.get("SEARCH_MAX_PAGE_SIZE", 100)
    elif psize < 1:
        psize = 10

    # calculate the position of the from cursor in the document set
    fro = (page - 1) * psize

    # assemble the query
    query = dao.QueryStringQuery(q, fro, psize)

    # load the DAO class and send the query through it
    klazz = plugin.load_class(app.config.get("SEARCH_DAO"))
    res = klazz.query(q=query.query())

    # check to see if there was a search error
    if res.get("error") is not None:
        abort(400)

    # unpack the results and pull out the search metadata
    obs = esprit.raw.unpack_json_result(res)
    total = res.get("hits", {}).get("total", 0)

    # optionally filter the result objects as per the config
    filter = app.config.get("SEARCH_RESULT_FILTER")
    if filter is not None:
        fn = plugin.load_function(filter)
        obs = [fn(o) for o in obs]

    # build the response object
    response = {
        "total": total,
        "page": page,
        "pageSize": psize,
        "timestamp": datetime.utcnow().strftime("%Y-%m%dT%H:%M:%SZ"),
        "query": q,
        "results": obs,
    }

    resp = make_response(json.dumps(response))
    resp.mimetype = "application/json"
    return resp