def artist_create(): artist_fields = artist_schema.load(request.json) new_artist = Artist() new_artist.name = artist_fields["name"] db.session.add(new_artist) db.session.commit() return jsonify(artist_schema.dump(new_artist))
def seed_tables(): # User for i in range(10): user = User() user.id = i user.displayname = f"testuser{i}" user.email = f"TestEmail{i}@test.com" user.username = f"TestUserName{i}" user.password = bcrypt.generate_password_hash("testpassword").decode("utf-8") db.session.add(user) db.session.commit() print("Seeded Table: user") # Artist artists = [] for i in range(10): artist = Artist() artist.name = f"TestArtistName{i}" db.session.add(artist) artists.append(artist) db.session.commit() print("Seeded Table: artist") # Image for i in range(20): image = Image() image.url = f"TestURL{i}" image.height = choice(range(600, 1200)) image.width = choice(range(600, 1200)) db.session.add(image) db.session.commit() print("Seeded Table: images") def moods_table_creation(): moods = Moods() moods.amusement = choice(range(1, 101)) moods.joy = choice(range(1, 101)) moods.beauty = choice(range(1, 101)) moods.relaxation = choice(range(1, 101)) moods.sadness = choice(range(1, 101)) moods.dreaminess = choice(range(1, 101)) moods.triumph = choice(range(1, 101)) moods.anxiety = choice(range(1, 101)) moods.scariness = choice(range(1, 101)) moods.annoyance = choice(range(1, 101)) moods.defiance = choice(range(1, 101)) moods.feelingpumped = choice(range(1, 101)) db.session.add(moods) db.session.commit() return moods # Tracks for i in range(30): track = Tracks() track.trackname = f"TestTrackName{i}" track.artist_id = choice(artists).id track.moods_id = moods_table_creation().id track.trackurl = f"TestTrackURL{i}" db.session.add(track) db.session.commit() print("Seeded Table: tracks")
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