-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
102 lines (89 loc) · 3.67 KB
/
Copy pathapp.py
File metadata and controls
102 lines (89 loc) · 3.67 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
import os
from flask import Flask, request, redirect, render_template, flash
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config.from_object(os.environ.get('APP_SETTINGS'))
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
from models import College
@app.route('/')
def index():
try:
colleges = College.query.all()
return render_template("index.html", colleges=colleges)
except Exception as e:
return str(e)
@app.route('/add_college', methods=['GET', 'POST'])
def add_college():
if request.method == "POST":
name = request.form.get('name')
app_deadline = request.form.get('app_deadline')
rec_deadline = request.form.get('rec_deadline')
num_essays = request.form.get('num_essays')
midyear_report = request.form.get('midyear_report')
acceptance_rate = request.form.get('acceptance_rate')
platform = request.form.get('platform')
try:
college = College(
name=name,
app_deadline=app_deadline,
rec_deadline=rec_deadline,
num_essays=num_essays,
midyear_report=midyear_report,
acceptance_rate=acceptance_rate,
platform=platform,
)
if request.form.get('name') == "":
raise ValueError('There is no name specified for the college. Please try again with a name.')
else:
db.session.add(college)
db.session.commit()
except ValueError as v:
flash(str(v))
return redirect('/')
except Exception as e:
return str(e)
return redirect('/')
else:
return render_template("college.html")
@app.route('/edit_college', methods=['GET', 'POST'])
def edit_college():
"""
Workflow: A user selects a college from the 'edit college' dropdown and encounters the else case of this
method because there is no post request yet. Then, once they edit the information, they submit the form
with a post request which enters the if condition of the function.
:return: if the user successfully edits, the '/' url
"""
college = College.query.filter_by(name=request.args.get('name')).first()
# Run this once the user makes some edits and submits the form with a post request
if request.method == "POST":
college.name = request.form.get('name')
college.app_deadline = request.form.get('app_deadline')
college.rec_deadline = request.form.get('rec_deadline')
college.num_essays = request.form.get('num_essays')
college.midyear_report = request.form.get('midyear_report')
college.acceptance_rate = request.form.get('acceptance_rate')
college.platform = request.form.get('platform')
db.session.commit()
return redirect('/')
# Run this if the user is visiting the edit page for the first time (no edits yet)
else:
try:
is_editing = True
try:
if request.args.get('name') is None:
raise ValueError('There is no college specified for editing.')
except ValueError as v:
flash(str(v))
return redirect('/')
return render_template("college.html", college=college, is_editing=is_editing)
except Exception as e:
return str(e)
@app.route('/delete_college', methods=['GET', 'POST'])
def delete_college():
college_to_delete = College.query.filter_by(name=request.args.get('name')).first()
db.session.delete(college_to_delete)
db.session.commit()
return redirect('/')
if __name__ == '__main__':
app.run()