def add_promo(user_id): """ Add Promo route. Adds clip to whatever the current show that is being edited. """ # Get current user that the promo is being added to. current_user = db.session.query(User).filter( User.id == user_id ).first() # Ensure user exists. if not current_user: flash("User not found.", category="error") abort(404) form = NewPromoForm() # On POST, handle new promo creation. if request.method == "POST" and form.validate_on_submit(): # Save file locally. uploaded_file = form.save_uploaded_file() # Get screencap from uploaded video. screencap = screencap_from_video(uploaded_file) # If we don't have both the uploaded file and the screencap, error out. if not all([uploaded_file, screencap]): flash("Error uploading promo.", category="error") abort(400) # Try to upload both files to GoogleStorage. try: # Initialize GoogleStorage client. storage = GoogleStorage() # Upload screencap. screencap_url = storage.upload_file(screencap, bucket="promo_images") # Upload video. video_file = storage.upload_file(uploaded_file, bucket="promo_videos") # Create new Promo object and add it to current user. current_user.promos.append(Promo( name=form.promo_name.data, description=form.description.data, clip_url=video_file, image_url=screencap_url, )) db.session.commit() except Exception as err: logging.error("Error uploading promo to user {}: {}, {}".format(current_user.id, type(err), err)) flash("Error creating promo, please try again.", category="error") abort(400) flash("Promo created successfully.", category="success") return render_template("admin/add_promo.html", form=form, current_user=current_user)
def add_clip(channel_id, show_id): """ Add Clip route. Clip will be added to whatever the current show that is being edited. """ form = NewClipForm() # POST method is the actual uploading of a new clip. if request.method == "POST" and form.validate_on_submit(): # Save the uploaded file. uploaded_file = form.save_uploaded_file() # Save a screencap from the video to use as a preview image. screencap = screencap_from_video(uploaded_file) # Ensure the uploaded file and screencap were saved successfully. if not all([uploaded_file, screencap]): flash("Error uploading clip, please try again.") abort(400) # Attempt to upload the video and the screencap to google. try: # Pull show that this clip will be associated with. show = db.session.query(Show).filter(Show.channel_id == channel_id, Show.id == show_id).first() # Initialize GoogleStorage client. storage = GoogleStorage() # Upload screencap to GoogleStorage. screencap_url = storage.upload_file(screencap, "clip_images") # Upload clip to GoogleStorage. clip_url = storage.upload_file(uploaded_file, "clip_videos") # Create new Clip object and add it to current show. show.clips.append( Clip( name=form.clip_name.data, description=form.description.data, clip_url=clip_url, image_url=screencap_url, )) db.session.commit() except Exception as err: logging.error("Error creating new clip for show {}: {} {}".format( show.id, type(err), err)) flash("Error creating clip, please try again.", category="error") abort(400) flash("Clip created successfully.", category="success") return render_template("admin/add_clip.html", form=form)
def add_loop(user_id): # Pull user to create loop for. current_user = db.session.query(User).filter( User.id == user_id ).first() # Initialize form. form = NewLoopForm() # Handle new loop creation. if request.method == "POST" and form.validate_on_submit(): # Save image file locally. uploaded_file = form.save_uploaded_file() # Ensure file saved successfully. if not uploaded_file: flash("Error uploading loop, please try again.", category="error") abort(400) # Attempt to upload image to GoogleStorage. try: # Initialize google storage client. storage = GoogleStorage() # Upload image to correct bucket. image_url = storage.upload_file(uploaded_file, bucket="loop_images") # Get play list from form. playlist_data = json.loads(form.loop_data.data).get("data") # Append new loop to current user. current_user.loops.append(Loop( name=form.loop_name.data, image_url=image_url, playlist=playlist_data )) db.session.commit() flash("Successfully created new loop.", category="success") except Exception as err: logging.error("Error uploading loop for user {}: {} {}".format(current_user.id, type(err), err)) flash("Error uploading loop, please try again.") abort(400) # Get all available Shows and Promos for current user. shows = Show.query.all() promos = Promo.query.filter_by(user_id=user_id).all() return render_template("admin/add_loop.html", current_user=current_user, shows=shows, promos=promos, form=form)
def add_channel(): """ Channel creation route """ form = NewChannelForm() # Handle post (actual channel creation). if request.method == "POST" and form.validate_on_submit(): # Complete file upload by saving the file locally. uploaded_file = form.save_uploaded_file() # Ensure the file was uploaded successfully. if not uploaded_file: flash("Error processing your upload. Please try again.") abort(400) # Check if uploaded image meets required image dimensions. If it # does not, then resize it. if get_image_size(uploaded_file) != (512, 288): resize_image(uploaded_file) # Attempt google storage upload. try: # Initialize storage client. storage = GoogleStorage() # Try and upload file. url = storage.upload_file(uploaded_file, bucket="channel_images") # Create the new Channel object to be put into the db. channel = Channel(name=form.channel_name.data, category=form.category.data, description=form.description.data, image_url=url) db.session.add(channel) db.session.commit() flash("Successfully completed.", category="success") except Exception as err: logging.error( "Ran into an error creating a new channel: {} {} ".format( type(err), err)) flash("Error uploading Channel. Please try again.", category="error") return render_template("admin/add_channel.html", form=form)
def edit_loop(user_id, loop_id): """ Edit existing loop route. """ # Pull loop and user from route data. current_loop = db.session.query(Loop).filter( Loop.id == loop_id ).first() current_user = db.session.query(User).filter( User.id == user_id ).first() # Return 404 if either aren't found. if not all([current_loop, current_user]): abort(404) # Initialize form. form = UpdateLoopForm() # Handle loop update post requests. if request.method == "POST" and form.validate_on_submit(): ## Check existing loop attributes vs posted ones to see what we need to update. # Check playlist, if none posted for some reason, use the existing one as the "new value". try: loop_playlist = json.loads(form.loop_data.data).get("data", current_loop.playlist) except ValueError: loop_playlist = current_loop.playlist if current_loop.playlist != loop_playlist: current_loop.playlist = loop_playlist # Check loop name. if current_loop.name != form.loop_name.data: current_loop.name = form.loop_name.data # Check for a newly uploaded image. if form.loop_image.data: uploaded_file = form.save_uploaded_file() error = False if not uploaded_file: error = True else: # Attempt to upload new image try: # Initialize GoogleStorag storage = GoogleStorage() # Upload image. image_url = storage.upload_file(uploaded_file, "loop_images") except Exception as err: error = True # If error, let the user know. Otherwise compare image_url vs existing one. if error: flash("Error updating loop image, please try again.", category="error") else: if current_loop.image_url != image_url: print("Updated image_url") current_loop.image_url = image_url # Commit any changes to the db. db.session.commit() # Pull all shows and promos. shows = db.session.query(Show).all() promos = db.session.query(Promo).filter( Promo.user_id == user_id ).all() # Get loop playlist in the correct format the front end needs. loop_playlist = [] # Iterate each item in the loop's playlist. for i in current_loop.playlist: # Strips the id out of the name. media_id = re.search(r'\d+', i).group() # Add promo if current i is promo/ if 'promo' in i.lower(): promo = db.session.query(Promo).filter( Promo.id == media_id).first() if not promo: continue loop_playlist.append( {'id': promo.id, 'name': promo.name, 'image_url': promo.image_url, 'type': 'promo'}) # Add show if current i is show. else: show = Show.query.filter_by(id=media_id).first() loop_playlist.append({'id': show.id, 'name': show.name, 'image_url': show.clips[-1].image_url, 'type': 'show'}) return render_template("admin/edit_loop.html", loop_playlist=json.dumps(loop_playlist), current_loop=current_loop, current_user=current_user, shows=shows, promos=promos, form=form)
def edit_loop(loop_id): """ Route for editing existing loops. """ # Pull loop specified in route. current_loop = db.session.query(Loop).filter(Loop.id == loop_id).first() if not current_loop: abort(404, {"error": "No channel by that id. (id:{})".format(loop_id)}) # Query Channels for the 4 categories (trends, entertainment, sports, news). channels = db.session.query(Channel).order_by(Channel.id.desc()).all() channel_categories = { "entertainment": [], "sports": [], "news": [], "trends": [] } # Add 10 channels to trends. channel_categories["trends"] = channels[:10] # Iterate Channels and add them to their category. for channel in channels: # Ensure current channel is the dict of categories. if channel.category.lower() in channel_categories: channel_categories[channel.category.lower()].append(channel) # Initialize forms. # Form for new promos. promo_form = NewPromoForm() # Form for editing loop. loop_form = UpdateDashboardLoopForm() # Since we have 2 possible form posts we need to have nested ifs to handle each. if request.method == "POST": # Both forms might use GoogleStorage so initialize it here. try: # Initialize GoogleStorage client. storage = GoogleStorage() except Exception as err: flash("Unable to complete request, please try again later.", category="error") abort(400) # Handle new promo form. if promo_form.validate_on_submit(): # Save uploaded file locally. uploaded_file = promo_form.save_uploaded_file() # Get screencap from uploaded file. screencap = screencap_from_video(uploaded_file) # Ensure upload and screencap were successful. if not all([uploaded_file, screencap]): flash("Error creating loop, please try again.") abort(400) # Attempt to upload to GoogleStorage. try: # Upload screencap. screencap_url = storage.upload_file(screencap, bucket="promo_images") # Upload video. video_url = storage.upload_file(uploaded_file, bucket="promo_videos") # Add new promo with uploaded screencap/video to current user. current_user.promos.append( Promo( name=promo_form.promo_name.data, description=promo_form.description.data, clip_url=video_url, image_url=screencap_url, )) db.session.commit() flash("Promo created successfully.", category="success") # Catch all exception to notify user. except Exception as err: logging.error("Error uploading promo to user {}: {} {}".format( current_user.id, type(err), err)) flash("Error creating promo, please try again.", category="error") abort(400) # Handle loop edit form. elif loop_form.validate_on_submit(): # Check each form attribute vs existing loop attribute to see if we need to update anything. # Check loop name. if current_loop.name != loop_form.loop_name.data: current_loop.name = loop_form.loop_name.data # Check loop playlist, if none posted for some reason, use the existing one as the "new value". try: loop_playlist = json.loads(loop_form.loop_data.data).get( "data", current_loop.playlist) except ValueError: loop_playlist = current_loop.playlist if current_loop.playlist != loop_playlist: current_loop.playlist = loop_playlist # Check loop image. if loop_form.loop_image.data: # Convert b64 string and save it as local image. uploaded_file = b64_image_string_to_file( loop_form.loop_image.data, loop_form.loop_name.data) if not uploaded_file: flash("Error updating loop, please try again.", category="error") abort(400) # Attempt to upload image to GoogleStorage, then update it on the Loop object. try: image_url = storage.upload_file(uploaded_file, bucket="loop_images") current_loop.image_url = image_url except Exception as err: logging.error( "Error uploading image for loop: {}: {} {}".format( current_loop.id, type(err), err)) flash("Error updating loop, please try again.", category="error") abort(400) # Commit any changes and notify success. db.session.commit() flash("Loop updated successfully.", category="success") # Get the current loop's playlist and make it the format the template expects. todo make this into helper function loop_playlist = [] for i in current_loop.playlist: media_id = re.search(r'\d+', i).group() if 'promo' in i.lower(): promo = db.session.query(Promo).filter( Promo.id == media_id).first() # if promo if not promo: continue loop_playlist.append({ 'id': promo.id, 'name': promo.name, 'image_url': promo.image_url, 'type': 'promo' }) else: show = db.session.query(Show).filter(Show.id == media_id).first() loop_playlist.append({ 'id': show.id, 'name': show.name, 'image_url': show.clips[-1].image_url, 'type': 'show' }) return render_template("dashboard/edit_loop.html", form=promo_form, loop_form=loop_form, loop_playlist=json.dumps(loop_playlist), current_loop=current_loop, current_user=current_user, trends=channel_categories["trends"], entertainments=channel_categories["entertainment"], sports=channel_categories["sports"], news=channel_categories["sports"])
def add_loop(): """ Create loop route. (also option to create promo, perhaps make this into 2 routes?) """ # Query Channels for the 4 categories (trends, entertainment, sports, news). channels = db.session.query(Channel).order_by(Channel.id.desc()).all() channel_categories = { "entertainment": [], "sports": [], "news": [], "trends": [] } # Add 10 channels to trends. channel_categories["trends"] = channels[:10] # Iterate Channels and add them to their category. for channel in channels: # Ensure current channel is the dict of categories. if channel.category.lower() in channel_categories: channel_categories[channel.category.lower()].append(channel) # Initialize forms. # Promo form. promo_form = NewPromoForm() # Loop form. loop_form = NewLoopFormDashboard() # Handle form post (loop or promo create). if request.method == "POST": try: # Initialize GoogleStorage client. storage = GoogleStorage() except Exception as err: flash("Unable to complete request, please try again later.", category="error") abort(400) # Handle Promo form post. if promo_form.validate_on_submit(): # Save uploaded file locally. uploaded_file = promo_form.save_uploaded_file() # Get screencap from uploaded file. screencap = screencap_from_video(uploaded_file) # Ensure upload and screencap were successful. if not all([uploaded_file, screencap]): flash("Error creating loop, please try again.") abort(400) # Attempt to upload to GoogleStorage. try: # Upload screencap. screencap_url = storage.upload_file(screencap, bucket="promo_images") # Upload video. video_url = storage.upload_file(uploaded_file, bucket="promo_videos") # Add new promo with uploaded screencap/video to current user. current_user.promos.append( Promo( name=promo_form.promo_name.data, description=promo_form.description.data, clip_url=video_url, image_url=screencap_url, )) db.session.commit() flash("Promo created successfully.", category="success") # Catch all exception to notify user. except Exception as err: logging.error("Error uploading promo to user {}: {} {}".format( current_user.id, type(err), err)) flash("Error creating promo, please try again.", category="error") abort(400) # Handle Loop form post. elif loop_form.validate_on_submit(): # Save loop_image locally from b64 form string. uploaded_file = b64_image_string_to_file(loop_form.loop_image.data, loop_form.loop_name.data) if not uploaded_file: flash("Error creating loop, please try again later.", category="error") abort(400) # Attempt to upload loop image to GoogleStorage. try: image_url = storage.upload_file(uploaded_file, bucket="loop_images") # Get playlist data from form. playlist_data = json.loads( loop_form.loop_data.data).get("data") # Create new loop and add it to current user. current_user.loops.append( Loop(name=loop_form.loop_name.data, playlist=playlist_data, image_url=image_url)) db.session.commit() flash("Successfully created new loop.", category="success") except Exception as err: logging.error("Error uploading loop for user {}: {} {}".format( current_user, type(err), err)) flash("Error creating loop, please try again later.", category="error") abort(400) return render_template("dashboard/add_loop.html", form=promo_form, loop_form=loop_form, current_user=current_user, trends=channel_categories["trends"], entertainments=channel_categories["entertainment"], sports=channel_categories["sports"], news=channel_categories["news"])