Ejemplo n.º 1
0
def update_rewatch():
    try:
        json_data = request.get_json()
        new_rewatch = int(json_data['rewatch'])
        media_id = int(json_data['element_id'])
        media_list = json_data['element_type']
    except:
        return '', 400

    # Check if the <media_list> exist and is valid
    try:
        list_type = ListType(media_list)
    except ValueError:
        return '', 400

    # Check that <new_rewatch> is between [0-10]
    if 0 > new_rewatch > 10:
        return '', 400

    media = check_media(media_id, list_type)
    if not media or (media[1].status != Status.COMPLETED
                     and media[1].status != Status.COMPLETED_ANIMATION):
        return '', 400

    # Get the old data
    old_rewatch = media[1].rewatched

    # Set the new data
    media[1].rewatched = new_rewatch
    app.logger.info('[{}] Series ID {} re-watched {}x times'.format(
        current_user.id, media_id, new_rewatch))

    # total eps/movies watched
    if list_type != ListType.MOVIES:
        media[1].eps_watched = media[0].total_episodes + (
            new_rewatch * media[0].total_episodes)
    elif list_type == ListType.MOVIES:
        media[1].eps_watched = 1 + new_rewatch

    # Commit the changes
    db.session.commit()

    # Compute the new time spent
    if list_type != ListType.MOVIES:
        compute_time_spent(media=media[0],
                           list_type=list_type,
                           old_rewatch=old_rewatch,
                           new_rewatch=new_rewatch)
    elif list_type == ListType.MOVIES:
        compute_time_spent(media=media[0],
                           list_type=list_type,
                           movie_status=media[1].status,
                           old_rewatch=old_rewatch,
                           new_rewatch=new_rewatch)

    return '', 204
Ejemplo n.º 2
0
def write_comment(media_type, media_id):
    # Check if <media_type> is valid
    try:
        media_type = MediaType(media_type)
    except ValueError:
        abort(404)

    if media_type == MediaType.SERIES:
        list_type = ListType.SERIES
    elif media_type == MediaType.ANIME:
        list_type = ListType.ANIME
    elif media_type == MediaType.MOVIES:
        list_type = ListType.MOVIES

    media = check_media(media_id, list_type)
    if not media:
        abort(404)

    form = MediaComment()
    if request.method == 'GET':
        form.comment.data = media[1].comment
    if form.validate_on_submit():
        comment = form.comment.data
        media[1].comment = comment

        db.session.commit()
        app.logger.info('[{}] added a comment on {} with ID [{}]'.format(
            current_user.id, media_type, media_id))

        if not comment or comment == '':
            flash('Your comment has been removed (or is empty).', 'warning')
        else:
            flash('Comment successfully added/modified.', 'success')

        if request.args.get('from') == 'media':
            return redirect(
                url_for('main.media_sheet',
                        media_type=media_type.value,
                        media_id=media_id))
        return redirect(
            url_for('main.mymedialist',
                    media_list=list_type.value,
                    user_name=current_user.username))

    return render_template('medialist_comment.html',
                           title='Add comment',
                           form=form,
                           media_name=media[0].name)
Ejemplo n.º 3
0
def delete_element():
    try:
        json_data = request.get_json()
        media_id = int(json_data['delete'])
        media_list = json_data['element_type']
    except:
        return '', 400

    # Check if the <media_list> exist and is valid
    try:
        list_type = ListType(media_list)
    except ValueError:
        return '', 400

    media = check_media(media_id, list_type)
    if media is None:
        return '', 400

    # Get the old data
    old_rewatch = media[1].rewatched

    # Compute the new time spent
    if list_type == ListType.SERIES or list_type == ListType.ANIME:
        old_watched = media[1].eps_watched
        compute_time_spent(media=media[0],
                           old_watched=old_watched,
                           list_type=list_type,
                           old_rewatch=old_rewatch)
    elif list_type == ListType.MOVIES:
        compute_time_spent(media=media[0],
                           list_type=list_type,
                           movie_status=media[1].status,
                           movie_delete=True,
                           old_rewatch=old_rewatch)

    # Delete the media from the user's list
    db.session.delete(media[1])
    db.session.commit()
    app.logger.info('[User {}] {} [ID {}] successfully removed.'.format(
        current_user.id, list_type.value.replace('list', ''), media_id))

    return '', 204
