def get_all_songs_from_playlist(session: Session, playlist_id: int, offset: int = 0, limit: int = 10, song_title: str = None, song_artist: str = None) -> list: if song_title and song_artist: songs_result = session.query(Song).filter(Song.title == song_title, Song.artist == song_artist, Song.playlists.any(id = playlist_id))\ .offset(offset)\ .limit(limit)\ .all() elif song_title: songs_result = session.query(Song).filter(Song.title == song_title, Song.playlists.any(id = playlist_id)) \ .offset(offset) \ .limit(limit) \ .all() elif song_artist: songs_result = session.query(Song).filter(Song.artist == song_artist, Song.playlists.any(id = playlist_id)) \ .offset(offset) \ .limit(limit) \ .all() else: songs_result = session.query(Song).filter(Song.playlists.any(id = playlist_id)) \ .offset(offset) \ .limit(limit) \ .all() return songs_result
def create_song(user): form = request.form song_file = request.files['file'] import os filename, file_extension = os.path.splitext(song_file.filename) if file_extension != ".wav" and file_extension != ".mp3": abort(400) song_new_filename = utils.generate_uuid() song_url = aws.upload_song(song_new_filename, file_extension, song_file) session = Session() crud_song.create_song(session, user_id=user.id, song_title=form['title'], song_artist=form['artist'], song_album=form['album'], song_release_year=int(form['releaseYear']), song_url=song_url) session.close() return Response(status=200)
def delete_user(user): session = Session() data = crud_song.get_all_songs_from_user(session, offset=0, limit=10, user_id=user.id) super_user = crud_user.get_user_by_email(session, "*****@*****.**") if not super_user: create_secure_user(session, first_name="admin", last_name="", email="*****@*****.**", password="******") super_user = crud_user.get_user_by_email(session, "*****@*****.**") for item in data: crud_song.update_song_ownership(session, item.id, super_user.id) crud_user.delete_user(session, user.id) session.close() return Response(status=200)
def update_song_ownership(session: Session, song_id: int, new_owner_user_id: int) -> bool: song = session.query(Song).filter_by(id = song_id).first() if song: song.user_id = new_owner_user_id session.commit() return True else: return False
def create_playlist(user): data = request.get_json() session = Session() crud_playlist.create_playlist(session, user_id=user.id, playlist_name=data['name']) session.close() return Response(status=200)
def update_playlist(session: Session, playlist_id: int, playlist_name: str) -> bool: playlist = session.query(Playlist).filter(Playlist.id == playlist_id) if playlist: playlist.name = playlist_name session.commit() return True else: return False
def create_song(session: Session, user_id: int, song_title: str, song_artist: str, song_album: str, song_release_year: int, song_url: str) -> bool: song = Song(user_id = user_id, title = song_title, artist = song_artist, album = song_album, release_year = song_release_year, url = song_url) session.add(song) session.commit() return True
def update_studio(): try: print("Input name:") name = input() print("Input new name:") new_name = input() print("Input country:") country = input() if name and (new_name or country): s = Session() studio = s.query(Studio).filter_by(name=name).first() if new_name: studio.name = new_name if country: studio.country = country s.add(studio) s.commit() s.close() View.display_success() else: print("Incorrect input") except SQLAlchemyError: View.display_error() except ValueError: print("Incorrect input")
def add_location(): """ Adds a new location to the database from the given form. Only reachable if the user is logged in. """ if not current_user.is_authenticated: flash('Please login or register to add locations.') return redirect(url_for('index')) form = AddLocationForm() if form.validate_on_submit(): s = Session() new_loc = Location(name=form.name.data, content=form.content.data, user_id=current_user.get_id()) s.add(new_loc) s.commit() # get the full data of the new loc loc = s.query(Location).filter_by(name=form.name.data).first() s.close() flash('"{}" has been added as a location!'.format(form.name.data)) return redirect(url_for('location', loc_name=loc.name)) return render_template('locations/add_location_form.html', form=form)
def update_actor(): try: print("Input name:") name = input() print("Input new name:") new_name = input() print("Input gender:") gender = input() print("Input age:") age = int(input()) if name and (new_name or gender or age): s = Session() actor = s.query(Actor).filter_by(full_name=name).first() if new_name: actor.full_name = new_name if gender: actor.gender = gender if age: actor.age = age s.add(actor) s.commit() s.close() View.display_success() else: print("Incorrect input") except SQLAlchemyError: View.display_error() except ValueError: print("Incorrect input")
def update_movie(): try: print("Input name:") name = input() print("Input new name:") new_name = input() print("Input genre:") genre = input() print("Input description:") description = input() print("Input budget:") budget = int(input()) if name and (new_name or genre or description or budget): s = Session() movie = s.query(Movie).filter_by(name=name).first() if new_name: movie.name = new_name if genre: movie.genre = genre if description: movie.description = description if budget: movie.budget = budget s.add(movie) s.commit() s.close() View.display_success() else: print("Incorrect input") except SQLAlchemyError: View.display_error() except ValueError: print("Incorrect input")
def show_studios(): try: s = Session() studios = s.query(Studio).all() s.close() if studios: View.display_studios(studios) else: View.display_no_results() except SQLAlchemyError: View.display_error()
def show_movies(): try: s = Session() movies = s.query(Movie).all() s.close() if movies: View.display_movies(movies) else: View.display_no_results() except SQLAlchemyError: View.display_error()
def show_actors(): try: s = Session() actors = s.query(Actor).all() s.close() if actors: View.display_actors(actors) else: View.display_no_results() except SQLAlchemyError: View.display_error()
def location(loc_name): """ Displays the page about the given location. :param loc_name: str - name of the location """ s = Session() loc = s.query(Location).filter_by(name=loc_name).first() s.close() return render_template('locations/location.html', location=loc)
def update_song(session: Session, song_id: int, song_title: str, song_album : str, song_release_year: int, song_url: str) -> bool: song = session.query(Song).filter_by(id = song_id).first() if song: song.title = song_title if song_title else song.title song.album = song_album if song_album else song_album song.release_year = song_release_year if song_release_year else song_release_year song.url = song_url if song_url else song.url session.commit() return True else: return False
def get_entry(id_): """ Gets the Entry corresponding to id_ from the database. :type id_: int :return: Entry """ s = Session() session_entry = s.query(Entry).filter_by(id=id_).first() s.close() return session_entry
def get_all_characters(): """ Gets a list of all characters in the database and the template displays their name and race. """ s = Session() try: characters = s.query(Character).all() s.close() return render_template("characters/character_list.html", characters=characters) except Exception as e: return str(e)
def get_pc(pc_id): """ Gets the Character corresponding to pc_id from the database. :type pc_id: str :return: Character """ s = Session() character = s.query(Character).filter_by(name=pc_id).first() s.close() return character
def get_user_playlists(user): args = request.args offset = args.get('offset') if 'offset' in args else 0 limit = args.get('limit') if 'limit' in args else None session = Session() data = crud_playlist.get_all_playlists(session, offset=offset, limit=limit, user_id=user.id) session.close() return jsonify(PlaylistSerializer.serialize(data, many=True))
def all_entries(): """ Gets a list of all Entries and displays their title and creation time in the template. """ s = Session() try: entries_list = s.query(Entry).all() s.close() return render_template("entries/entries_list.html", entries=entries_list) except Exception as e: return str(e)
def delete_song(user, song_id): # if there isn't any super user, create one session = Session() super_user = crud_user.get_user_by_email(session, "*****@*****.**") if not super_user: create_secure_user(session, first_name="admin", last_name="", email="*****@*****.**", password="******") super_user = crud_user.get_user_by_email(session, "*****@*****.**") song = crud_song.get_song(session, song_id=song_id) if not song: session.close() abort(404) if song.user_id != user.id: session.close() abort(403) crud_song.update_song_ownership(session, song_id, super_user.id) session.close() return Response(status=200)
def get_songs_from_playlist(user, playlist_id): args = request.args session = Session() playlist = crud_playlist.get_playlist(session, playlist_id=playlist_id) if not playlist: session.close() abort(404) if playlist.user_id != user.id: session.close() abort(403) offset = args.get('offset') if 'offset' in args else 0 limit = args.get('limit') if 'limit' in args else None title = args.get('title') artist = args.get('artist') data = crud_song.get_all_songs_from_playlist(session, playlist_id=playlist_id, offset=offset, limit=limit, song_title=title, song_artist=artist) session.close() return jsonify(SongSerializer.serialize(data, many=True))
def all_locations(): """ Gets a list of all locations from the database and passes it to the template. The template iterates through the list and displays some info on the location. """ s = Session() try: locations_list = s.query(Location).all() s.close() return render_template("locations/locations_list.html", locations=locations_list) except Exception as e: return str(e)
def add_character(): """ Presents a form to create a character that is added to the database. Flashes success and redirects to the page of that character. """ if not current_user.is_authenticated: flash('Please login or register to add entries.') return redirect(url_for('index')) form = AddCharacterForm() if form.validate_on_submit(): s = Session() if form.char_class.data == 'Select a Class': form.char_class.data = None if form.race.data == 'Select a Race': form.race.data = None character = Character( name=form.name.data, desc=form.desc.data, race=form.race.data, char_class=form.char_class.data, player_character=form.player_character.data ) s.add(character) s.commit() s.close() flash('"{}" has been added as a character!'.format(form.name.data)) return redirect(url_for('view_character', pc_id=form.name.data)) return render_template('characters/add_char_form.html', form=form)
def create_movie(): try: print("Input name:") name = input() print("Input genre:") genre = input() print("Input description:") description = input() print("Input budget:") budget = int(input()) if name and genre and description and budget: s = Session() s.add( Movie(name=name, genre=genre, description=description, budget=budget)) s.commit() s.close() View.display_success() else: print("Incorrect input") except SQLAlchemyError: View.display_error() except ValueError: print("Incorrect input")
def update_user(session: Session, user_id: str, user_first_name: str, user_last_name: str, user_email: str, user_password: str, user_password_salt: str) -> bool: user = session.query(User).filter_by(id=user_id).first() if not user: return False user.first_name = user_first_name if user_first_name else user.first_name user.last_name = user_last_name if user_last_name else user.last_name user.email = user_email if user_email else user.email user.password = user_password if user_password else user.password user.password_salt = user_password_salt if user_password_salt else user.password_salt session.commit() return True
def create_user(session: Session, user_first_name: str, user_last_name: str, user_email: str, user_password: str, user_password_salt: str, user_auth_token: str) -> bool: result = session.query(exists().where(User.email == user_email)).scalar() if result: return False user = User(first_name=user_first_name, last_name=user_last_name, email=user_email, password_hashed=user_password, password_salt=user_password_salt, auth_token=user_auth_token) session.add(user) session.commit() return True
def show_actor(): try: print("Input name:") name = input() if name: s = Session() actor = s.query(Actor).filter_by(full_name=name).first() s.close() if actor is None: View.display_no_results() else: View.display_actor(actor) else: print("Incorrect input") except SQLAlchemyError: View.display_error()
def get_all_playlists(session: Session, user_id: int, offset: int = 0, limit: int = 10) -> list: return session.query(Playlist).filter_by(user_id = user_id)\ .offset(offset)\ .limit(limit)