Esempio n. 1
0
def get_user(id):
    if 'Authorization' not in app.current_request.headers.keys(
    ) or not token.is_valid_token(
            app.current_request.headers['Authorization']):
        return UNAUTHORIZED_RESPONSE
    users = User.query(id)
    return [{'name': u.name, 'email': u.email} for u in users]
Esempio n. 2
0
def validate_tokens(event):
    with Token.batch_write() as batch:
        tokens = Token.scan()
        for t in tokens:
            if not token.is_valid_token(t.token):
                t.valid = False
                batch.save(t)
    return "Success"
Esempio n. 3
0
def logout():
    try:
        if 'Authorization' not in app.current_request.headers.keys(
        ) or not token.is_valid_token(
                app.current_request.headers['Authorization']):
            return UNAUTHORIZED_RESPONSE
        t = Token.get(app.current_request.headers['Authorization'])
        t.valid = False
        t.save()
    except (DoesNotExist, Exception) as e:
        pass
    return Response(body={'message': 'Logout successfully'},
                    headers=DEFAULT_HEADERS)
Esempio n. 4
0
def get_song_by_user(user_id):
    if 'Authorization' not in app.current_request.headers.keys(
    ) or not token.is_valid_token(
            app.current_request.headers['Authorization']):
        return UNAUTHORIZED_RESPONSE
    songs = Song.scan(Song.owner.startswith(user_id))
    return [{
        'id': s.id,
        'name': s.name,
        'file': s.file,
        'shared': s.shared,
        'owner': s.owner,
        'genre': s.genre
    } for s in songs]
Esempio n. 5
0
def get_song_by_id(id):
    if 'Authorization' not in app.current_request.headers.keys(
    ) or not token.is_valid_token(
            app.current_request.headers['Authorization']):
        return UNAUTHORIZED_RESPONSE
    s = Song.get(id)
    return {
        'id': s.id,
        'name': s.name,
        'file': s.file,
        'shared': s.shared,
        'owner': s.owner,
        'genre': s.genre
    }
Esempio n. 6
0
def create_user():
    try:
        if 'Authorization' not in app.current_request.headers.keys(
        ) or not token.is_valid_token(
                app.current_request.headers['Authorization']):
            return UNAUTHORIZED_RESPONSE
        info = token.extract_info(app.current_request.headers['Authorization'])
        u = User.get(info['email'])
        return {'email': u.email, 'name': u.name}
    except (DoesNotExist, Exception) as e:
        app.log.warn(e)
        return Response(body={'message': 'Token is invalid'},
                        headers=DEFAULT_HEADERS,
                        status_code=401)
Esempio n. 7
0
def create_song():
    if 'Authorization' not in app.current_request.headers.keys(
    ) or not token.is_valid_token(
            app.current_request.headers['Authorization']):
        return UNAUTHORIZED_RESPONSE
    data = app.current_request.json_body
    s = Song(**data)
    s.id = Song.uuid()
    s.save()
    return {
        'id': s.id,
        'name': s.name,
        'file': s.file,
        'shared': s.shared,
        'owner': s.owner,
        'genre': s.genre
    }
Esempio n. 8
0
def upload_to_s3(user_id):
    try:
        if 'Authorization' not in app.current_request.headers.keys(
        ) or not token.is_valid_token(
                app.current_request.headers['Authorization']):
            return UNAUTHORIZED_RESPONSE
        body = app.current_request.raw_body
        file_name = str(uuid.uuid4())
        # write body to tmp file
        tmp_file_name = '/tmp/' + file_name
        with open(tmp_file_name, 'wb') as tmp_file:
            tmp_file.write(body)

        # upload tmp file to s3 bucket
        s3_client.upload_file(tmp_file_name, BUCKET,
                              'music/%s/%s' % (user_id, file_name))

        return {'path': 'music/%s/%s' % (user_id, file_name)}
    except Exception as e:
        app.log.error('error occurred during upload %s' % e)
        return Response(body={'message': 'upload failed % s' % e},
                        headers=DEFAULT_HEADERS,
                        status_code=400)