def songs(): if request.method == "POST": title = str(request.data.get('title', '')) artist = str(request.data.get('artist', '')) if title and artist: song = Song(title=title, artist=artist) song.save() response = jsonify({ 'id': song.id, 'title': song.title, 'date_created': song.date_created, 'date_modified': song.date_modified }) response.status_code = 201 return response else: # GET songs = Song.get_all() results = [] for song in songs: obj = { 'id': song.id, 'title': song.title, 'artist': song.artist, 'date_created': song.date_created, 'date_modified': song.date_modified } results.append(obj) response = jsonify(results) response.status_code = 200 return response
def add_song(self, **kwargs): artist_name = kwargs.get('title')[0] artist = Artist.objects(name=artist_name) song_name = kwargs.get('title')[-1] if not artist or Song.objects(name=song_name): self.add_artist(**kwargs) if self.is_song_exist(song_name): Log(type_added='song', name_added=song_name).save() return False self.download(kwargs.get('download_url')) size = kwargs.get('duration_size') size = ' '.join((size[2], size[3])) song = Song(artist=artist.get(), name=song_name, duration=kwargs.get('duration_size')[0], download_url=kwargs.get('download_url'), size=size) with open(TEMP_FILE, 'rb') as binary_file: song.audio_file.put(binary_file) shutil.rmtree(TEMP) if song: song.save() Log(type_added='song', name_added=song_name, added=True).save() return True Log(type_added='song', name_added=song_name).save() return False
def new_song(request, id): if request.method == "GET": return render(request, "album_detail.html") elif request.method == "POST": album = Song(title=request.POST["title"], seconds=int(request.POST["seconds"]), album_id=id) album.save() return redirect(f"album/{id}", {"album": album})