def update_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 <media_list> exist and is valid try: list_type = ListType(media_list) models = get_models_group(list_type) except ValueError: return '', 400 # Check if the media exists media = models[1].query.filter_by(user_id=current_user.id, media_id=media_id).first() if not media: return '', 400 # Check if the episode number is between 1 and <last_episode> if 1 > new_episode or new_episode > media.media.eps_per_season[ media.current_season - 1].episodes: return '', 400 # Get the old data old_season = media.current_season old_episode = media.last_episode_watched old_total = media.total # Set the new data new_watched = sum( [x.episodes for x in media.media.eps_per_season[:old_season - 1]]) + new_episode media.last_episode_watched = new_episode new_total = new_watched + (media.rewatched * media.media.total_episodes) media.total = new_total app.logger.info( f"[User {current_user.id}] {list_type} [ID {media_id}] episode updated to {new_episode}" ) # Set the last updates UserLastUpdate.set_last_update(media=media.media, media_type=list_type, old_season=old_season, new_season=old_season, old_episode=old_episode, new_episode=new_episode) # Compute new time spent media.compute_new_time_spent(old_data=old_total, new_data=new_total) # Commit the changes db.session.commit() return '', 204
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 <media_list> try: list_type = ListType(media_list) models = get_models_group(list_type) except: return '', 400 # Check <status> parameter try: new_status = Status(media_cat) except: return '', 400 # Check that the <media> is not in the user's list in_list = models[1].query.filter_by(user_id=current_user.id, media_id=media_id).first() if in_list: return '', 400 # Check if <media> exists media = models[0].query.filter_by(id=media_id).first() if not media: return '', 400 # Add the media to the user new_watched = media.add_media_to_user(new_status) # Commit the changes db.session.commit() app.logger.info( f"[User {current_user.id}] {list_type} Added [ID {media_id}] in the category: {new_status}" ) # Set the last update UserLastUpdate.set_last_update(media=media, media_type=list_type, new_status=new_status) # Compute new time spent in_list = models[1].query.filter_by(user_id=current_user.id, media_id=media_id).first() in_list.compute_new_time_spent(new_data=new_watched) # Commit the last updates and the new time spent changes db.session.commit() return '', 204
def update_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 if <media_list> exist and is valid try: list_type = ListType(media_list) models = get_models_group(list_type) except: return '', 400 # Check the <status> parameter try: new_status = Status(media_new_cat) except: return '', 400 # Get the media media = models[1].query.filter_by(user_id=current_user.id, media_id=media_id).first() if not media: return '', 400 # Change the <status> and get the data to compute <last_updates> and <new_time_spent> try: old_total = media.total except: old_total = media.playtime old_status = media.status new_total = media.category_changes(new_status) # Set the last updates UserLastUpdate.set_last_update(media=media.media, media_type=list_type, old_status=old_status, new_status=new_status, old_playtime=old_total, new_playtime=new_total) # Compute the new time spent media.compute_new_time_spent(old_data=old_total, new_data=new_total) # Commit the session db.session.commit() app.logger.info( f"[User {current_user.id}] {list_type}'s category [ID {media_id}] changed to {new_status}" ) return '', 204
def update_page(): try: json_data = request.get_json() new_page = int(json_data['page']) media_id = int(json_data['element_id']) media_list = json_data['element_type'] except: return '', 400 # Check if <media_list> exist and is valid try: list_type = ListType(media_list) models = get_models_group(list_type) except ValueError: return '', 400 # Check if the media exists media = models[1].query.filter_by(user_id=current_user.id, media_id=media_id).first() if not media: return '', 400 # Check if the page number is between 0 and max(pages) if new_page > int(media.media.pages) or new_page < 0: return "The number of pages cannot be below 0 or greater than the total pages.", 400 # Get the old data old_page = media.actual_page old_total = media.total # Set the new data media.actual_page = new_page new_total = new_page + (media.rewatched * media.media.pages) media.total = new_total app.logger.info( f"[User {current_user.id}] {list_type} [ID {media_id}] page updated from {old_page} to {new_page}" ) # Set the last updates UserLastUpdate.set_last_update(media=media.media, media_type=list_type, old_page=old_page, new_page=new_page, old_status=media.status) # Compute new time spent media.compute_new_time_spent(old_data=old_total, new_data=new_total) # Commit the changes db.session.commit() return '', 204
def set_last_update(media, media_type, old_status=None, new_status=None, old_season=None, new_season=None, old_episode=None, new_episode=None): check = UserLastUpdate.query.filter_by(user_id=current_user.id, media_type=media_type, media_id=media.id) \ .order_by(UserLastUpdate.date.desc()).first() diff = 10000 if check: diff = (datetime.utcnow() - check.date).total_seconds() update = UserLastUpdate(user_id=current_user.id, media_name=media.name, media_id=media.id, media_type=media_type, old_status=old_status, new_status=new_status, old_season=old_season, new_season=new_season, old_episode=old_episode, new_episode=new_episode, date=datetime.utcnow()) if diff > 600: db.session.add(update) else: db.session.delete(check) db.session.add(update) db.session.commit()
def update_playtime(): try: json_data = request.get_json() new_playtime = int(json_data['playtime']) * 60 # To get minutes media_id = int(json_data['media_id']) media_list = json_data['media_type'] except: return '', 400 if new_playtime < 0: return '', 400 # Check if <media_list> exist and valid try: list_type = ListType(media_list) models = get_models_group(list_type) except ValueError: return '', 400 media = models[1].query.filter_by(user_id=current_user.id, media_id=media_id).first() if not media: return '', 400 # Set the last updates UserLastUpdate.set_last_update(media=media.media, media_type=list_type, old_playtime=media.playtime, new_playtime=new_playtime, old_status=media.status) # Compute the new time spent media.compute_new_time_spent(new_data=new_playtime) # Update new playtime media.playtime = new_playtime # Commit the changes db.session.commit() app.logger.info( f"[{current_user.id}] Games ID {media_id} playtime updated to {new_playtime}" ) return '', 204