Exemple #1
0
def update_user(id, body):
    """ Updates an active user matching a given id with given parameters such as name, email and password. When a
    parameter is empty it is not updated"""
    logging.debug("{users_controller} BEGIN function update_user()")

    if id is '':
        return RESP.response_400(message='The id parameter is empty!')

    try:
        user = CRUD.read_user_by_id(id)
    except Exception:
        return RESP.response_500(message='Database is down!')

    if user is None:
        return RESP.response_404(message='User not found!')

    try:
        if body['email'] is not '' and body['email'] != user.email:
            user_email = CRUD.read_user_by_email(body['email'])
            if user_email is not None:
                return RESP.response_409(
                    message='The given email is already in use!')
    except Exception:
        return RESP.response_500(message='Database is down!')

    try:
        CRUD.update_user(user, body['name'], body['email'],
                         UTILS.hash_password(body['password']))
        CRUD.commit()
    except Exception:
        CRUD.rollback()
        return RESP.response_500(message='Database is down!')

    return RESP.response_200(message='User updated with success!')
def delete_playlist(id):
    """ Deletes a playlist"""
    logging.debug("{users_controller} BEGIN function delete_playlist()")

    if id is '':
        return RESP.response_400(message='The id parameter is empty!')

    try:
        playlist = CRUD.read_playlist_by_id(id)
    except Exception:
        return RESP.response_500(message='Database is down!')

    if playlist is None:
        return RESP.response_404(message='Playlist not found!')

    try:
        CRUD.delete_object(playlist)

        entries = CRUD.read_songs_from_playlist(id)

        for entry in entries:
            CRUD.delete_object(entry)

        CRUD.commit()
    except Exception:
        CRUD.rollback()
        RESP.response_500(message='Database is down!')

    return RESP.response_200(message='Playlist deleted with success')
Exemple #3
0
def update_song(id, body):
    """ Updates an active song matching a given id with given parameters such as title, artist, album, release year and
    path. When a parameter is empty it is not updated"""
    logging.debug("{songs_controller} BEGIN function update_song()")

    if id is '':
        return RESP.response_400(message='The id parameter is empty!')

    try:
        song = CRUD.read_song_by_song_id(id)
    except Exception:
        return RESP.response_500(message='Database is down!')

    if song is None:
        return RESP.response_404(message='Song not found!')

    try:
        CRUD.update_song(song, body['title'], body['artist'], body['album'],
                         body['release_year'], body['path'])
        CRUD.commit()
    except Exception:
        CRUD.rollback()
        return RESP.response_500(message='Database is down!')

    return RESP.response_200(message='Song updated with success!')
def update_playlist(id, body):
    """ Updates a playlist matching a given id with given the given name"""
    logging.debug("{users_controller} BEGIN function update_playlist()")

    if id is '':
        return RESP.response_400(message='The id parameter is empty!')

    if body['name'] is '':
        return RESP.response_400(message='The name parameter is empty!')

    try:
        playlist = CRUD.read_playlist_by_id(id)
    except Exception:
        return RESP.response_500(message='Database is down!')

    if playlist is None:
        return RESP.response_404(message='Playlist not found!')

    try:
        CRUD.update_playlist(playlist, name=body['name'])
        CRUD.commit()
    except Exception:
        CRUD.rollback()
        return RESP.response_500(message='Database is down!')

    return RESP.response_200(message='Playlist updated with success!')
def get_playlist_songs(id):
    """ Retrieves all playlist songs' ids"""
    logging.debug(
        "{users_controller} BEGIN function delete_song_from_playlist()")

    if id is '':
        return RESP.response_400(message='A given parameter is empty')

    try:
        playlist = CRUD.read_playlist_by_id(id)
    except Exception:
        return RESP.response_500(message='Database is down!')

    if playlist is None:
        return RESP.response_404(message='Playlist not found!')

    try:
        songs = CRUD.read_songs_from_playlist(id)
    except Exception:
        return RESP.response_500(message='Database is down!')

    array = []

    for song in songs:
        array.append(song.dump())

    return RESP.response_200(message=array)
