def create_artist_submission(): error = False data = request.form try: artist = Artist() artist.name = data['name'] artist.city = data['city'] artist.state = data['state'] artist.phone = data.get('phone', '') artist.facebook_link = data.get('facebook_link', '') artist.genres = [ ArtistGenres(genre=GenreEnum[genre]) for genre in data.getlist('genres') ] db.session.add(artist) db.session.commit() except: error = True db.session.rollback() finally: data = artist.to_dict() db.session.close() if not error: flash(f'Artist {data["name"]} was successfully listed!', 'alert-success') else: flash( f'An error occurred. Artist {data["name"]} could not be listed. \ Does the artist exist already?', 'alert-danger') return render_template('pages/home.html')
def populate_database(session): artist = Artist() artist.name = "Test Artist" artist.spotify_uri = "spotify:artist:arn" session.add(artist) album = Album() album.name = "Test Album" album.artists.append(artist) album.spotify_uri = "spotify:album:arn" session.add(album) media = Media() media.tmdb_id = "TMDB_ID" media.name = "Test media" media.type = 1 session.add(media) session.commit()
def updateDBInfo(response, track): tags = Information() # Changing tags in the database if 'TITLE' in response and response['TITLE'] != '': tags.trackTitle = strip_tags(response['TITLE']).lstrip().rstrip() track.title = tags.trackTitle if 'ARTISTS' in response and response['ARTISTS'] != '': tags.trackArtist = strip_tags( response['ARTISTS']).lstrip().rstrip().split(',') artists = [] for artist in tags.trackArtist: if Artist.objects.filter(name=artist).count() == 0: newArtist = Artist() newArtist.name = artist newArtist.save() artists.append(Artist.objects.get(name=artist)) track.artist.clear() for artist in artists: track.artist.add(artist) if 'PERFORMER' in response and response['PERFORMER'] != '': tags.trackPerformer = strip_tags( response['PERFORMER']).lstrip().rstrip() track.performer = tags.trackPerformer if 'COMPOSER' in response and response['COMPOSER'] != '': tags.trackComposer = strip_tags(response['COMPOSER']).lstrip().rstrip() track.composer = tags.trackComposer if 'YEAR' in response and response['YEAR'] != '': tags.trackYear = checkIntValueError(response['YEAR']) track.year = tags.trackYear if 'TRACK_NUMBER' in response and response['TRACK_NUMBER'] != '': tags.trackNumber = checkIntValueError(response['TRACK_NUMBER']) track.number = tags.trackNumber if 'BPM' in response and response['BPM'] != '': track.bpm = checkIntValueError(response['BPM']) if 'LYRICS' in response and response['LYRICS'] != '': tags.lyrics = strip_tags(response['LYRICS']).lstrip().rstrip() track.lyrics = tags.lyrics if 'COMMENT' in response and response['COMMENT'] != '': tags.comment = strip_tags(response['COMMENT']).lstrip().rstrip() track.comment = tags.comment if 'GENRE' in response and response['GENRE'] != '': tags.trackGenre = strip_tags(response['GENRE']).lstrip().rstrip() if Genre.objects.filter(name=tags.trackGenre).count() == 0: genre = Genre() genre.name = tags.trackGenre genre.save() genre = Genre.objects.get(name=tags.trackGenre) track.genre = genre if 'COVER' in response: md5Name = hashlib.md5() if str(response['COVER'].split(",")[0]) == "image/png": extension = "png" else: extension = "jpg" md5Name.update(base64.b64decode(str(response['COVER'].split(",")[1]))) # Check if the folder exists filePath = "/ManaZeak/static/img/covers/" if not os.path.isdir(filePath): os.mkdir(filePath) # Create the folder filePath += +md5Name.hexdigest() + extension # if the filePath is the same, then the md5 hash of the image is # the same, therefore the images are the same, therefore do nothing if not os.path.isfile(filePath): with open(filePath, 'wb+') as destination: # Split the header with MIME type tags.cover = base64.b64decode( str(response['COVER'].split(",")[1])) destination.write(tags.cover) track.coverLocation = md5Name.hexdigest() + extension if 'ALBUM_TITLE' in response and 'ALBUM_ARTISTS' in response and response['ALBUM_TITLE'] != '' \ and response['ALBUM_ARTISTS'] != '': tags.albumTitle = strip_tags(response['ALBUM_TITLE']).lstrip().rstrip() tags.albumArtist = strip_tags( response['ALBUM_ARTISTS']).lstrip().rstrip().split(',') if Album.objects.filter(title=tags.albumTitle).count() == 0: album = Album() album.title = tags.albumTitle album.save() album = Album.objects.get(title=tags.albumTitle) album.artist.clear() for artist in tags.albumArtist: if Artist.objects.filter(name=artist).count() == 0: newArtist = Artist() newArtist.name = artist newArtist.save() album.artist.add(Artist.objects.get(name=artist)) if 'ALBUM_TOTAL_DISC' in response and response[ 'ALBUM_TOTAL_DISC'] != '': tags.albumTotalDisc = checkIntValueError( response['ALBUM_TOTAL_DISC']) album.totalDisc = tags.albumTotalDisc if 'DISC_NUMBER' in response and response['DISC_NUMBER'] != '': tags.albumDiscNumber = checkIntValueError(response['DISC_NUMBER']) track.discNumber = tags.albumDiscNumber if 'ALBUM_TOTAL_TRACK' in response and response[ 'ALBUM_TOTAL_TRACK'] != '': tags.albumTotalTrack = checkIntValueError( response['ALBUM_TOTAL_TRACK']) album.totalTrack = tags.albumTotalTrack album.save() track.album = album track.save() return tags