Esempio n. 1
0
def interpreter_single(interpreter_id):
    interpreter = validators.interpreter_existence(interpreter_id)

    if request.method == 'GET':
        return jsonify(interpreter.get_serialized_data()), 200

    elif request.method == 'PUT':
        interpreter = validators.interpreter_existence(interpreter_id)

        data = request.get_json()
        validators.json_request(data)
        data = validators.interpreters_request(data)

        interpreter.set_data(data)

        data['interpreter_id'] = interpreter_id
        g.model.interpreters.save(interpreter)
        log_event(EVENTS.INTERPRETER_EDIT, current_user.get_id(), data)

        return jsonify(interpreter.get_serialized_data()), 200

    else:
        g.model.interpreters.delete(interpreter)
        log_event(EVENTS.INTERPRETER_DELETE, current_user.get_id(), interpreter_id)

        return jsonify(), 204
Esempio n. 2
0
def song_single(song_id):
    song = validators.song_existence(song_id)

    if request.method == 'GET':
        return jsonify(song.get_serialized_data(current_user.get_id())), 200

    else:
        data = request.get_json()
        validators.json_request(data)
        data = validators.songs_request(data)

        for author in data['authors']['music']:
            validators.author_existence(author)
        for author in data['authors']['lyrics']:
            validators.author_existence(author)
        for interpreter in data['interpreters']:
            validators.interpreter_existence(interpreter)

        song.set_data(data)

        data['song_id'] = song_id
        g.model.songs.save(song)
        log_event(EVENTS.SONG_EDIT, current_user.get_id(), data)

        return jsonify(song.get_serialized_data(current_user.get_id())), 200
Esempio n. 3
0
def song_variants(song_id):
    validators.song_existence(song_id)

    if request.method == 'GET':
        result = g.model.variants.find_filtered(current_user.get_id(), song_id=song_id)
        response = []
        for res in result:
            response.append(res.get_serialized_data())

        return jsonify(response), 200

    else:
        data = request.get_json()
        validators.json_request(data)

        data = validators.song_variant_request(data)
        data['song_id'] = song_id
        data['owner'] = current_user.get_id()
        data['visibility'] = data['visibility'] if 'visibility' in data else PERMISSION.PRIVATE

        validators.song_format(data)

        variant = g.model.variants.create_variant(data)
        log_event(EVENTS.VARIANT_NEW, current_user.get_id(), data)

        return jsonify(variant.get_serialized_data()), 201
Esempio n. 4
0
def author_single(author_id):
    author = validators.author_existence(author_id)

    if request.method == 'GET':
        return jsonify(author.get_serialized_data()), 200

    elif request.method == 'PUT':
        author = validators.author_existence(author_id)

        data = request.get_json()
        validators.json_request(data)
        data = validators.authors_request(data)

        author.set_data(data)

        data['author_id'] = author_id
        g.model.authors.save(author)
        log_event(EVENTS.AUTHOR_EDIT, current_user.get_id(), data)

        return jsonify(author.get_serialized_data()), 200

    else:
        g.model.authors.delete(author)
        log_event(EVENTS.AUTHOR_DELETE, current_user.get_id(), author_id)

        return jsonify(), 204
Esempio n. 5
0
def songbook_single(songbook_id):
    songbook = validators.songbook_existence(songbook_id)
    if current_user.get_id() != songbook.get_owner():
        raise AppException(EVENTS.BASE_EXCEPTION, 403,
                           (EXCODES.INSUFFICIENT_PERMISSIONS,
                            STRINGS.INSUFFICIENT_PERMISSIONS))

    if request.method == 'GET':
        if 'Accept' in request.headers and request.headers[
                'Accept'] == 'application/pdf':
            return jsonify(export_songbook(songbook)), 200
        return jsonify(songbook.get_serialized_data()), 200

    elif request.method == 'PUT':
        data = request.get_json()
        validators.json_request(data)
        data = validators.songbooks_request(data)

        data['options'] = validators.songbook_options(data['options'])
        data['songs'] = validators.songbook_songs(data['songs'])

        songbook.set_data(data)
        g.model.songbooks.save(songbook)

        data['songbook_id'] = songbook_id
        log_event(EVENTS.SONGBOOK_EDIT, current_user.get_id(), data)

        return jsonify(songbook.get_serialized_data()), 200

    else:
        g.model.songbooks.delete(songbook)
        log_event(EVENTS.SONGBOOK_DELETE, current_user.get_id(), songbook_id)

        return jsonify(), 204