Exemple #6
0
def get_playlist_songs_info(id):
    """ Retrieves all playlist songs' information"""
    logging.debug("{aggregator_controller} BEGIN function get_playlist_songs_info()")

    if id is '':
        return RESP.response_400(message='A given parameter is empty')

    base_url = PLAYLISTS_MS + '/playlists/songs'
    url = '/'.join((base_url, str(id)))

    # Checks if song exists by sending a request into the Songs Microservice

    headers = {'Content-Type': 'application/json',
               'Authorization': request.headers['Authorization']}
    headers.update(create_http_headers_for_new_span())

    with zipkin_span(service_name='aggregator_ms', span_name='get_playlists_songs') as zipkin_context:
        r = requests.get(url, headers=headers)
        zipkin_context.update_binary_annotations({'http.method': 'GET', 'http.url': url,
                                                  'http.status_code': r.status_code})

    if r.status_code == 400:
        return RESP.response_400()
    if r.status_code == 404:
        return RESP.response_404(message='Playlist not found!')
    if r.status_code == 500:
        return RESP.response_500(message='Playlists_MS is down!')

    response_data = json.loads(r.text)

    song_ids = []
    for dictionary in response_data:
        song_ids.append(dictionary['song_id'])

    return RESP.response_200(message="Playlist Songs Info Retrieved Successfully")
def add_song_to_playlist(id, body):
    """ Adds a song into a playlist"""
    logging.debug("{users_controller} BEGIN function add_song_to_playlist()")

    if id is '' or body['song_id'] is '' or body['user_id'] is '':
        return RESP.response_400(message='A given parameter is empty')

    try:
        playlist = CRUD.read_playlist_by_id(id)
    except Exception:
        return RESP.response_500(message='Database is down!')

    if playlist is None:
        return RESP.response_404(message='Playlist not found!')

    if playlist.user_id != body['user_id']:
        return RESP.response_400(
            message='This playlist belongs to another user')

    # Checks if song exists by sending a request into the Songs Microservice
    headers = {
        'Content-Type': 'application/json',
        'Authorization': request.headers['Authorization']
    }
    param = {'id': body['song_id']}
    r = requests.get(SONGS_MS + '/songs', params=param, headers=headers)
    if r.status_code == 404:
        return RESP.response_404(message='Song not found!')
    if r.status_code == 500:
        return RESP.response_500(message='Songs_MS is down!')

    try:
        CRUD.create_song_in_playlist(id, body['song_id'])
        CRUD.commit()
    except Exception:
        CRUD.rollback()
        return RESP.response_500(message='Database is down!')

    return RESP.response_200(message='Song added into playlist with success!')
def get_song(id):
    """ Retrives a song given an id"""
    headers = {
        'Content-Type': 'application/json',
        'Authorization': request.headers['Authorization']
    }
    param = {'id': id}
    r = requests.get(SONGS_MS + '/songs', params=param, headers=headers)
    if r.status_code == 404:
        return RESP.response_404(message='Song not found!')
    if r.status_code == 500:
        return RESP.response_500(message='Songs_MS is down!')

    return json.loads(r.text)
def delete_song_from_playlist(id, song_id, user_id):
    """ Removes a song from a playlist"""
    logging.debug(
        "{users_controller} BEGIN function delete_song_from_playlist()")

    if id is '' or song_id is '' or user_id is '':
        return RESP.response_400(message='A given parameter is empty')

    try:
        playlist = CRUD.read_playlist_by_id(id)
    except Exception:
        return RESP.response_500(message='Database is down!')

    if playlist is None:
        return RESP.response_404(message='Playlist not found!')

    if playlist.user_id != user_id:
        return RESP.response_400(
            message='This playlist belongs to another user')

    try:
        playlist_song = CRUD.read_song_from_playlist(id, song_id)
    except Exception:
        return RESP.response_500(message='Database is down!')

    if playlist_song is None:
        return RESP.response_404(message='Song not found is playlist')

    try:
        CRUD.delete_object(playlist_song)
        CRUD.commit()
    except Exception:
        CRUD.rollback()
        return RESP.response_500(message='Database is down!')

    return RESP.response_200(message='Song removed from playlist with success')
Exemple #10
0
def read_user(email):
    """ Returns an active user (if any) given an email"""
    logging.debug("{users_controller} BEGIN function read_user()")

    if email is '':
        return RESP.response_400(message='The email parameter is empty!')

    try:
        user = CRUD.read_user_by_email_not_deleted(email)
    except Exception:
        return RESP.response_500(message='Database is down!')

    if user is None:
        return RESP.response_404(message='User not found!')

    return RESP.response_200(message=user.dump())
