Example #1
0
def suggest_property(slug):
    project = object_or_404(Project.by_slug(slug))
    authz.require(authz.project_read(project))

    prefix = '%%%s%%' % request.args.get('prefix', '')
    q = db.session.query(Attribute)
    q = q.join(Schema)
    q = q.filter(Schema.obj=='entity')
    q = q.filter(Schema.project==project)
    q = q.filter(or_(Attribute.label.ilike(prefix), Attribute.name.ilike(prefix)))
    q = q.limit(get_limit(default=5))

    matches = []
    for attribute in q:
        matches.append({
            'name': attribute.label,
            'n:type': {
                'id': '/properties/property',
                'name': 'Property'
            },
            'id': attribute.name
        })
    return jsonify({
        "code" : "/api/status/ok",
        "status" : "200 OK",
        "prefix" : request.args.get('prefix', ''),
        "result" : matches
        })
Example #2
0
def reconcile_op(project, query):
    log.info("Reconciling in %s: %r", project.slug, query)

    schemata = []
    if "type" in query:
        schemata = query.get("type")
        if isinstance(schemata, basestring):
            schemata = [schemata]
        schemata = [s.rsplit("/", 1)[-1] for s in schemata]

    properties = []
    if "properties" in query:
        for p in query.get("properties"):
            properties.append((p.get("pid"), p.get("v")))

    matches = find_matches(project, request.account, query.get("query", ""), schemata=schemata, properties=properties)
    matches = matches.limit(get_limit(default=5))

    results = []
    for match in matches:
        data = {
            "name": match["entity"]["name"].value,
            "score": match["score"],
            "type": [],
            "id": match["entity"].id,
            "uri": url_for("entities_api.view", id=match["entity"].id, _external=True),
            "match": match["score"] == 100,
        }
        for schema in match["entity"].schemata:
            if schema.hidden:
                continue
            data["type"].append({"id": "/" + project.slug + "/" + schema.name, "name": schema.label})
        results.append(data)

    return {"result": results, "num": len(results)}
Example #3
0
def suggest_type(slug):
    project = object_or_404(Project.by_slug(slug))
    authz.require(authz.project_read(project))

    prefix = '%%%s%%' % request.args.get('prefix', '')
    log.info("Suggesting types in %s: %r", project.slug, prefix)

    q = db.session.query(Schema)
    q = q.filter(Schema.obj == 'entity')
    q = q.filter(Schema.hidden == False) # noqa
    q = q.filter(Schema.project == project)
    q = q.filter(or_(Schema.label.ilike(prefix), Schema.name.ilike(prefix)))
    q = q.limit(get_limit(default=5))

    matches = []
    for schema in q:
        matches.append({
            'name': schema.label,
            'id': '/%s/%s' % (slug, schema.name)
        })
    return jsonify({
        "code": "/api/status/ok",
        "status": "200 OK",
        "prefix": request.args.get('prefix', ''),
        "result": matches
    })
Example #4
0
def suggest_property(slug):
    project = object_or_404(Project.by_slug(slug))
    authz.require(authz.project_read(project))

    prefix = "%%%s%%" % request.args.get("prefix", "")
    log.info("Suggesting property names in %s: %r", project.slug, prefix)

    q = db.session.query(Attribute)
    q = q.join(Schema)
    q = q.filter(Schema.obj == "entity")
    q = q.filter(Schema.project == project)
    q = q.filter(or_(Attribute.label.ilike(prefix), Attribute.name.ilike(prefix)))
    q = q.limit(get_limit(default=5))

    matches = []
    for attribute in q:
        matches.append(
            {
                "name": attribute.label,
                "n:type": {"id": "/properties/property", "name": "Property"},
                "id": attribute.name,
            }
        )
    return jsonify(
        {"code": "/api/status/ok", "status": "200 OK", "prefix": request.args.get("prefix", ""), "result": matches}
    )
