Esempio n. 1
0
def suggest(dataset):
    """ 
    Suggest API, emulates Google Refine API. See:
    http://code.google.com/p/google-refine/wiki/SuggestApi
    """
    dataset = Dataset.by_name(dataset)
    entities = Entity.all().filter(Entity.invalid!=True)
    query = request.args.get('prefix', '').strip()
    entities = entities.filter(Entity.name.ilike('%s%%' % query))
    entities = entities.offset(get_offset(field='start'))
    entities = entities.limit(get_limit(default=20))

    matches = []
    for entity in entities:
        matches.append({
            'name': entity.name,
            'n:type': {
                'id': '/' + dataset.name,
                'name': dataset.label
                },
            'id': entity.id
            })
    return jsonify({
        "code" : "/api/status/ok",
        "status" : "200 OK",
        "prefix" : query,
        "result" : matches
        })
Esempio n. 2
0
def query_pager(q, paginate=True, serializer=lambda x: x, **kw):
    limit = get_limit()
    offset = get_offset()
    if paginate:
        results = q.offset(offset).limit(limit)
    else:
        results = q
    url = url_for(request.endpoint, _external=True, **kw)
    count = q.count()
    data = {
        'count': count,
        'limit': limit,
        'offset': offset,
        'format': url + args('LIMIT', 'OFFSET'),
        'previous': prev_url(url, count, offset, limit),
        'next': next_url(url, count, offset, limit),
        'results': map(serializer, results)
    }
    response = jsonify(data, refs=True)
    if data['next']:
        response.headers.add_header('Link', '<%s>; rel=next' % data['next'])
    if data['previous']:
        response.headers.add_header('Link',
                                    '<%s>; rel=previous' % data['previous'])
    return response
Esempio n. 3
0
def query_pager(q, paginate=True, serializer=lambda x: x, **kw):
    limit = get_limit()
    offset = get_offset()
    if paginate:
        results = q.offset(offset).limit(limit)
    else:
        results = q
    url = url_for(request.endpoint, _external=True, **kw)
    count = q.count()
    data = {
        'count': count,
        'limit': limit,
        'offset': offset,
        'format': url + args('LIMIT', 'OFFSET'),
        'previous': prev_url(url, count, offset, limit),
        'next': next_url(url, count, offset, limit),
        'results': map(serializer, results)
    }
    response = jsonify(data, refs=True)
    if data['next']:
        response.headers.add_header('Link', '<%s>; rel=next' % data['next'])
    if data['previous']:
        response.headers.add_header('Link', '<%s>; rel=previous' % data['previous'])
    return response