-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
321 lines (252 loc) · 10.4 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#----------------------------------------------------------------------------#
# Imports
#----------------------------------------------------------------------------#
from curses.ascii import FF
from unicodedata import name
from flask import Flask
import json
import dateutil.parser
import babel
from flask import Flask, render_template, request, Response, flash, redirect, url_for
from flask_moment import Moment
import logging
from logging import Formatter, FileHandler
from flask_wtf import Form
from forms import *
from flask_migrate import Migrate
from models import Artist, Area,Show, Venue, db
#----------------------------------------------------------------------------#
# App Config.
#----------------------------------------------------------------------------#
app = Flask(__name__)
moment = Moment(app)
app.config.from_object('config')
db.init_app(app)
migrate = Migrate(app, db)
app.config['SQLALCHEMY_DATABASE_URI']
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
#----------------------------------------------------------------------------#
# Filters.
#----------------------------------------------------------------------------#
def format_datetime(value, format='medium'):
if isinstance(value, str):
date = dateutil.parser.parse(value)
else:
date = value
if format == 'full':
format="EEEE MMMM, d, y 'at' h:mma"
elif format == 'medium':
format="EE MM, dd, y h:mma"
return babel.dates.format_datetime(date, format, locale='en')
app.jinja_env.filters['datetime'] = format_datetime
#----------------------------------------------------------------------------#
# Controllers.
#----------------------------------------------------------------------------#
@app.route('/')
def index():
return render_template('pages/home.html')
# Venues
# ----------------------------------------------------------------
@app.route('/venues')
def venues():
return render_template('pages/venues.html', areas=Area.query.all())
@app.route('/venues/search', methods=['POST'])
def search_venues():
query = request.form.get("search_term")
venues = Venue.query.filter(Venue.name.ilike("%" + query + "%")).all()
return render_template('pages/search_venues.html', results=venues, count=len(venues), search_term=request.form.get('search_term', ''))
@app.route('/venues/<int:venue_id>')
def show_venue(venue_id):
# shows the venue page with the given venue_id
venue=Venue.query\
.join(Area, Venue.area_id == Area.id)\
.add_columns(Venue.id, Venue.name, Venue.image_link, Area.city, Area.state)\
.filter(Venue.id == venue_id).first()
return render_template('pages/show_venue.html', venue=venue)
# Create Venue
# ----------------------------------------------------------------
@app.route('/venues/create', methods=['GET'])
def create_venue_form():
form = VenueForm()
return render_template('forms/new_venue.html', form=form)
@app.route('/venues/create', methods=['POST'])
def create_venue_submission():
form = VenueForm()
if form.validate_on_submit():
name = request.form['name']
image_link = request.form['image_link']
facebook_link = request.form['facebook_link']
city = request.form['city']
state = request.form['state']
address= request.form['address']
talent= request.form['seeking_talent']
area = Area(city, state)
db.session.add(area)
db.session.commit()
area_id = area.id
record = Venue(name, image_link, facebook_link, address, talent, area_id)
try:
db.session.add(record)
db.session.commit()
# on successful db insert, flash success
flash('Venue ' + request.form['name'] + ' was successfully listed!')
except:
# flash('An error occurred. Venue ' + request.form['name'] + ' could not be listed.' + form.errors.items())
for field, message in form.errors.items():
flash(field + ' - ' + str(message), 'danger')
else:
for field, message in form.errors.items():
flash(field + ' - ' + str(message), 'danger')
return render_template('pages/home.html')
@app.route('/venues/<venue_id>', methods=['POST'])
def delete_venue(venue_id):
try:
Venue.query.filter(Venue.id == venue_id).delete()
db.session.commit()
flash('Deleted venue with id = ' + venue_id)
except:
flash('Unable to delete venue with id = ' + venue_id)
return render_template('pages/home.html')
# Artists
# ----------------------------------------------------------------
@app.route('/artists')
def artists():
return render_template('pages/artists.html', artists=Artist.query.all())
@app.route('/artists/search', methods=['POST'])
def search_artists():
query = request.form.get("search_term")
response = Artist.query.filter(Artist.name.ilike("%" + query + "%")).all()
return render_template('pages/search_artists.html', results=response, search_term=request.form.get('search_term', ''), count=len(response))
@app.route('/artists/<int:artist_id>')
def show_artist(artist_id):
artist=Artist.query.get(artist_id)
upcoming_shows = Show.query.join(Venue).\
add_columns(Venue.id.label("venue_id"), Venue.name.label("venue_name"), Venue.image_link.\
label("venue_image_link"), Show.start_time).\
filter(Show.artist_id==artist_id).filter(Show.start_time>datetime.now()).all()
past_shows = Show.query.join(Venue).\
add_columns(Venue.id.label("venue_id"), Venue.name.label("venue_name"), Venue.image_link.\
label("venue_image_link"), Show.start_time).\
filter(Show.artist_id==artist_id).filter(Show.start_time<datetime.now()).all()
# add new items to artist
upcoming_shows_count = len(upcoming_shows)
past_shows_count = len(past_shows)
artist.upcoming_shows = upcoming_shows
artist.past_shows = past_shows
artist.upcoming_shows_count = upcoming_shows_count
artist.past_shows_count = past_shows_count
return render_template('pages/show_artist.html', artist=artist)
# Update
# ----------------------------------------------------------------
@app.route('/artists/<int:artist_id>/edit', methods=['GET'])
def edit_artist(artist_id):
form = ArtistForm()
return render_template('forms/edit_artist.html', form=form, artist=Artist.query.get(artist_id))
@app.route('/artists/<int:artist_id>/edit', methods=['POST'])
def edit_artist_submission(artist_id):
artist=Artist.query.filter(Artist.id == artist_id).first()
# update values
artist.name = request.form['name']
artist.image_link = request.form['image_link']
artist.facebook_link = request.form['facebook_link']
artist.city = request.form['city']
db.session.commit()
return redirect(url_for('show_artist', artist_id=artist_id))
@app.route('/venues/<int:venue_id>/edit', methods=['GET'])
def edit_venue(venue_id):
form = VenueForm()
venue={
"id": 1,
"name": "The Musical Hop",
"genres": ["Jazz", "Reggae", "Swing", "Classical", "Folk"],
"address": "1015 Folsom Street",
"city": "San Francisco",
"state": "CA",
"phone": "123-123-1234",
"website": "https://www.themusicalhop.com",
"facebook_link": "https://www.facebook.com/TheMusicalHop",
"seeking_talent": True,
"seeking_description": "We are on the lookout for a local artist to play every two weeks. Please call us.",
"image_link": "https://images.unsplash.com/photo-1543900694-133f37abaaa5?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=400&q=60"
}
return render_template('forms/edit_venue.html', form=form, venue=venue)
@app.route('/venues/<int:venue_id>/edit', methods=['POST'])
def edit_venue_submission(venue_id):
# TODO: take values from the form submitted, and update existing
# venue record with ID <venue_id> using the new attributes
return redirect(url_for('show_venue', venue_id=venue_id))
# Create Artist
# ----------------------------------------------------------------
@app.route('/artists/create', methods=['GET'])
def create_artist_form():
form = ArtistForm()
return render_template('forms/new_artist.html', form=form)
@app.route('/artists/create', methods=['POST'])
def create_artist_submission():
form = ArtistForm()
name = request.form['name']
image_link = request.form['image_link']
facebook_link = request.form['facebook_link']
record = Artist(name, image_link, facebook_link)
try:
db.session.add(record)
db.session.commit()
flash('Artist ' + request.form['name'] + ' was successfully listed!')
except:
flash('An error occurred. Artist ' + request.form['name'] + ' could not be listed.')
return render_template('pages/home.html')
# Shows
# ----------------------------------------------------------------
@app.route('/shows')
def shows():
# displays list of shows at /shows
shows=Show.query.join(Artist, Artist.id == Show.id).join(Venue, Venue.id == Show.id).add_columns(Show.id, Show.venue_id, Show.artist_id, Show.start_time, Venue.name.label("venue_name"), Artist.name.label("artist_name"), Artist.image_link.label("artist_image_link")).all()
return render_template('pages/shows.html', shows=shows)
@app.route('/shows/create')
def create_shows():
# renders form. do not touch.
form = ShowForm()
return render_template('forms/new_show.html', form=form)
@app.route('/shows/create', methods=['POST'])
def create_show_submission():
form = ShowForm()
artist = request.form['artist_id']
venue = request.form['venue_id']
show_date = request.form['start_time']
record = Show(artist, venue, show_date)
try:
db.session.add(record)
db.session.commit()
# on successful db insert, flash success
flash('Show was successfully listed!')
except:
flash('An error occurred. Show could not be listed.')
# see: http://flask.pocoo.org/docs/1.0/patterns/flashing/
return render_template('pages/home.html')
@app.errorhandler(404)
def not_found_error(error):
return render_template('errors/404.html'), 404
@app.errorhandler(500)
def server_error(error):
return render_template('errors/500.html'), 500
if not app.debug:
file_handler = FileHandler('error.log')
file_handler.setFormatter(
Formatter('%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]')
)
app.logger.setLevel(logging.INFO)
file_handler.setLevel(logging.INFO)
app.logger.addHandler(file_handler)
app.logger.info('errors')
#----------------------------------------------------------------------------#
# Launch.
#----------------------------------------------------------------------------#
# Default port:
if __name__ == '__main__':
app.run()
# Or specify port manually:
'''
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)
'''