Example #5
0
def suggest_property(slug):
    project = object_or_404(Project.by_slug(slug))
    authz.require(authz.project_read(project))

    prefix = '%%%s%%' % request.args.get('prefix', '')
    log.info("Suggesting property names in %s: %r", project.slug, prefix)

    q = db.session.query(Attribute)
    q = q.join(Schema)
    q = q.filter(Schema.obj == 'entity')
    q = q.filter(Schema.project == project)
    q = q.filter(or_(Attribute.label.ilike(prefix),
                     Attribute.name.ilike(prefix)))
    q = q.limit(get_limit(default=5))

    matches = []
    for attribute in q:
        matches.append({
            'name': attribute.label,
            'n:type': {
                'id': '/properties/property',
                'name': 'Property'
            },
            'id': attribute.name
        })
    return jsonify({
        "code": "/api/status/ok",
        "status": "200 OK",
        "prefix": request.args.get('prefix', ''),
        "result": matches
    })
Example #6
0
 def __init__(self, query, name=None, limit=25, pager_range=4, **kwargs):
     self.args = request.args
     self.name = name
     self.query = query
     self.kwargs = kwargs
     self.pager_range = pager_range
     self.offset = arg_int(self.arg_name("offset"), default=0)
     self.limit = get_limit(default=limit, field=self.arg_name("limit"))
Example #7
0
 def __init__(self, query, name=None, limit=25, pager_range=4, **kwargs):
     self.args = request.args
     self.name = name
     self.query = query
     self.kwargs = kwargs
     self.pager_range = pager_range
     self.offset = arg_int(self.arg_name('offset'), default=0)
     self.limit = get_limit(default=limit, field=self.arg_name('limit'))
Example #8
0
def suggest_entity(slug):
    """
    Suggest API, emulates Google Refine API. See:
    https://github.com/OpenRefine/OpenRefine/wiki/Reconciliation-Service-API
    """

    project = object_or_404(Project.by_slug(slug))
    authz.require(authz.project_read(project))

    prefix = "%%%s%%" % request.args.get("prefix", "")
    log.info("Suggesting entities in %s: %r", project.slug, prefix)

    q = db.session.query(Entity)
    q = q.join(Property)
    q = q.join(Project)
    q = q.filter(Property.name == "name")
    q = q.filter(Property.active == True)
    q = q.filter(Property.entity_id == Entity.id)
    q = q.filter(Property.value_string.ilike(prefix))
    q = q.filter(Project.slug == slug)

    if "type" in request.args:
        schema_name = request.args.get("type")
        if "/" in schema_name:
            _, schema_name = schema_name.rsplit("/", 1)
        q = q.join(Schema)
        q = q.filter(Schema.name == schema_name)

    q = q.distinct()
    q = q.limit(get_limit(default=5))

    matches = []
    for e in q:
        data = {
            "name": e["name"].value,
            "n:type": {},
            "type": [],
            "uri": url_for("entities_api.view", id=e.id, _external=True),
            "id": e.id,
        }

        for schema in e.schemata:
            if schema.hidden:
                continue
            data["type"].append({"id": "/" + project.slug + "/" + schema.name, "name": schema.label})

        if len(data["type"]):
            data["n:type"] = data["type"][0]

        matches.append(data)

    return jsonify(
        {"code": "/api/status/ok", "status": "200 OK", "prefix": request.args.get("prefix", ""), "result": matches}
    )
Example #9
0
 def __init__(self, query, name=None, limit=25, pager_range=4,
              results_converter=lambda x: x, **kwargs):
     self.args = request.args
     self.results_converter = results_converter
     self.name = name
     self.query = query
     self.kwargs = kwargs
     self.pager_range = pager_range
     self.offset = arg_int(self.arg_name('offset'), default=0)
     self.limit = get_limit(default=limit, field=self.arg_name('limit'))
     self._results = None
