Skip to content

Commit 573edd9

Browse files
committed
catch db error & simple decorator to check for db
1 parent a774d3b commit 573edd9

File tree

5 files changed

+50
-1
lines changed

5 files changed

+50
-1
lines changed

app.py

+27-1
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,18 @@
2525
import sys
2626
import os
2727
from models import setup_db, db, Artist, Venue, Show
28+
from check_db.check_db import requires_db
2829
#----------------------------------------------------------------------------#
2930
# App Config.
3031
#----------------------------------------------------------------------------#
3132

3233
app = Flask(__name__)
3334
app.config['SECRET_KEY'] = os.urandom(32)
35+
deployment_location = os.environ.get('DEPLOYMENT_LOCATION')
3436

3537
moment = Moment(app)
36-
setup_db(app)
38+
if deployment_location:
39+
setup_db(app)
3740

3841
#----------------------------------------------------------------------------#
3942
# Filters.
@@ -62,6 +65,7 @@ def index():
6265
# ----------------------------------------------------------------
6366

6467
@app.route('/venues')
68+
@requires_db()
6569
def venues():
6670
cities = db.session.query(Venue.city).group_by(Venue.city).all()
6771
current_time = datetime.now(timezone.utc)
@@ -90,6 +94,7 @@ def venues():
9094
return render_template('pages/venues.html', areas=data)
9195

9296
@app.route('/venues/search', methods=['POST'])
97+
@requires_db()
9398
def search_venues():
9499
term = request.form.get('search_term')
95100
search = "%{}%".format(term.lower())
@@ -98,6 +103,7 @@ def search_venues():
98103
return render_template('pages/search_venues.html', results=response, search_term=request.form.get('search_term', ''))
99104

100105
@app.route('/venues/<int:venue_id>')
106+
@requires_db()
101107
def show_venue(venue_id):
102108
venue = db.session.query(Venue).filter(Venue.id == venue_id).all()
103109
current_time = datetime.now(timezone.utc)
@@ -152,11 +158,13 @@ def show_venue(venue_id):
152158
# ----------------------------------------------------------------
153159

154160
@app.route('/venues/create', methods=['GET'])
161+
@requires_db()
155162
def create_venue_form():
156163
form = VenueForm()
157164
return render_template('forms/new_venue.html', form=form)
158165

159166
@app.route('/venues/create', methods=['POST'])
167+
@requires_db()
160168
def create_venue_submission():
161169
error = False
162170
try:
@@ -189,6 +197,7 @@ def create_venue_submission():
189197
return render_template('pages/home.html')
190198

191199
@app.route('/venues/<venue_id>/delete', methods=['DELETE'])
200+
@requires_db()
192201
def delete_venue(venue_id):
193202
error = False
194203
try:
@@ -208,6 +217,7 @@ def delete_venue(venue_id):
208217
# Artists
209218
# ----------------------------------------------------------------
210219
@app.route('/artists')
220+
@requires_db()
211221
def artists():
212222
data=[]
213223
artists = db.session.query(Artist).order_by('id').all()
@@ -219,6 +229,7 @@ def artists():
219229
return render_template('pages/artists.html', artists=data)
220230

221231
@app.route('/artists/search', methods=['POST'])
232+
@requires_db()
222233
def search_artists():
223234
term = request.form.get('search_term')
224235
search = "%{}%".format(term.lower())
@@ -227,6 +238,7 @@ def search_artists():
227238
return render_template('pages/search_artists.html', results=response, search_term=request.form.get('search_term', ''))
228239

229240
@app.route('/artists/<int:artist_id>')
241+
@requires_db()
230242
def show_artist(artist_id):
231243
artist = db.session.query(Artist).filter(Artist.id == artist_id).all()
232244
current_time = datetime.now(timezone.utc)
@@ -279,6 +291,7 @@ def show_artist(artist_id):
279291
# Update
280292
# ----------------------------------------------------------------
281293
@app.route('/artists/<int:artist_id>/edit', methods=['GET'])
294+
@requires_db()
282295
def edit_artist(artist_id):
283296
form = ArtistForm()
284297
data = Artist.query.get(artist_id)
@@ -298,6 +311,7 @@ def edit_artist(artist_id):
298311
return render_template('forms/edit_artist.html', form=form, artist=artist)
299312