Exemple #11
0
def read_song(id):
    """ Returns a song (if any) given an id"""
    logging.debug("{songs_controller} BEGIN function read_song()")

    if id is '':
        return RESP.response_400(message='The id parameter is empty!')

    try:
        song = CRUD.read_song_by_song_id(id)
    except Exception:
        return RESP.response_500(message='Database is down!')

    if song is None:
        return RESP.response_404(message='Song not found!')

    return RESP.response_200(message=song.dump())
def get_playlist(id):
    """ Gets a playlist given an id"""
    logging.debug("{users_controller} BEGIN function get_playlist()")

    if id is '':
        return RESP.response_400(message='The id parameter is empty!')

    try:
        playlist = CRUD.read_playlist_by_id(id)
    except Exception:
        return RESP.response_500(message='Database is down!')

    if playlist is None:
        return RESP.response_404(message='Playlist not found!')

    return RESP.response_200(message=playlist.dump())
Exemple #13
0
def get_song(id):
    """ Retrives a song given an id"""
    headers = {'Content-Type': 'application/json',
               'Authorization': request.headers['Authorization']}
    headers.update(create_http_headers_for_new_span())
    param = {'id': id}

    with zipkin_span(service_name='aggregator_ms', span_name='get_song') as zipkin_context:
        r = requests.get(SONGS_MS + '/songs', params=param, headers=headers)
        zipkin_context.update_binary_annotations({'http.method': 'GET', 'http.url': SONGS_MS + '/songs',
                                                  'http.status_code': r.status_code})

    if r.status_code == 404:
        return RESP.response_404(message='Song not found!')
    if r.status_code == 500:
        return RESP.response_500(message='Songs_MS is down!')

    return json.loads(r.text)
Exemple #14
0
def delete_user(id):
    """ Deletes an active user given an id"""
    logging.debug("{users_controller} BEGIN function delete_user()")

    if id is '':
        return RESP.response_400(message='The id parameter is empty!')

    try:
        user = CRUD.read_user_by_id(id)
    except Exception:
        return RESP.response_500(message='Database is down!')

    if user is None:
        return RESP.response_404(message='User not found!')

    try:
        CRUD.delete_user(user)
        CRUD.commit()
    except Exception:
        CRUD.rollback()
        return RESP.response_500(message='Database is down!')

    return RESP.response_200(message='User deleted with success')
Exemple #15
0
def delete_song(id):
    """ Deletes an active song given an id"""
    logging.debug("{songs_controller} BEGIN function delete_song()")

    if id is '':
        return RESP.response_400(message='The id parameter is empty!')

    try:
        song = CRUD.read_song_by_song_id(id)
    except Exception:
        return RESP.response_500(message='Database is down!')

    if song is None:
        return RESP.response_404(message='Song not found!')

    try:
        CRUD.delete_song(song)
        CRUD.commit()
    except Exception:
        CRUD.rollback()
        return RESP.response_500(message='Database is down!')

    return RESP.response_200(message='Song deleted with success')
def get_playlist_songs_info(id):
    """ Retrieves all playlist songs' information"""
    logging.debug(
        "{aggregator_controller} BEGIN function get_playlist_songs_info()")

    if id is '':
        return RESP.response_400(message='A given parameter is empty')

    # Checks if song exists by sending a request into the Songs Microservice
    headers = {
        'Content-Type': 'application/json',
        'Authorization': request.headers['Authorization']
    }

    base_url = PLAYLISTS_MS + '/playlists/songs'
    url = '/'.join((base_url, str(id)))

    r = requests.get(url, headers=headers)
    if r.status_code == 400:
        return RESP.response_400()
    if r.status_code == 404:
        return RESP.response_404(message='Playlist not found!')
    if r.status_code == 500:
        return RESP.response_500(message='Playlists_MS is down!')

    response_data = json.loads(r.text)

    song_ids = []
    for dictionary in response_data:
        song_ids.append(dictionary['song_id'])

    pool = multiprocessing.Pool(processes=len(song_ids))
    pool_outputs = pool.map(get_song, song_ids)
    pool.close()
    pool.join()

    return RESP.response_200(message=pool_outputs)