def playlist(playlist_slug): """Add song to playlist.""" try: playlist = Playlist.get(playlist_slug) except DoesNotExist as e: sys.exit(e) songs = [song.slug for song in Song.all() if song not in playlist] try: song_slug = subprocess.run( ['fzf', '--no-sort', '--exact'], input='\n'.join(songs), universal_newlines=True, stdout=subprocess.PIPE, ).stdout.strip() except FileNotFoundError as e: sys.exit(e) if not song_slug: return song = Song.get(song_slug) # If the user selects nothing we leave the root empty, and the # default root is used. If he wants to explicitly set the default # root in case it's changed in the future, he has to select it. default = song.scale[0:2].strip() root = click.prompt(f'Root [{default}]', default='', show_default=False) if root and root not in SHARPS + FLATS: sys.exit(f'Invalid root: {root}') playlist.add(song_slug, root)
def index(): """A list of all songs in the database.""" return render_template( 'index.html', title='Τραγούδια', songs=Song.all(), admin=session.get('logged_in'), )
def test_save_delete(client): with client.session_transaction() as session: session['logged_in'] = True song = SongFactory(body='Bm F# Bm') song.tofile() assert len(Song.all()) == 1 url = url_for('admin.save', slug='name', semitones=1) resp = client.get(url, follow_redirects=True) assert resp.status_code == 200 song = Song.get('name') assert song.body == 'Cm G Cm' resp = client.get(url_for('admin.delete', slug='name'), follow_redirects=True) assert Song.all() == [] resp = client.get(url_for('admin.delete', slug='name'), follow_redirects=True) assert 'Δεν υπάρχει τέτοια σελίδα'.encode() in resp.data
def index(): """A list of all songs in the database.""" songs = Song.all() return render_template( 'index.html', title='Admin', songs=songs, admin=True, )
def complement(slug): """A list of all songs in a given playlist.""" playlist = Playlist.get_or_404(slug) all_songs = Song.all() songs = [song for song in all_songs if song not in playlist.songs] return render_template( 'index.html', title=playlist.name, songs=songs, admin=session.get('logged_in'), )
def index(): """Index all data into elasticsearch.""" elastic.create_index() def index_all(items, name): with click.progressbar(items, label=f"Indexing {name}") as bar: for item in bar: elastic.index(item) index_all(Song.all(frommetadata=False), 'songs') index_all(Artist.all(), 'artists') index_all(Scale.all(), 'scales')
def random(): """Redirect to a random song. The song is chosen randomly from the selected playlist, or the whole database, if no playlist is selected. The last accessed songs, which are located in the 'latest_songs' cookie, via the `add_slug_to_cookie` decorator, are excluded from the selection. """ playlist_slug = request.cookies.get('playlist') songs = Playlist.get(playlist_slug).songs if playlist_slug else Song.all() cookie = request.cookies.get('latest_songs') latest_songs = json.loads(cookie) if cookie else [] song = choice([song for song in songs if song.slug not in latest_songs]) return redirect(url_for('main.song', slug=song.slug) + '?random=true')
def scale(slug, root='D'): """A list of all available scales.""" if not re.match('^[A-G][bs]?$', root): abort(404) root = re.sub('s', '#', root) scale = Scale.get_or_404(slug) scale.root = root songs = [song for song in Song.all() if scale.name in song.scale] scales = [s for s in Scale.all() if s.slug != scale.slug] return render_template( 'scale.html', scale=scale, songs=songs, scales=scales, admin=session.get('logged_in'), )
def wrapper(*args, **kwargs): playlist = get_selected_playlist() num_songs = playlist.num if playlist else len(Song.all()) limit = int(0.9 * num_songs) response = make_response(f(*args, **kwargs)) cookie = request.cookies.get('latest_songs') latest_songs = json.loads(cookie) if cookie else [] slug = kwargs['slug'] latest_songs.sort(key=slug.__eq__) # Move to end if not latest_songs or latest_songs[-1] != slug: latest_songs.append(slug) len_cookie = len(latest_songs) if len_cookie > limit: latest_songs = latest_songs[len_cookie - limit:] response.set_cookie('latest_songs', json.dumps(latest_songs)) return response