-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
154 lines (105 loc) · 4.48 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
from flask import Flask, redirect, render_template
from flask_debugtoolbar import DebugToolbarExtension
from models import db, connect_db, Playlist, Song, PlaylistSong
from forms import NewSongForPlaylistForm, SongForm, PlaylistForm
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql:///playlist-app'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_ECHO'] = True
connect_db(app)
db.create_all()
app.config['SECRET_KEY'] = "I'LL NEVER TELL!!"
# Having the Debug Toolbar show redirects explicitly is often useful;
# however, if you want to turn it off, you can uncomment this line:
#
# app.config['DEBUG_TB_INTERCEPT_REDIRECTS'] = False
debug = DebugToolbarExtension(app)
@app.route("/")
def root():
"""Homepage: redirect to /playlists."""
return redirect("/playlists")
##############################################################################
# Playlist routes
@app.route("/playlists")
def show_all_playlists():
"""Return a list of playlists."""
playlists = Playlist.query.all()
return render_template("playlists.html", playlists=playlists)
@app.route("/playlists/<int:playlist_id>")
def show_playlist(playlist_id):
"""Show detail on specific playlist.""" # ADD THE NECESSARY CODE HERE FOR THIS ROUTE TO WORK
playlist=Playlist.query.get_or_404(playlist_id)
return render_template('playlist.html', playlist=playlist)
@app.route("/playlists/add", methods=["GET", "POST"])
def add_playlist():
"""Handle add-playlist form:
- if form not filled out or invalid: show form
- if valid: add playlist to SQLA and redirect to list-of-playlists
"""
# ADD THE NECESSARY CODE HERE FOR THIS ROUTE TO WORK
form = PlaylistForm()
if form.validate_on_submit():
name = form.playlist_name.data
description = form.playlist_description.data
new_playlist = Playlist(playlist_name=name, playlist_description=description)
db.session.add(new_playlist)
db.session.commit()
flash('new playlist created!', 'success')
return redirect('/playlist')
return render_template('new_playlist.html', form=form)
##############################################################################
# Song routes
@app.route("/songs")
def show_all_songs():
"""Show list of songs."""
songs = Song.query.all()
return render_template("songs.html", songs=songs)
@app.route("/songs/<int:song_id>")
def show_song(song_id):
"""return a specific song"""
# ADD THE NECESSARY CODE HERE FOR THIS ROUTE TO WORK
song = Song.query.get_or_404(song_id)
return render_template('song.html', song=song)
@app.route("/songs/add", methods=["GET", "POST"])
def add_song():
"""Handle add-song form:
- if form not filled out or invalid: show form
- if valid: add playlist to SQLA and redirect to list-of-songs
"""
# ADD THE NECESSARY CODE HERE FOR THIS ROUTE TO WORK
form = SongForm()
if form.validate_on_submit():
song_title = form.song_title.data
song_artist = form.song_artist.data
new_song=Song(song_title=song_title, song_artist=song_artist)
db.session.add(song)
db.session.commit()
flash('New song added!')
return redirect('/songs')
return render_template('new_song.html', form=form)
@app.route("/playlists/<int:playlist_id>/add-song", methods=["GET", "POST"])
def add_song_to_playlist(playlist_id):
"""Add a playlist and redirect to list."""
# BONUS - ADD THE NECESSARY CODE HERE FOR THIS ROUTE TO WORK
playlist = Playlist.query.get_or_404(playlist_id)
form = NewSongForPlaylistForm()
# Restrict form to songs not already on this playlist
curr_on_playlist = [s.id for s in playlist.songs]
form.song.choices = (db.session.query(Song.id, Song.title)
.filter(Song.id.notin_(curr_on_playlist))
.all())
if form.validate_on_submit():
# This is one way you could do this ...
playlist_song = PlaylistSong(song_id=form.song.data,
playlist_id=playlist_id)
db.session.add(playlist_song)
# Here's another way you could that is slightly more ORM-ish:
#
# song = Song.query.get(form.song.data)
# playlist.songs.append(song)
# Either way, you have to commit:
db.session.commit()
return redirect(f"/playlists/{playlist_id}")
return render_template("add_song_to_playlist.html",
playlist=playlist,
form=form)