예제 #1
0
    def index(format=None):
        request.cache_key['args'] = request.args.items()
        q = filter_query(cls.all())

        format = response_format(request)
        if format == 'csv':
            def generate():
                for entity in q:
                    if hasattr(entity, 'as_shallow'):
                        yield entity.as_shallow()
                    else:
                        yield entity.as_dict()
            return stream_csv(generate(), filename="%s.csv" % name)

        count = q.count()
        if q.has_fts:
            q = q.order_by('ts_rank_cd(entity.full_text, plainto_tsquery(:ftsq)) DESC')
        limit = get_limit()
        q = q.limit(limit)
        offset = get_offset()
        q = q.offset(offset)
        return jsonify({
            'count': count,
            'next': paged_url('.index', limit, offset+limit) if count > offset else False, 
            'previous': paged_url('.index', limit, offset-limit) if offset > 0 else False,
            'limit': limit,
            'offset': offset,
            'results': q,
            'facets': make_facets()
            }, shallow=True)
예제 #2
0
파일: api.py 프로젝트: tttp/lobbyfacts
    def index(format=None):
        request.cache_key['args'] = request.args.items()
        q = filter_query(cls.all())
        if 'update' in request.args:
            # FIXME set time correctly!
            lastupdate=datetime.now().replace(hour=1,minute=0,second=0,microsecond=0) - timedelta(days=arg_int('update',1))
            q=q.filter(cls.updated_at > lastupdate)

        format = response_format(request)
        if format == 'csv':
            def generate():
                for entity in q:
                    if hasattr(entity, 'as_shallow'):
                        yield entity.as_shallow()
                    else:
                        yield entity.as_dict()
            return stream_csv(generate(), filename="%s.csv" % name)

        count = q.count()
        if q.has_fts:
            q = q.order_by('ts_rank_cd(entity.full_text, plainto_tsquery(:ftsq)) DESC')
        limit = get_limit()
        q = q.limit(limit)
        offset = get_offset()
        q = q.offset(offset)
        return jsonify({
            'count': count,
            'next': paged_url('.index', limit, offset+limit) if count > offset+limit else False, 
            'previous': paged_url('.index', limit, offset-limit) if offset > 0 else False,
            'limit': limit,
            'offset': offset,
            'results': q,
            'facets': make_facets()
            }, shallow=True)
예제 #3
0
        return NotFound(name)
    func = REPORTS[name]
    argspec = inspect.getargspec(func)
    args = {}
    for k, v in request.args.items():
        if k in argspec.args:
            args[k] = v
    try:
        q = func(**args)
    except Exception, exc:
        return jsonify({'error': unicode(exc)}, status=400)
    
    format = response_format(request)
    if format == 'csv':
        return stream_csv(get_results(q), filename="%s.csv" % name)

    count = q.count()
    limit = get_limit()
    q = q.limit(limit)
    offset = get_offset()
    q = q.offset(offset)
    return jsonify({
        'count': count,
        'next': paged_url('.report', limit, offset+limit, name=name),
        'previous': paged_url('.report', limit, offset-limit, name=name),
        'limit': limit,
        'offset': offset,
        'query': str(q),
        'results': get_results(q)
        }, shallow=True)