Ejemplo n.º 1
0
def create_download_file():
    """Create Excel file from action resource records and return URI to file"""
    if request.method == 'OPTIONS':
        return make_api_response()

    qb = QueryBuilder(request.args)
    if qb.query_type == 'aggregate':
        query_cursor = db.action.aggregate(qb.get_query())
        data = [elem for elem in query_cursor]
    else:
        query_cursor = db.action.find(qb.get_query()).sort('timestamp', pymongo.DESCENDING)
        data = process_query_results(query_cursor, 'action')

    username = g.user.username
    filename = username + ''.join(random.choice(string.ascii_uppercase + string.digits) for x in xrange(16))

    make_excel_file(filename, data)

    token = AuthToken.generate(filename)
    urlsafe_token = urllib.quote(token)
    tempDownloadUri = request.base_url + '/report.xlsx?fileToken=' + urlsafe_token

    return make_api_response(json_util.dumps({'file_token': tempDownloadUri}))
Ejemplo n.º 2
0
def get_action_resource():
    """Return action resource records"""
    if request.method == 'OPTIONS':
        return make_api_response()

    qb = QueryBuilder(request.args)
    if qb.query_type == 'aggregate':
        query_cursor = db.action.aggregate(qb.get_query())
        data = [elem for elem in query_cursor]
    else:
        query_cursor = db.action.find(qb.get_query()).sort('timestamp', pymongo.DESCENDING)
        data = process_query_results(query_cursor, 'action')

    query_string = urllib.urlencode(request.args)
    uri = '/api/v1/action?' + query_string

    response_data = {
        'uri': uri,
        'type': qb.query_type,
        'total': len(data),
        'results': data
    }

    return make_api_response(json_util.dumps(response_data))