Exemple #1
0
def generate_random_pairing():
    query = {
        'label': None,
        'sort': 'random',
        'same_as': {'optional': 'forbidden'}
    }
    ent = execute_query(query).get('result')
    ent_id = ent.get('id')
    avoid = same_as.expand(ent_id)
    avoid.update(Pairing.existing(ent_id))
    q = {
        'id|!=': list(avoid),
        'label%=': ent.get('label')
    }
    for res in execute_query([q]).get('result'):
        return (res.get('id'), ent_id, res.get('score'))
def query():
    authz.require(authz.system_read())
    if request.method == "GET":
        try:
            q = json.loads(request.args.get("q"))
        except (TypeError, ValueError) as e:
            data = {"status": "error", "message": "Invalid query: %s" % unicode(e)}
            return jsonify(data, status=400)
    else:
        q = request_data()
    return jsonify(execute_query(q))
def view(root, id):
    authz.require(authz.system_edit())
    q = Context.by_root(root)
    q = q.filter(Context.id == id)
    context = obj_or_404(q.first())
    statements = []
    entities = set()
    for stmt in context.statements:
        entities.add(stmt.subject)
        if qualified[stmt.attribute].data_type == "entity":
            entities.add(stmt._value)
        statements.append(stmt.to_dict(raw=True))
    q = [{"assume": [context.id], "id|=": list(entities), "label": None, "type": None}]
    entities = {e.get("id"): e for e in execute_query(q).get("result")}
    return jsonify(
        {"status": "ok", "entities": entities, "context": context.to_dict(enrich=True), "statements": statements}
    )