Ejemplo n.º 4
0
def update_score():
    try:
        json_data = request.get_json()
        new_score = json_data['score']
        media_id = int(json_data['element_id'])
        media_list = json_data['element_type']
    except:
        return '', 400

    # Check if <media_list> exist and valid
    try:
        list_type = ListType(media_list)
    except ValueError:
        return '', 400

    # Check that <new_score> is '---' or between [0-10]
    try:
        if 0 > float(new_score) > 10:
            return '', 400
    except:
        new_score = -1

    media = check_media(media_id, list_type)
    if not media:
        return '', 400

    # Get the old data
    old_score = media[1].score

    # Set the new data
    media[1].score = new_score
    app.logger.info('[{}] Series ID {} score updated from {} to {}'.format(
        current_user.id, media_id, old_score, new_score))

    # Commit the changes
    db.session.commit()

    return '', 204
Ejemplo n.º 5
0
def add_element():
    try:
        json_data = request.get_json()
        media_id = json_data['element_id']
        media_list = json_data['element_type']
        media_cat = json_data['element_cat']
    except:
        return '', 400

    # Check if the <media_list> exist and is valid
    try:
        list_type = ListType(media_list)
    except ValueError:
        return '', 400

    # Check if <status> is valid compared to <list_type>
    new_status = check_cat_type(list_type, media_cat)
    if not new_status:
        return '', 400

    # Check if the <media>
    media = check_media(media_id, list_type, add=True)
    if media is None:
        return '', 400

    # Setup the <season>, <episode> and <category> of the <media>
    new_watched = 1
    if list_type == ListType.SERIES or list_type == ListType.ANIME:
        new_season = 1
        new_episode = 1
        if new_status == Status.COMPLETED:
            new_season = len(media.eps_per_season)
            new_episode = media.eps_per_season[-1].episodes
            new_watched = media.total_episodes
        elif new_status == Status.RANDOM or new_status == Status.PLAN_TO_WATCH:
            new_episode = 0
            new_watched = 0
    elif list_type == ListType.MOVIES:
        if new_status == Status.COMPLETED:
            if MoviesGenre.query.filter_by(media_id=media.id,
                                           genre="Animation").first():
                new_status = Status.COMPLETED_ANIMATION
        else:
            new_watched = 0

    # Add <media> to the <user>
    if list_type == ListType.SERIES:
        user_list = SeriesList(user_id=current_user.id,
                               media_id=media.id,
                               current_season=new_season,
                               last_episode_watched=new_episode,
                               status=new_status,
                               eps_watched=new_watched)
    elif list_type == ListType.ANIME:
        user_list = AnimeList(user_id=current_user.id,
                              media_id=media.id,
                              current_season=new_season,
                              last_episode_watched=new_episode,
                              status=new_status,
                              eps_watched=new_watched)
    elif list_type == ListType.MOVIES:
        user_list = MoviesList(user_id=current_user.id,
                               media_id=media.id,
                               status=new_status,
                               eps_watched=new_watched)

    # Commit the changes
    db.session.add(user_list)
    db.session.commit()
    app.logger.info('[User {}] {} Added [ID {}] in the category: {}'.format(
        current_user.id, list_type.value.replace('list', ''), media_id,
        new_status.value))

    # Set the last update
    set_last_update(media=media, media_type=list_type, new_status=new_status)

    # Compute the new time spent
    if list_type == ListType.SERIES or list_type == ListType.ANIME:
        compute_time_spent(media=media,
                           new_watched=new_watched,
                           list_type=list_type)
    elif list_type == ListType.MOVIES:
        compute_time_spent(media=media,
                           list_type=list_type,
                           movie_status=new_status,
                           movie_add=True)

    return '', 204