300313
@app.route('/artists/<int:artist_id>/edit', methods=['POST'])
314+
@requires_db()
301315
def edit_artist_submission(artist_id):
302316
try:
303317
data = Artist.query.get(artist_id)
@@ -323,6 +337,7 @@ def edit_artist_submission(artist_id):
323337
return redirect(url_for('show_artist', artist_id=artist_id))
324338

325339
@app.route('/venues/<int:venue_id>/edit', methods=['GET'])
340+
@requires_db()
326341
def edit_venue(venue_id):
327342
form = VenueForm()
328343
data = Venue.query.get(venue_id)
@@ -343,6 +358,7 @@ def edit_venue(venue_id):
343358
return render_template('forms/edit_venue.html', form=form, venue=venue)
344359

345360
@app.route('/venues/<int:venue_id>/edit', methods=['POST'])
361+
@requires_db()
346362
def edit_venue_submission(venue_id):
347363
try:
348364
data = Venue.query.get(venue_id)
@@ -371,11 +387,13 @@ def edit_venue_submission(venue_id):
371387
# ----------------------------------------------------------------
372388

373389
@app.route('/artists/create', methods=['GET'])
390+
@requires_db()
374391
def create_artist_form():
375392
form = ArtistForm()
376393
return render_template('forms/new_artist.html', form=form)
377394

378395
@app.route('/artists/create', methods=['POST'])
396+
@requires_db()
379397
def create_artist_submission():
380398
error = False
381399
try:
@@ -406,6 +424,7 @@ def create_artist_submission():
406424
return render_template('pages/home.html')
407425

408426
@app.route('/artists/<artist_id>/delete', methods=['DELETE'])
427+
@requires_db()
409428
def delete_artist(artist_id):
410429
error = False
411430
try:
@@ -426,6 +445,7 @@ def delete_artist(artist_id):
426445
# ----------------------------------------------------------------
427446

428447
@app.route('/shows')
448+
@requires_db()
429449
def shows():
430450
data = []
431451
shows = db.session.query(Show).order_by(desc(Show.start_time)).all()
@@ -443,12 +463,14 @@ def shows():
443463
return render_template('pages/shows.html', shows=data)
444464

445465
@app.route('/shows/create')
466+
@requires_db()
446467
def create_shows():
447468
# renders form. do not touch.
448469
form = ShowForm()
449470
return render_template('forms/new_show.html', form=form)
450471

451472
@app.route('/shows/create', methods=['POST'])
473+
@requires_db()
452474
def create_show_submission():
453475
error=False
454476
try:
@@ -471,6 +493,10 @@ def create_show_submission():
471493
abort(500)
472494
return render_template('pages/home.html')
473495

496+
@app.errorhandler(400)
497+
def not_found_error(error):
498+
return render_template('errors/400.html'), 400
499+
474500
@app.errorhandler(404)
475501
def not_found_error(error):
476502
return render_template('errors/404.html'), 404

check_db/__init__.py

Whitespace-only changes.

check_db/check_db.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from functools import wraps
2+
from flask import abort
3+
4+
def requires_db():
5+
def requires_db_decorator(f):
6+
@wraps(f)
7+
def wrapper(*args, **kwargs):
8+
try:
9+
connection_string = app.config["SQLALCHEMY_DATABASE_URI"]
10+
except:
11+
abort(400)
12+
return f(*args, **kwargs)
13+
return wrapper
14+
return requires_db_decorator

models.py

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
dbname=conn_str_params['dbname']
3030
)
3131

32+
else:
33+
database_path = ""
34+
3235
db = SQLAlchemy()
3336

3437
'''

templates/errors/400.html

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{% extends 'layouts/main.html' %}
2+
{% block content %}
3+
<h1>Sorry ...</h1>
4+
<p>There's nothing here! <br> Please connect your Database.</p>
5+
<p><a href="{{url_for('index')}}">Back</a></p>
6+
{% endblock %}

0 commit comments

Comments
 (0)