def persons(media_type, job, person): # Check if <media_type> is valid try: models = get_models_group(MediaType(media_type)) except ValueError: return abort(400) data = models[0].get_persons(job, person) return render_template('persons.html', title=person, person=person, data=data)
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)
def write_comment(media_type, media_id): # Check if <media_type> is valid try: models = get_models_group(MediaType(media_type)) except ValueError: return abort(400) # Check if the <media> is in the current user's list media = models[1].query.filter_by(user_id=current_user.id, media_id=media_id).first() if not media: return abort(400) form = MediaComment() if request.method == 'GET': form.comment.data = media.comment session['back_url'] = request.referrer or '/' if form.validate_on_submit(): comment = form.comment.data media.comment = comment db.session.commit() app.logger.info( f"[{current_user.id}] added a comment on {media_type} with ID [{media_id}]" ) if not comment or comment == '': flash('Your comment has been removed/is empty.', 'warning') else: flash('Your comment has been added/modified.', 'success') return redirect(session['back_url']) return render_template('medialist_comment.html', title='Add comment', form=form, media_name=media.media.name)
def media_sheet_form(media_type, media_id): if current_user.role == RoleType.USER: abort(403) form = EditMediaData() # 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 media = Series.query.filter_by(id=media_id).first() elif media_type == MediaType.ANIME: list_type = ListType.ANIME media = Anime.query.filter_by(id=media_id).first() elif media_type == MediaType.MOVIES: list_type = ListType.MOVIES media = Movies.query.filter_by(id=media_id).first() if not media: abort(404) if request.method == 'GET': form.original_name.data = media.original_name form.name.data = media.name form.homepage.data = media.homepage form.synopsis.data = media.synopsis # form.genres.data = ', '.join([r.genre for r in media.genres]) form.actors.data = ', '.join([r.name for r in media.actors]) if list_type != ListType.MOVIES: form.created_by.data = media.created_by form.first_air_date.data = media.first_air_date form.last_air_date.data = media.last_air_date form.production_status.data = media.status form.duration.data = media.episode_duration form.origin_country.data = media.origin_country form.networks.data = ', '.join([r.network for r in media.networks]) elif list_type == ListType.MOVIES: form.directed_by.data = media.director_name form.release_date.data = media.release_date form.duration.data = media.runtime form.original_language.data = media.original_language form.tagline.data = media.tagline form.budget.data = media.budget form.revenue.data = media.revenue if form.validate_on_submit(): if form.cover.data: media.image_cover = save_new_cover(form.cover.data, media_type) media.original_name = form.original_name.data media.name = form.name.data media.homepage = form.homepage.data media.synopsis = form.synopsis.data if list_type != ListType.MOVIES: media.created_by = form.created_by.data media.first_air_date = form.first_air_date.data media.last_air_date = form.last_air_date.data media.status = form.production_status.data media.episode_duration = form.duration.data media.origin_country = form.origin_country.data elif list_type == ListType.MOVIES: media.director_name = form.directed_by.data media.release_date = form.release_date.data media.runtime = form.duration.data media.original_language = form.original_language.data media.tagline = form.tagline.data media.budget = form.budget.data media.revenue = form.revenue.data db.session.commit() if list_type == ListType.SERIES or list_type == ListType.ANIME: if [r.name for r in media.actors] == form.actors.data.split(', '): pass else: for actor in [r.name for r in media.actors]: if list_type == ListType.SERIES: SeriesActors.query.filter_by(media_id=media_id, name=actor).delete() elif list_type == ListType.ANIME: AnimeActors.query.filter_by(media_id=media_id, name=actor).delete() elif list_type == ListType.MOVIES: MoviesActors.query.filter_by(media_id=media_id, name=actor).delete() db.session.commit() for actor in form.actors.data.split(', '): if list_type == ListType.SERIES: add_actor = SeriesActors(media_id=media_id, name=actor) elif list_type == ListType.ANIME: add_actor = AnimeActors(media_id=media_id, name=actor) elif list_type == ListType.MOVIES: add_actor = MoviesActors(media_id=media_id, name=actor) db.session.add(add_actor) db.session.commit() # Genres # if [r.genre for r in element.genres] == form.genres.data.split(', '): # pass # else: # for genre in [r.genre for r in element.genres]: # if list_type == ListType.SERIES: # SeriesGenre.query.filter_by(media_id=media_id, genre=genre).delete() # elif list_type == ListType.ANIME: # AnimeGenre.query.filter_by(media_id=media_id, genre=genre).delete() # elif list_type == ListType.MOVIES: # MoviesGenre.query.filter_by(media_id=media_id, genre=genre).delete() # db.session.commit() # for genre in form.genres.data.split(', '): # if list_type == ListType.SERIES: # add_genre = SeriesGenre(media_id=media_id, # genre=genre) # elif list_type == ListType.ANIME: # add_genre = AnimeGenre(media_id=media_id, # genre=genre) # elif list_type == ListType.MOVIES: # add_genre = MoviesGenre(media_id=media_id, # genre=genre) # db.session.add(add_genre) # db.session.commit() # Networks elif list_type != ListType.MOVIES: if [r.network for r in media.networks] == form.networks.data.split(', '): pass else: for network in [r.network for r in media.networks]: if list_type == ListType.SERIES: SeriesNetwork.query.filter_by( media_id=media_id, network=network).delete() elif list_type == ListType.ANIME: AnimeNetwork.query.filter_by(media_id=media_id, network=network).delete() db.session.commit() for network in form.networks.data.split(', '): if list_type == ListType.SERIES: add_network = SeriesNetwork(media_id=media_id, network=network) elif list_type == ListType.ANIME: add_network = AnimeNetwork(media_id=media_id, network=network) db.session.add(add_network) db.session.commit() return redirect( url_for('main.media_sheet', media_type=media_type.value, media_id=media_id)) return render_template('media_sheet_form.html', title='Media Form', form=form, media_type=media_type.value)
def media_sheet(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 # Check if <media_id> came from an API and if in local DB api_id = request.args.get('search') if api_id: search = {'themoviedb_id': media_id} else: search = {'id': media_id} html_template = 'media_sheet_tv.html' if list_type == ListType.SERIES: media = Series.query.filter_by(**search).first() elif list_type == ListType.ANIME: media = Anime.query.filter_by(**search).first() elif list_type == ListType.MOVIES: media = Movies.query.filter_by(**search).first() html_template = 'media_sheet_movies.html' # If <media> does not exist and <api_id> is provived: Add the <media> to DB, else abort. if not media: if api_id: try: media_api_data = ApiData().get_details_and_credits_data( media_id, list_type) except Exception as e: app.logger.error( '[ERROR] - Impossible to get the details and credits from API ({}) ID [{}]: {}' .format(media_type.value, media_id, e)) flash( 'Sorry, a problem occured trying to load the media info. Please try again later.', 'warning') return redirect(request.referrer) try: media_details = MediaDetails(media_api_data, list_type).get_media_details() except Exception as e: app.logger.error( '[ERROR] - Occured trying to parse the API data to dict ({}) ID [{}]: {}' .format(media_type.value, media_id, e)) flash( 'Sorry, a problem occured trying to load the media info. Please try again later.', 'warning') return redirect(request.referrer) try: media = AddtoDB(media_details, list_type).add_media_to_db() except Exception as e: app.logger.error( '[ERROR] - Occured trying to add media ({}) ID [{}] to DB: {}' .format(media_type.value, media_id, e)) flash( 'Sorry, a problem occured trying to load the media info. Please try again later.', 'warning') return redirect(request.referrer) else: abort(404) # If <media> and <api_id> provived: redirect to get a nice URL. if media and api_id: return redirect( url_for('main.media_sheet', media_type=media_type.value, media_id=media.id)) media_info = MediaDict(media, list_type).create_list_dict() title = media_info['display_name'] return render_template(html_template, title=title, data=media_info, media_list=list_type.value)
def media_sheet_form(media_type, media_id): if current_user.role == RoleType.USER: abort(403) # Check if <media_type> is valid try: models = get_models_group(MediaType(media_type)) except ValueError: return abort(400) # Get the media and check if it exists media = models[0].query.filter_by(id=media_id).first() if not media: flash("The media does not exist.", 'warning') return redirect(request.referrer or '/') # Class which converts the attributs of a <SQLAlchemy_model> to a <FlaskForm> class Form(ModelForm): class Meta: csrf = False model = models[0] only = models[0].form_only() image_cover = StringField('insert an img URL') # If the media is a book also display a genre attr if media_type == 'Books': genre_attr = SelectMultipleField( 'Genres', choices=[('Action & Adventure', 'Action & Adventure'), ('Biography', 'Biography'), ('Chick lit', 'Chick lit'), ('Children', 'Children'), ('Classic', 'Classic'), ('Crime', 'Crime'), ('Drama', 'Drama'), ('Dystopian', 'Dystopian'), ('Fantastic', 'Fantastic'), ('Fantasy', 'Fantasy'), ('History', 'History'), ('Humor', 'Humor'), ('Horror', 'Horror'), ('Mystery', 'Mystery'), ('Paranormal', 'Paranormal'), ('Philosophy', 'Philosophy'), ('Poetry', 'Poetry'), ('Romance', 'Romance'), ('Science', 'Science'), ('Science-Fiction', 'Science-Fiction'), ('Short story', 'Short story'), ('Suspense', 'Suspense'), ('Thriller', 'Thriller'), ('Western', 'Western'), ('Young adult', 'Young adult')]) # Populate the form with the model data except for the <image_cover> form = Form(obj=media) if request.method == 'GET': form.image_cover.data = None genres = None if media_type == 'Books': genres = db.session.query(func.group_concat(BooksGenre.genre.distinct())) \ .filter(BooksGenre.media_id == media_id).first() if form.is_submitted(): if form.image_cover.data == "": picture_fn = media.image_cover else: picture_fn = secrets.token_hex(8) + '.jpg' picture_path = Path(app.root_path, f"static/covers/{media_type.lower()}_covers", picture_fn) try: urlretrieve(f"{form.image_cover.data}", f"{picture_path}") img = Image.open(f"{picture_path}") img = img.resize((300, 450), Image.ANTIALIAS) img.save(f"{picture_path}", quality=90) except Exception as e: app.logger.error( f"[SYSTEM] - Error occured updating media cover: {e}") flash(str(e), 'warning') picture_fn = media.image_cover form.image_cover.data = picture_fn form.populate_obj(media) db.session.add(media) db.session.commit() # If media_type is Books check the genre form. if media_type == 'Books': if form.genre_attr.data: try: BooksGenre.query.filter( BooksGenre.media_id == media_id).delete() for genre in form.genre_attr.data[:5]: adding = BooksGenre(genre=genre, media_id=media_id) db.session.add(adding) db.session.commit() flash('Data successfully updated.', 'success') except: db.session.rollback() flash('Error while updating the genres.', 'warning') return redirect( url_for('main.media_sheet', media_type=media_type, media_id=media_id)) return render_template('media_sheet_form.html', title='Media Form', form=form, genres=genres, media_type=media_type)
def media_sheet(media_type, media_id): # Check if <media_type> is valid try: media_type = MediaType(media_type) models = get_models_group(media_type) list_type = ListType(media_type.value.lower() + 'list') except ValueError: abort(400) # Check if <media_id> came from an API from_api = request.args.get('search') # Check <media> in local DB search = {'id': media_id} if from_api: search = {'api_id': media_id} media = models[0].query.filter_by(**search).first() # If not <media> and <api_id>: Add <media> to DB, else abort. if not media: if from_api: API_model = ApiData.get_API_model(media_type) try: media = API_model(API_id=media_id).save_media_to_db() db.session.commit() except Exception as e: flash( 'Sorry, a problem occured trying to load the media info. Please try again later.', 'warning') app.logger.error( '[ERROR] - Occured trying to add media ({}) ID [{}] to DB: {}' .format(list_type.value, media_id, e)) location = request.referrer or '/' return redirect(location) else: abort(400) # If <media> and <api_id>: redirect for URL with media.id instead of media.api_id if media and from_api: return redirect( url_for('main.media_sheet', media_type=media_type.value, media_id=media.id)) # Get the list info of the user on this media list_info = media.get_user_list_info() # Get the HTML template template = models[0].media_sheet_template() # Get the history of the media for the user media_updates = UserLastUpdate.query.filter(UserLastUpdate.user_id == current_user.id, UserLastUpdate.media_type == list_type, UserLastUpdate.media_id == media_id)\ .order_by(UserLastUpdate.date.desc()).all() history = current_user._shape_to_dict_updates(media_updates) # Get the Genre form for books form = GenreForm() form_cover = CoverForm() genres = None if list_type == ListType.BOOKS: genres = db.session.query(func.group_concat(BooksGenre.genre.distinct())) \ .filter(BooksGenre.media_id == media_id).first() return render_template(template, title=media.name, media=media, list_info=list_info, form=form, genres=genres, media_list=list_type.value, form_cover=form_cover, media_updates=history)