Ejemplo n.º 6
0
def change_element_category():
    try:
        json_data = request.get_json()
        media_new_cat = json_data['status']
        media_id = int(json_data['element_id'])
        media_list = json_data['element_type']
    except:
        return '', 400

    # Check the <media_list> parameter
    try:
        list_type = ListType(media_list)
    except ValueError:
        return '', 400

    # Check if the <status> parameter
    new_status = check_cat_type(list_type, media_new_cat)
    if not new_status:
        return '', 400

    # Recover the media
    media = check_media(media_id, list_type)
    if media is None:
        return '', 400

    # Get the old status
    old_status = media[1].status

    # Set the new status
    media[1].status = new_status

    # Get the old rewatched
    old_rewatch = media[1].rewatched
    # Reset the rewatched time multiplier
    media[1].rewatched = 0

    # Set and change accordingly <last_episode_watched>, <current_season> and <eps_watched> for anime and series
    if list_type == ListType.SERIES or list_type == ListType.ANIME:
        old_watched = media[1].eps_watched
        if new_status == Status.COMPLETED:
            media[1].current_season = len(media[0].eps_per_season)
            media[1].last_episode_watched = media[0].eps_per_season[
                -1].episodes
            media[1].eps_watched = media[0].total_episodes
            new_watched = media[0].total_episodes
        elif new_status == Status.RANDOM or new_status == Status.PLAN_TO_WATCH:
            media[1].current_season = 1
            media[1].last_episode_watched = 0
            media[1].eps_watched = 0
            new_watched = 0
        else:
            new_watched = old_watched
    elif list_type == ListType.MOVIES:
        if new_status == Status.COMPLETED or new_status == Status.COMPLETED_ANIMATION:
            media[1].eps_watched = 1
        else:
            media[1].eps_watched = 0

    # Set the last updates
    set_last_update(media=media[0],
                    media_type=list_type,
                    old_status=old_status,
                    new_status=new_status)

    # Compute the new time spent
    if list_type == ListType.SERIES or list_type == ListType.ANIME:
        compute_time_spent(media=media[0],
                           list_type=list_type,
                           old_watched=old_watched,
                           new_watched=new_watched,
                           old_rewatch=old_rewatch)
    elif list_type == ListType.MOVIES:
        compute_time_spent(media=media[0],
                           list_type=list_type,
                           movie_status=media[1].status,
                           old_rewatch=old_rewatch,
                           movie_runtime=media[0].runtime)

    db.session.commit()
    app.logger.info(
        "[User {}] {}'s category [ID {}] changed from {} to {}.".format(
            current_user.id, media_id, list_type.value.replace('list', ''),
            old_status.value, new_status.value))

    return '', 204
Ejemplo n.º 7
0
def update_element_episode():
    try:
        json_data = request.get_json()
        new_episode = int(json_data['episode']) + 1
        media_id = int(json_data['element_id'])
        media_list = json_data['element_type']
    except:
        return '', 400

    # Check if the media_list exist and is valid
    try:
        list_type = ListType(media_list)
    except ValueError:
        return '', 400
    finally:
        if list_type == ListType.MOVIES:
            return '', 400

    # Check if the media exists
    media = check_media(media_id, list_type)
    if not media or media[1].status == Status.RANDOM or media[
            1].status == Status.PLAN_TO_WATCH:
        return '', 400

    # Check if the episode number is between 1 and <last_episode>
    if 1 > new_episode > media[0].eps_per_season[media[1].current_season -
                                                 1].episodes:
        return '', 400

    # Get the old data
    old_season = media[1].current_season
    old_episode = media[1].last_episode_watched
    old_watched = media[1].eps_watched

    # Set the new data
    new_watched = sum(
        [x.episodes
         for x in media[0].eps_per_season[:old_season - 1]]) + new_episode
    media[1].last_episode_watched = new_episode
    media[1].eps_watched = new_watched + (media[1].rewatched *
                                          media[0].total_episodes)
    app.logger.info(
        '[User {}] {} [ID {}] episode updated from {} to {}'.format(
            current_user.id, list_type.value.replace('list', ''), media_id,
            old_episode, new_episode))

    # Commit the changes
    db.session.commit()

    # Set the last updates
    set_last_update(media=media[0],
                    media_type=list_type,
                    old_season=old_season,
                    new_season=old_season,
                    old_episode=old_episode,
                    new_episode=new_episode)

    # Compute the new time spent
    compute_time_spent(media=media[0],
                       old_watched=old_watched,
                       new_watched=new_watched,
                       list_type=list_type)

    return '', 204