Example #10
0
def suggest_entity(slug):
    """
    Suggest API, emulates Google Refine API. See:
    https://github.com/OpenRefine/OpenRefine/wiki/Reconciliation-Service-API
    """

    project = object_or_404(Project.by_slug(slug))
    authz.require(authz.project_read(project))

    prefix = '%%%s%%' % request.args.get('prefix', '')
    log.info("Suggesting entities in %s: %r", project.slug, prefix)

    q = db.session.query(Entity)
    q = q.join(Property)
    q = q.join(Project)
    q = q.filter(Property.name == 'name')
    q = q.filter(Property.active == True) # noqa
    q = q.filter(Property.entity_id == Entity.id)
    q = q.filter(Property.value_string.ilike(prefix))
    q = q.filter(Project.slug == slug)

    if 'type' in request.args:
        schema_name = request.args.get('type')
        if '/' in schema_name:
            _, schema_name = schema_name.rsplit('/', 1)
        q = q.join(Schema)
        q = q.filter(Schema.name == schema_name)

    q = q.distinct()
    q = q.limit(get_limit(default=5))

    matches = []
    for e in q:
        data = {
            'name': e['name'].value,
            'n:type': {
                'id': '/' + project.slug + '/' + e.schema.name,
                'name': e.schema.label
            },
            'uri': url_for('entities_api.view', id=e.id, _external=True),
            'id': e.id
        }

        data['type'] = [data.get('n:type')]
        matches.append(data)

    return jsonify({
        "code": "/api/status/ok",
        "status": "200 OK",
        "prefix": request.args.get('prefix', ''),
        "result": matches
    })
Example #11
0
def suggest_entity(slug):
    """ 
    Suggest API, emulates Google Refine API. See:
    https://github.com/OpenRefine/OpenRefine/wiki/Reconciliation-Service-API
    """

    project = object_or_404(Project.by_slug(slug))
    authz.require(authz.project_read(project))

    prefix = request.args.get('prefix', '') + '%'

    q = db.session.query(EntityProperty)
    q = q.join(Entity)
    q = q.join(Project)
    q = q.filter(EntityProperty.name=='name')
    q = q.filter(EntityProperty.active==True)
    q = q.filter(EntityProperty.entity_id!=None)
    q = q.filter(EntityProperty.value_string.ilike(prefix))
    q = q.filter(Project.slug==slug)

    if 'type' in request.args:
        schema_name = request.args.get('type')
        if '/' in schema_name:
            _, schema_name = schema_name.rsplit('/', 1)
        q = q.join(Schema)
        q = q.filter(Schema.name==schema_name)

    q = q.distinct()
    q = q.limit(get_limit(default=5))

    matches = []
    for eprop in q:
        matches.append({
            'name': eprop.value_string,
            'n:type': {
                'id': '/' + project.slug,
                'name': project.label
                },
            'id': eprop.entity_id
            })
    return jsonify({
        "code" : "/api/status/ok",
        "status" : "200 OK",
        "prefix" : request.args.get('prefix', ''),
        "result" : matches
        })
Example #12
0
def reconcile_op(project, query):
    log.info("Reconciling in %s: %r", project.slug, query)
    
    schemata = []
    if 'type' in query:
        schemata = query.get('type')
        if isinstance(schemata, basestring):
            schemata = [schemata]
        schemata = [s.rsplit('/', 1)[-1] for s in schemata]

    properties = []
    if 'properties' in query:
        for p in query.get('properties'):
            properties.append((p.get('pid'), p.get('v')))

    matches = find_matches(project, request.account,
                           query.get('query', ''),
                           schemata=schemata,
                           properties=properties)
    matches = matches.limit(get_limit(default=5))

    results = []
    for match in matches:
        data = {
            'name': match['entity']['name'].value,
            'score': match['score'],
            'type': [{
                'id': '/' + project.slug + '/' + match['entity'].schema.name,
                'name': match['entity'].schema.label
            }],
            'id': match['entity'].id,
            'uri': url_for('entities_api.view', id=match['entity'].id,
                           _external=True),
            'match': False #match['score'] == 100
        }
        results.append(data)

    return {
        'result': results,
        'num': len(results)
    }
Example #13
0
def table(id):
    file = object_or_404(File.by_id(id))
    authz.require(authz.project_read(file.project))
    limit = get_limit(10)
    validate_cache(keys={'id': file.id, 'limit': limit})
    return jsonify(files.as_table(file, limit))
Example #14
0
def table(id):
    file = object_or_404(File.by_id(id))
    authz.require(authz.project_read(file.project))
    limit = get_limit(10)
    validate_cache(keys={'id': file.id, 'limit': limit})
    return jsonify(files.as_table(file, limit))