def music_example(): # Create artists kanye = Artist(name='Kanye West') mozart = Artist(name='Wolfgang Amadeus Mozart') # Add artists to the DB session and flush to generate their primary key IDs session.add(kanye) session.add(mozart) session.flush() # Create songs stronger = Song( filename='stronger.mp3', title='Stronger', date_added=datetime.utcnow(), artist=kanye, # Assign the artist object directly ) love_lockdown = Song( filename='love_lockdown.mp3', title='Love Lockdown', date_added=datetime.utcnow(), artist=kanye, ) requiem = Song( filename='requiem.mp3', title='Requiem', date_added=datetime.utcnow(), artist=mozart, ) # Add songs to the DB session and flush to generate their primary key IDs # as well as populating the artist_id field session.add(stronger) session.add(love_lockdown) session.add(requiem) session.flush() # Create a playlist playlist = Playlist(name='test') session.add(playlist) session.flush() # Add some songs to the playlist playlist.append_song(stronger) playlist.append_song(love_lockdown) playlist.append_song(requiem) # Delete a song from the playlist playlist.delete_song(1) # Insert a song at number 3 in the playlist playlist.insert_song(2, love_lockdown) session.commit() print "Artists: %s, %s" % (kanye, mozart) print "Each artist's songs: Kanye: %s, Mozart: %s" % (kanye.songs, mozart.songs) print "Playlist songs: %s" % playlist.songs
def getPlan(plan_id): plan = ActivityPlan.query.filter(ActivityPlan.id == plan_id).first() if not plan: print '\nNo Activity Plan exists, creating new plan\n' plan = ActivityPlan(name='Plan') session.add(plan) session.commit() return plan
def getPlayList(playlist_name): # Check if Playlist exists, create one if it doesn't playlist = Playlist.query.filter(Playlist.name == playlist_name).first() if not playlist: print '\nNo Activity Playlist exists, creating new Playlist\n' playlist = Playlist(name=playlist_name) session.add(playlist) session.commit() return playlist
def generatePlayList(playlist_name, plan_id): songs = Song.query.all() if not songs: # make sure there are songs in the database print '\nNo songs available\n' #TODO: possibly add the songs? return playlist = getPlayList(playlist_name) plan = getPlan(plan_id) playlist.generate(plan) session.commit()
def csv_import(): """ Import the song metadata from song_dict.csv into the database. """ print "Importing CSV song data into the database..." csvfile = open('song_dict.csv', 'r') csvreader = csv.reader(csvfile, delimiter=',', quotechar = '\"') artist_dict = {} for title, artist_name, bpm, duration, filename in csvreader: # Create or fetch artist artist_name = artist_name.decode('utf-8') artist = artist_dict.get(artist_name) if not artist: # Create new artist artist = Artist(name=artist_name) # Add artists to the DB session and flush to generate their # primary key IDs session.add(artist) session.flush() # Remember the artist via the artists dict artist_dict[artist_name] = artist # Create songs song = Song( filename = os.path.abspath(filename.decode('utf-8')), title = title.decode('utf-8'), date_added = datetime.utcnow(), artist = artist, # Assign the artist object directly ) # Add songs to the DB session and flush to generate their # primary key IDs as well as populating the artist_id field print "Adding song %s..." % song session.add(song) session.flush() # Create Metadata meta = SongMeta( duration = int(float(duration)), bpm = int(bpm), song = song, ) session.add(meta) session.flush() session.commit() print "CSV import finished successfully."
def csv_import(): """ Import the song metadata from song_dict.csv into the database. """ print "Importing CSV song data into the database..." csvfile = open('song_dict.csv', 'r') csvreader = csv.reader(csvfile, delimiter=',', quotechar='\"') artist_dict = {} for title, artist_name, bpm, duration, filename in csvreader: # Create or fetch artist artist_name = artist_name.decode('utf-8') artist = artist_dict.get(artist_name) if not artist: # Create new artist artist = Artist(name=artist_name) # Add artists to the DB session and flush to generate their # primary key IDs session.add(artist) session.flush() # Remember the artist via the artists dict artist_dict[artist_name] = artist # Create songs song = Song( filename=os.path.abspath(filename.decode('utf-8')), title=title.decode('utf-8'), date_added=datetime.utcnow(), artist=artist, # Assign the artist object directly ) # Add songs to the DB session and flush to generate their # primary key IDs as well as populating the artist_id field print "Adding song %s..." % song session.add(song) session.flush() # Create Metadata meta = SongMeta( duration=int(float(duration)), bpm=int(bpm), song=song, ) session.add(meta) session.flush() session.commit() print "CSV import finished successfully."
def setup_database(): ''' Create the database and its tables. ''' print 'Creating tables...' try: Base.metadata.create_all(engine) # Create Pace objects for pace in ['Slow', 'Steady', 'Fast', 'Sprint']: session.add(Pace(speed=pace)) session.commit() except: raise else: print 'Tables created successfully.'
def activity_example(): # Create pace objects and add to session slow, steady, fast, sprint = Pace(speed='Slow'), Pace(speed='Steady'), \ Pace(speed='Fast'), Pace(speed='Sprint') session.add(slow) session.add(steady) session.add(fast) session.add(sprint) # Create an activity plan plan = ActivityPlan(name='test') session.add(plan) session.flush() # Add 4 segments to the plan plan.append_segment(pace=steady, length=60) plan.append_segment(pace=steady, length=60) plan.append_segment(pace=slow, length=10) plan.append_segment(pace=sprint, length=60) # Whoops - I made a mistake and want to delete a segment! plan.delete_segment(1) # Fix the second segment plan.update_segment(position=1, pace=fast, length=60) # Add a segment to the beginning plan.insert_segment(position=0, pace=slow, length=60) session.flush() session.commit() # Clean up orphaned segments Segment.remove_orphans() session.commit() print "Plan segments: %s" % plan.segments
def updateSegPace(plan_id, seg_pos, pace): plan = getPlan(plan_id) pace = paces[pace] plan.update_segment(position=seg_pos, pace=pace) session.commit()
def updateSegTime(plan_id, seg_pos, time): plan = getPlan(plan_id) plan.update_segment(position=seg_pos, length=time) session.commit()
def removeSegment(plan_id, seg_pos): plan = getPlan(plan_id) plan.delete_segment(seg_pos) session.commit()
def insertSegment(plan_id, seg_pos, pace, time): plan = getPlan(plan_id) pace = paces[pace] plan.insert_segment(position=seg_pos, pace=pace, length=time) session.commit()
def addSegment(plan_id, pace, time): plan = getPlan(plan_id) pace = paces[pace] plan.append_segment(pace=pace, length=time) session.commit()