Esempio n. 6
0
def songs():
    if request.method == 'GET':
        data = validators.handle_GET_request(request.args)

        # find all results for currect user
        result = g.model.songs.find_filtered(data['query'], data['order'], current_user.get_id())

        # prepare response
        size = len(result)
        response = {
            'data': [],
            'count': size,
            'pages': int(math.ceil(size / data['per_page']))
        } # yapf: disable

        # slice results based on 'page' and 'per_page' values
        result = result[(data['per_page'] * data['page']):(data['per_page'] * (data['page'] + 1))]

        for res in result:
            response['data'].append(res.get_serialized_data(current_user.get_id()))

        return jsonify(response), 200

    else:
        data = request.get_json()
        validators.json_request(data)

        data = validators.songs_extended_request(data)
        data['owner'] = current_user.get_id()
        data['visibility'] = data['visibility'] if 'visibility' in data else PERMISSION.PRIVATE

        for author in data['authors']['music']:
            validators.author_existence(author)
        for author in data['authors']['lyrics']:
            validators.author_existence(author)
        for interpreter in data['interpreters']:
            validators.interpreter_existence(interpreter)

        data['variant']['owner'] = current_user.get_id()
        data['variant']['visibility'] = data['variant']['visibility'] if 'visibility' in data[
            'variant'] else PERMISSION.PRIVATE

        validators.song_format(data['variant'])

        song = g.model.songs.create_song(data)
        log_event(EVENTS.SONG_NEW, current_user.get_id(), data)

        data['variant']['song_id'] = song.get_id()

        variant = g.model.variants.create_variant(data['variant'])
        log_event(EVENTS.VARIANT_NEW, current_user.get_id(), data['variant'])

        return jsonify(song.get_serialized_data(current_user.get_id())), 201
Esempio n. 7
0
def song_variant_single(song_id, variant_id):
    song = validators.song_existence(song_id)

    variant = validators.song_variant_existence(variant_id)
    if not permissions.check_perm(current_user, variant, visibility=True):
        raise AppException(EVENTS.BASE_EXCEPTION, 403,
                           (EXCODES.INSUFFICIENT_PERMISSIONS, STRINGS.INSUFFICIENT_PERMISSIONS))

    if request.method == 'GET':
        return jsonify(variant.get_serialized_data()), 200

    elif request.method == 'PUT':
        if not permissions.check_perm(current_user, variant, editing=True):
            raise AppException(EVENTS.BASE_EXCEPTION, 403,
                               (EXCODES.INSUFFICIENT_PERMISSIONS, STRINGS.INSUFFICIENT_PERMISSIONS))

        data = request.get_json()
        validators.json_request(data)
        data = validators.song_variant_request(data)

        # Currently, translated and validated song is not saved.
        # This can be changed in the future.
        validators.song_format(data)
        variant.set_data(data)

        data['variant_id'] = variant_id
        g.model.variants.save(variant)
        log_event(EVENTS.VARIANT_EDIT, current_user.get_id(), data)

        return jsonify(variant.get_serialized_data()), 200

    else:
        if not permissions.check_perm(current_user, variant, editing=True):
            raise AppException(EVENTS.BASE_EXCEPTION, 403,
                               (EXCODES.INSUFFICIENT_PERMISSIONS, STRINGS.INSUFFICIENT_PERMISSIONS))

        g.model.variants.delete(variant)
        log_event(EVENTS.VARIANT_DELETE, current_user.get_id(), variant_id)

        # there is no remaining variant for this song
        if not len(g.model.variants.find(song_id=song_id)):
            g.model.songs.delete(song)
            log_event(EVENTS.SONG_DELETE, current_user.get_id(), song_id)

        return jsonify(), 204
Esempio n. 8
0
def songbook_title(songbook_id):
    songbook = validators.songbook_existence(songbook_id)
    if current_user.get_id() != songbook.get_owner():
        raise AppException(EVENTS.BASE_EXCEPTION, 403,
                           (EXCODES.INSUFFICIENT_PERMISSIONS,
                            STRINGS.INSUFFICIENT_PERMISSIONS))

    data = request.get_json()
    validators.json_request(data)
    data = validators.songbooks_title_request(data)

    songbook.set_title(data['title'])
    g.model.songbooks.save(songbook)

    data['songbook_id'] = songbook_id
    log_event(EVENTS.SONGBOOK_EDIT, current_user.get_id(), data)

    return jsonify(songbook.get_serialized_data()), 200
Esempio n. 9
0
def interpreters():
    if request.method == 'GET':
        result = g.model.interpreters.find()
        response = []
        for res in result:
            response.append(res.get_serialized_data())

        return jsonify(response), 200

    else:
        data = request.get_json()
        validators.json_request(data)

        data = validators.interpreters_request(data)
        validators.interpreter_nonexistence(data['name'])

        interpreter = g.model.interpreters.create_interpreter(data)
        log_event(EVENTS.INTERPRETER_NEW, current_user.get_id(), data)

        return jsonify(link='interpreters/{}'.format(interpreter.get_id())), 201, \
              {'location': '/interpreters/{}'.format(interpreter.get_id())}
Esempio n. 10
0
def songbooks():
    if request.method == 'GET':
        data = validators.handle_GET_request(request.args)

        # find all results for currect user
        result = g.model.songbooks.find_filtered(data['query'],
                                                 current_user.get_id())

        # prepare response
        size = len(result)
        response = {
            'data': [],
            'count': size,
            'pages': int(math.ceil(size / data['per_page']))
        } # yapf: disable

        # slice results based on 'page' and 'per_page' values
        result = result[(data['per_page'] * data['page']):(data['per_page'] *
                                                           (data['page'] + 1))]

        for res in result:
            response['data'].append(res.get_serialized_data())

        return jsonify(response), 200

    else:
        data = request.get_json()
        validators.json_request(data)

        data = validators.songbooks_title_request(data)
        data['owner'] = current_user.get_id()

        songbook = g.model.songbooks.create_songbook(data)
        log_event(EVENTS.SONGBOOK_NEW, current_user.get_id(), data)

        return jsonify(songbook.get_serialized_data()), 201