示例#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
示例#2
0
文件: songs.py 项目: zpevnik/backend
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
示例#3
0
文件: songs.py 项目: zpevnik/backend
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
示例#4
0
文件: main.py 项目: zpevnik/backend
def cleanup():
    ip = request.remote_addr
    if ip != app.config['SERVER_IP']:
        abort(404)

    log_event(EVENTS.CLEANUP, None,
              'Cleaning up the temp folder from {}.'.format(ip))

    # go through every songbook and check its cache
    valid_files = []
    songbooks = g.model.songbooks.find()
    for songbook in songbooks:
        # check whether this songbook is cached
        if songbook.is_cached():
            if not songbook.is_cache_valid():
                # invalidate cache in case that time is up
                songbook.invalidate_cache()
                g.model.songbooks.save(songbook)
            else:
                # append cached file as valid
                valid_files.append(get_cached_file() + '.pdf')

    # check every file in done folder and delete invalid ones
    for temp_file in os.listdir(app.config['SONGBOOK_DONE_FOLDER']):
        if temp_file not in valid_files:
            os.unlink(app.config['SONGBOOK_DONE_FOLDER'] + temp_file)

    return 'Ok'
示例#5
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
示例#6
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
示例#7
0
文件: main.py 项目: zpevnik/backend
def handle_AppException(exception):
    # create response from exception data
    response = jsonify(exception.get_exception())
    response.status_code = exception.status_code

    # log exception event and respond
    log_event(exception.exp_type, current_user.get_id(),
              exception.get_exception())
    return response
示例#8
0
文件: songs.py 项目: zpevnik/backend
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
示例#9
0
def songbook_duplicate(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 = songbook.get_serialized_data()
    data['owner'] = current_user.get_id()

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

    return jsonify(link='songbooks/{}'.format(new_songbook.get_id())), 201, \
          {'location': '/songbooks/{}'.format(new_songbook.get_id())}
示例#10
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
示例#11
0
文件: songs.py 项目: zpevnik/backend
def song_variant_duplicate(song_id, variant_id):
    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))

    data = variant.get_serialized_data()
    data['owner'] = current_user.get_id()
    data['visibility'] = PERMISSION.PRIVATE
    data['title'] = '{} duplicate'.format(data['title'])

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

    song = validators.song_existence(song_id)

    return jsonify(song.get_serialized_data(current_user.get_id())), 200
示例#12
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())}
示例#13
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
示例#14
0
文件: songs.py 项目: zpevnik/backend
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