def seed_db(): from models.User import User # Importing the User model from models.Profile import Profile # Importing the Profile model from main import bcrypt # Hashing module for the passwords from faker import Faker # Importing the faker module for fake data import random # Importing random from the python standard library faker = Faker() users = [] for i in range(5): # Do this 5 times user = User() # Create an user object from the User model user.email = f"test{i+1}@test.com" # Assign an email to the user object user.password = bcrypt.generate_password_hash("123456").decode("utf-8") # Assign ta hashed password to the user object db.session.add(user) # Add the user to the db session users.append(user) # Append the user to the users list db.session.commit() # Commit the seeion to the db for i in range(5): profile = Profile() # Create a profile object from the Profile model profile.username = faker.first_name() # Add a username to the profile object profile.firstname = faker.first_name() # Add a firstname to the profile object profile.lastname = faker.last_name() # Add a lastname to the profile object profile.user_id = users[i].id # Add a user_id to the profile object. This comes from real ids from the users list db.session.add(profile) # Add the profile to the session db.session.commit() # Commit the session to the database print("Tables seeded")
def profile_create(user): # This function will run when the route is matched if user.profile != []: # If the user already has a profile return abort(400, description="User already has profile" ) # Return the error "Email already in use" profile_fields = profile_schema.load( request.json) # Retrieving the fields from the request profile = Profile.query.filter_by( username=profile_fields["username"]).first( ) # Query the user table with the email and return the first user if profile: # If a user is returned return abort(400, description="username already in use" ) # Return the error "Email already in use" new_profile = Profile( ) # Create a new profile object from the Profile model new_profile.username = profile_fields[ "username"] # Add username to the new_profile new_profile.firstname = profile_fields[ "firstname"] # Add username to the new_profile new_profile.lastname = profile_fields[ "lastname"] # Add username to the new_profile new_profile.user_id = user.id # Add username to the new_profile user.profile.append(new_profile) # Add profile to the user db.session.commit() # Commit the DB session return jsonify( profile_schema.dump(new_profile)) # Return the newly created profile
def seed_db(): from models.User import User # Importing the User model from models.Profile import Profile # Importing the Profile model from models.League import League from main import bcrypt # Hashing module for the passwords from faker import Faker # Importing the faker module for fake data import random # Importing random from the python standard library faker = Faker() users = [] leagues = [] for i in range(5): # Do this 5 times user = User() # Create an user object from the User model user.email = f"test{i+1}@test.com" # Assign an email to the user object user.password = bcrypt.generate_password_hash("123456").decode("utf-8") # Assign ta hashed password to the user object db.session.add(user) # Add the user to the db session users.append(user) # Append the user to the users list db.session.commit() # Commit the seeion to the db for i in range(5): profile = Profile() # Create a profile object from the Profile model profile.username = faker.first_name() # Add a username to the profile object profile.firstname = faker.first_name() # Add a firstname to the profile object profile.lastname = faker.last_name() # Add a lastname to the profile object profile.user_id = users[i].id # Add a user_id to the profile object. This comes from real ids from the users list db.session.add(profile) # Add the profile to the session db.session.commit() # Commit the session to the database for i in range(3): new_league = League() new_league.title = f"League title {i}" new_league.description = f"A nice league to the power of {i}" for i in range(3): new_league.users_leagues.append(users[i]) leagues.append(new_league) db.session.commit() for member in leagues[0].users_leagues: # print(f"League {i.title} => {i.account_id}") print(member.email) print("Tables seeded") # Print a message to let the user know they
def profile_create(user): if user.profile != []: return abort(400, description="User already has profile") profile_fields = profile_schema.load(request.json) profile = Profile.query.filter_by( username=profile_fields["username"]).first() if profile: return abort(400, description="username already in use") new_profile = Profile() new_profile.username = profile_fields["username"] new_profile.firstname = profile_fields["firstname"] new_profile.lastname = profile_fields["lastname"] new_profile.user_id = user.id user.profile.append(new_profile) db.session.commit() return jsonify(profile_schema.dump(new_profile))
def seed_db(): from models.User import User # Importing the User model from models.Profile import Profile # Importing the Profile model from models.Playlist import Playlist from models.Track import Track from models.Album import Album from models.Artist import Artist from models.AlbumType import AlbumType from models.Collection import Collection from main import bcrypt # Hashing module for the passwords from faker import Faker # Importing the faker module for fake data import random # Importing random from the python standard library faker = Faker() users = [] albums = [] album_types = ["SINGLE", "ALBUM", "COMPILATION"] for i in range(5): # Do this 5 times user = User() # Create an user object from the User model if i == 0: user.is_admin = True else: user.is_admin = False user.email = f"test{i+1}@test.com" # Assign an email to the user object user.password = bcrypt.generate_password_hash("123456").decode( "utf-8") # Assign ta hashed password to the user object db.session.add(user) # Add the user to the db session users.append(user) # Append the user to the users list db.session.commit() # Commit the seeion to the db for i in range(5): profile = Profile() # Create a profile object from the Profile model profile.username = faker.first_name( ) # Add a username to the profile object profile.firstname = faker.first_name( ) # Add a firstname to the profile object profile.lastname = faker.last_name( ) # Add a lastname to the profile object profile.user_id = users[ i].id # Add a user_id to the profile object. This comes from real ids from the users list db.session.add(profile) # Add the profile to the session db.session.commit() # Commit the session to the database for i in range(10): album = Album() album.name = faker.catch_phrase() db.session.add(album) albums.append(album) db.session.commit() for at in album_types: album_type = AlbumType() album_type.name = at album_type.album_id = random.choice(albums).album_id db.session.add(album_type) db.session.commit() for i in range(5): artist = Artist() artist.name = faker.name_nonbinary() db.session.add(artist) db.session.commit() for i in range(20): track = Track() track.name = faker.file_name() track.track_num = random.choice(range(20)) track.album_id = random.choice(albums).album_id track.disc_num = 1 track.duration_ms = 2500 track.explicit = True playlist = Playlist() # Create a playlist object from Playlist model playlist.name = faker.catch_phrase() playlist.owner_id = random.choice(users).id playlist.collaborative = False playlist.public = True collection = Collection( ) # Create a playlist object from Playlist model collection.name = faker.catch_phrase() collection.owner_id = random.choice(users).id collection.collaborative = False collection.public = True db.session.add(track) db.session.add(playlist) db.session.add(collection) db.session.commit() print("Tables seeded") # Print a message to let the user know they
def seed_db(): from datetime import date from models.User import User # Importing the User model from models.Profile import Profile # Importing the Profile model from models.League import League from models.Member import Member from models.Fine import Fine from models.Point import Point from models.Category import Category from models.Sprint import Sprint from main import bcrypt # Hashing module for the passwords from faker import Faker # Importing the faker module for fake data import random # Importing random from the python standard library import copy import time faker = Faker() users = [] leagues = [] categories = [] sprints = [] points = [] fines = [] for i in range(5): time.sleep(0.2) user = User() user.email = f"test{i+1}@test.com" user.password = bcrypt.generate_password_hash("123456").decode("utf-8") db.session.add(user) users.append(user) db.session.commit() for i in range(5): # time.sleep(0.2) profile = Profile() profile.username = f"username{i}" profile.firstname = f"firstname{i}" profile.lastname = f"lastname{i}" profile.user_id = users[i].id db.session.add(profile) db.session.commit() for i in range(5): # time.sleep(0.2) new_league = League() new_league.title = f"League title {i}" new_league.description = f"A nice league to the power of {i}" new_league.owner = users[i].id leagues.append(new_league) db.session.add(new_league) db.session.commit() for i in range(5): # time.sleep(0.2) owner = Member() owner.user_id = leagues[i].owner owner.league_id = i + 1 owner.active = True db.session.add(owner) new_member = Member() new_member.active = True new_member.league_id = i + 1 new_member.user_id = random.choice(users).id while new_member.user_id == owner.user_id: new_member.user_id = random.choice(users).id db.session.add(new_member) db.session.commit() for i in range(5): new_sprint = Sprint() new_sprint.title = f"Sprint title #{i}" new_sprint.meeting_point = f"The Outback" new_sprint.creation_time = date.today() league = leagues[i] new_sprint.league = league sprints.append(new_sprint) db.session.commit() for i in range(5): # time.sleep(0.2) new_category = Category() new_category.title = f"category title {i}" new_category.description = f"category description {i}" if i % 2 == 0: private = True else: private = False new_category.private = private new_category.owner = random.choice(users).id new_category.leagues_categories.append(leagues[i]) categories.append(new_category) db.session.commit() for i in range(5): # time.sleep(0.2) new_fine = Fine() new_fine.title = f"Title {i}" new_fine.description = f"Description {i}" new_fine.amount = i if i % 2 == 0: style = "Award" else: style = "Fine" new_fine.style = style category = categories[i] new_fine.category = category fines.append(new_fine) db.session.commit() for i in range(4): # time.sleep(0.2) new_point = Point() new_point.creation_time = date.today() new_point.fine_id = random.choice(fines).id sprint = sprints[i] new_point.sprint = sprint new_point.giver_id = sprint.league.owner new_point.receiver_id = sprint.league.members[1].id db.session.commit() print("Tables seeded")