forked from yfeng46/SongAnalytics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
51 lines (38 loc) · 1.61 KB
/
app.py
File metadata and controls
51 lines (38 loc) · 1.61 KB
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
from flask import render_template, request, redirect, url_for
import logging.config
from app import db, app
from app.models import Track
# Define LOGGING_CONFIG in config.py - path to config file for setting up the logger (e.g. config/logging/local.conf)
logging.config.fileConfig(app.config["LOGGING_CONFIG"])
logger = logging.getLogger("penny-lane")
logger.debug('Test log')
@app.route('/')
def index():
"""Main view that lists songs in the database.
Create view into index page that uses data queried from Track database and
inserts it into the msiapp/templates/index.html template.
Returns: rendered html template
"""
try:
tracks = Track.query.all()
logger.debug("Index page accessed")
return render_template('index.html', tracks=tracks)
except:
logger.warning("Not able to display tracks, error page returned")
return render_template('error.html')
@app.route('/add', methods=['POST'])
def add_entry():
"""View that process a POST with new song input
:return: redirect to index page
"""
try:
track1 = Track(artist=request.form['artist'], album=request.form['album'], title=request.form['title'])
db.session.add(track1)
db.session.commit()
logger.info("New song added: %s by %s", request.form['title'], request.form['artist'])
return redirect(url_for('index'))
except:
logger.warning("Not able to display tracks, error page returned")
return render_template('error.html')
if __name__ == "__main__":
app.run(debug=app.config["DEBUG"], port=app.config["PORT"], host=app.config["HOST"])