def add_contract_to_actor(): try: print("Input movie name:") movie_name = input() print("Input actor name:") actor_name = input() print("Input studio name:") studio_name = input() print("Input value:") value = int(input()) if movie_name and actor_name and studio_name and value: s = Session() movie = s.query(Movie).filter_by(name=movie_name).first() studio = s.query(Studio).filter_by(name=studio_name).first() actor = s.query(Actor).filter_by(full_name=actor_name).first() if (movie is None) or (actor is None) or (studio is None): View.display_no_results() s.close() else: s.add( Contract(movie_id=movie.id, studio_id=studio.id, actor_id=actor.id, value=value)) s.commit() s.close() View.display_success() else: print("Incorrect input") except SQLAlchemyError as e: View.display_error()
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 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 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 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 add_entry(): """ Presents a form to add a new entry to the database. Requires the user to be logged in. """ if not current_user.is_authenticated: flash('Please login or register to add entries.') return redirect(url_for('index')) s = Session() if request.method == 'POST': date = request.form['date'] content = request.form['content'] if not date: flash('Date is required!') else: try: new_entry = Entry(date=date, content=content, user_id=current_user.get_id()) s.add(new_entry) s.commit() s.close() return redirect(url_for('index')) except Exception as e: s.close() return render_template('404_page.html', message=str(e)) s.close() return render_template('entries/add_entry_form.html')
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 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 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 create_studio(): try: print("Input name:") name = input() print("Input country:") country = input() if name and country: s = Session() s.add(Studio( name=name, country=country )) s.commit() s.close() View.display_success() else: print("Incorrect input") except SQLAlchemyError: View.display_error() except ValueError: print("Incorrect input")
def register(): """ Presents an anonymous user with the registration form and adds their info to the database. Uses user.set_password() to hash their password (instead of storing it plaintext). """ if current_user.is_authenticated: return redirect(url_for('index')) form = RegistrationForm() if form.validate_on_submit(): s = Session() user = User(username=form.username.data, email=form.email.data) user.set_password(form.password.data) s.add(user) s.commit() s.close() flash('Congratulations, you are now a registered user!') return redirect(url_for('login')) return render_template('registration.html', form=form)
def add_actor_to_movie(): try: print("Input movie name:") movie_name = input() print("Input actor name:") actor_name = input() if movie_name and actor_name: s = Session() movie = s.query(Movie).filter_by(name=movie_name).first() actor = s.query(Actor).filter_by(full_name=actor_name).first() s.add(movie) if (movie is None) or (actor is None): View.display_no_results() s.close() else: movie.cast.append(actor) View.display_success() s.commit() s.close() else: print("Incorrect input") except SQLAlchemyError as e: View.display_error()
def remove_movie_from_studio(): try: print("Input movie name:") movie_name = input() print("Input studio name:") studio_name = input() if movie_name and studio_name: s = Session() movie = s.query(Movie).filter_by(name=movie_name).first() studio = s.query(Studio).filter_by(name=studio_name).first() s.add(studio) if (movie is None) or (studio is None): View.display_no_results() s.close() else: studio.movies.remove(movie) View.display_success() s.commit() s.close() else: print("Incorrect input") except SQLAlchemyError as e: View.display_error()
def create_actor(): try: print("Input name:") name = input() print("Input gender:") gender = input() print("Input age:") age = int(input()) if name and gender and age: s = Session() s.add(Actor( full_name=name, gender=gender, age=age, )) s.commit() s.close() View.display_success() else: print("Incorrect input") except SQLAlchemyError: View.display_error() except ValueError: print("Incorrect input")
def run(): start_time = datetime.now() # turn on the program flag global program_flag program_flag = True # program kill switch def signal_handler(number, frame): global program_flag program_flag = False # kill signal handler signal.signal(signal.SIGTERM, signal_handler) # Create Reddit Instance reddit = praw.Reddit( user_agent=config('REDDIT_USER_AGENT'), client_id=config('REDDIT_CLIENT_ID'), client_secret=config('REDDIT_CLIENT_SECRET'), username=config('REDDIT_USERNAME'), password=config('REDDIT_PASSWORD'), ) subreddit = reddit.subreddit("wallstreetbets") # Parse XML File tree = ET.parse("StockListComplete.xml") root = tree.getroot() Base = declarative_base() s = Session() comment_stream = subreddit.stream.comments(skip_existing=True) for comment in comment_stream: if (program_flag == False): comment_stream.close() cut_off = datetime.now() - timedelta(hours=24) s.execute(delete(Stock).where(Stock.date < cut_off)) records = root.findall('record') if len(records) > 0: for record in root.findall('record'): tickers = list(record.iter('StockTicker')) fullnames = list(record.iter('StockFullname')) sectors = list(record.iter('StockSector')) if len(tickers) > 0: ticker = tickers[0].text else: ticker = '' if len(fullnames) > 0: fullname = fullnames[0].text else: fullname = '' if len(sectors) > 0: sector = sectors[0].text else: sector = '' if (ticker != '' and ticker in comment.body.split()) or ( fullname != '' and fullname in comment.body): new_stock = generateStock(ticker, fullname, sector) s.add(new_stock) s.commit() s.close() end_time = datetime.now() print('Started: ', start_time) print('Ended: ', end_time)
letters = string.ascii_lowercase ticker = ''.join( random.choice(letters) for i in range(random.randint(0, 4))) if len(ticker) == 1 or len(ticker) == 2: ticker = ''.join( random.choice(letters) for i in range(random.randint(3, 4))) name = r.get_random_word() stockList.append({ "ticker": ticker, "name": name, "sector": sectorList[random.randint(0, len(sectorList) - 1)] }) max_num_of_days_old = 7 for x in range(8000): r = random.randint(0, len(stockList) - 1) s.add( generateStock(stockList[r]["ticker"], stockList[r]["name"], stockList[r]["sector"], random.randint(0, max_num_of_days_old))) s.commit() s.close()
def update_song(session: Session, song: Song) -> bool: session.add(song) session.commit() return True
from crud import Session from models import Book s = Session() books = s.query(Book).all() for book in books: price = input(f"Price for '{book.title}': $") book.price = price s.add(book) s.commit() s.close()
def create_playlist(session: Session, user_id: int, playlist_name: str) -> bool: playlist = Playlist(user_id=user_id, name=playlist_name) session.add(playlist) session.commit() return True
def update_playlist(session: Session, playlist: Playlist) -> bool: session.add(playlist) session.commit() return True
#!/usr/bin/env python import arrow import yaml from sqlalchemy import and_, or_ from crud import Session, create_database, drop_database, recreate_database from model import Case, Member, CaseDetermination recreate_database() s = Session() member = Member(client_id="abc", external_member_id="123", person_code="01") s.add(member) s.commit() case0 = Case( case_number=1000000000, rn="rn0", drug_ndc="drug_ndc0", origin="fax", member_id=member.id, prescriber_npi="0123456789", requester_type="pharmacy", ) s.add(case0) case1 = Case( case_number=1000000001, rn="rn1",