-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
126 lines (97 loc) · 3.63 KB
/
app.py
File metadata and controls
126 lines (97 loc) · 3.63 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
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
import os
from datetime import datetime
from flask import Flask, jsonify, redirect, render_template, request, send_from_directory, url_for
from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy
from flask_wtf.csrf import CSRFProtect
from sqlalchemy import MetaData
app = Flask(__name__, static_folder='static')
csrf = CSRFProtect(app)
# WEBSITE_HOSTNAME exists only in production environment
if 'WEBSITE_HOSTNAME' not in os.environ:
# local development, where we'll use environment variables
print("Loading config.development and environment variables from .env file.")
app.config.from_object('azureproject.development')
else:
# production
print("What are you doing here, this is just a workshop.")
print("Loading config.production.")
app.config.from_object('azureproject.production')
app.config.update(
SQLALCHEMY_DATABASE_URI=app.config.get('DATABASE_URI'),
SQLALCHEMY_TRACK_MODIFICATIONS=False,
)
# Initialize the database connection
db = SQLAlchemy(app)
# Enable Flask-Migrate commands "flask db init/migrate/upgrade" to work
migrate = Migrate(app, db)
@app.route('/', methods=['GET'])
def index():
return render_template('index.html')
@app.route('/subject/<name>', methods = ['GET'])
def subject(name):
# TODO Write SQL Query to get all artworks that depict the subject given in {name}
sql = f'''
SELECT *
FROM ...
'''
with db.engine.connect() as conn:
result = conn.execute(db.text(sql)).fetchall()
rows = [dict(row._mapping) for row in result]
return render_template('index.html', results = rows)
@app.route('/style/<name>', methods = ['GET'])
def style(name):
# TODO Write SQL Query to get all the artworks of the given style. Bonus points if you can get all columns filled. -> {name}
sql = f'''
SELECT
FROM
'''
with db.engine.connect() as conn:
result = conn.execute(db.text(sql)).fetchall()
rows = [dict(row._mapping) for row in result]
return render_template('index.html', results = rows)
@app.route('/museum/<name>', methods = ['GET'])
def museum(name):
name = name.lower()
# TODO Write SQL Query to get all artworks from a certain museum. -> {name}
sql = f'''
SELECT
FROM
'''
with db.engine.connect() as conn:
result = conn.execute(db.text(sql)).fetchall()
rows = [dict(row._mapping) for row in result]
return render_template('index.html', results = rows)
@app.route('/add_artwork', methods=['POST'])
@csrf.exempt
def add_artwork():
try:
work_id = request.form.get('work_id')
work_name = request.form.get('work_name')
artist_id = request.form.get('artist_id')
style = request.form.get('style')
museum_id = request.form.get('museum_id')
except KeyError as e:
return jsonify({'error': f'Missing parameter: {str(e)}'}), 400
sql = '''
INSERT INTO public."work" ("work_id", "name", "artist_id", "style", "museum_id")
VALUES (:work_id, :work_name, :artist_id, :style, :museum_id)
'''
with db.engine.begin() as conn:
conn.execute(db.text(sql), {
'work_id': work_id,
'work_name': work_name,
'artist_id': artist_id,
'style': style,
'museum_id': museum_id,
})
return redirect(url_for('index'))
@app.route('/add_artwork_form', methods=['GET'])
def add_artwork_form():
return render_template('add.html')
@app.route('/dsa.ico')
def dsa():
return send_from_directory(os.path.join(app.root_path, 'static'),
'dsa.ico', mimetype='image/png')
if __name__ == '__main__':
app.run()