-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
338 lines (273 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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.exc import PendingRollbackError
import os
from datetime import timedelta, datetime
from urllib import parse
import secrets
import pandas as pd
from utils import delete_beneficiary_data, pandas_to_html
from dotenv import load_dotenv
import re
load_dotenv()
from flask import Flask, render_template, request, redirect, url_for, session, flash
from flask_login import (
LoginManager,
UserMixin,
login_user,
login_required,
logout_user,
current_user,
)
app = Flask(__name__)
server = "510-emergencies.database.windows.net"
database = "relief-app-users"
username = os.getenv("SQL_USERNAME")
password = os.getenv("SQL_PASSWORD")
driver = "{ODBC Driver 17 for SQL Server}"
odbc_str = (
"DRIVER="
+ driver
+ ";SERVER="
+ server
+ ";PORT=1433;UID="
+ username
+ ";DATABASE="
+ database
+ ";PWD="
+ password
)
app.config["SECRET_KEY"] = secrets.token_urlsafe(16)
app.config["SQLALCHEMY_DATABASE_URI"] = (
"mssql+pyodbc:///?odbc_connect=" + parse.quote_plus(odbc_str)
)
app.config["SQLALCHEMY_COMMIT_ON_TEARDOWN"] = True
db = SQLAlchemy(app)
class User(UserMixin, db.Model):
id = db.Column(
db.Integer, primary_key=True
) # primary keys are required by SQLAlchemy
email = db.Column(db.String(100), unique=True)
password = db.Column(db.String(100))
name = db.Column(db.String(100))
class Distribution(db.Model):
__table_args__ = {"extend_existing": True}
id = db.Column(
db.Integer, primary_key=True
) # primary keys are required by SQLAlchemy
user_email = db.Column(db.String(100))
name = db.Column(db.String(100))
place = db.Column(db.String(100))
date = db.Column(db.Date)
items = db.Column(db.String(100))
donor = db.Column(db.String(100))
login_manager = LoginManager()
login_manager.login_view = "login"
login_manager.init_app(app)
@login_manager.user_loader
def load_user(id):
return User.query.get(int(id))
@login_manager.unauthorized_handler
def unauthorized_handler():
return render_template("login.html")
def check_login(email):
for try_ in range(100):
try:
user = User.query.filter_by(email=email).first()
return user
except PendingRollbackError:
db.session.rollback()
@app.route("/login", methods=["GET"])
def login():
return render_template("login.html")
@app.route("/login", methods=["POST"])
def login_post():
# login code goes here
email = request.form.get("email")
password = request.form.get("password")
# check if connection still active
user = check_login(email=email)
# check if the user actually exists
# take the user-supplied password, hash it, and compare it to the hashed password in the database
if not user or user.password.strip() != password:
flash("Please check your login details and try again.")
return redirect(
url_for("login")
) # if the user doesn't exist or password is wrong, reload the page
# if the above check passes, then we know the user has the right credentials
login_user(user, remember=False, duration=timedelta(days=1))
return render_template(
"index_distrib.html", email=current_user.email
) # url_for('main.index'))
@app.route("/signup", methods=["GET"])
def signup():
return render_template("signup.html")
@app.route("/signup", methods=["POST"])
def signup_post():
# code to validate and add user to database goes here
email = request.form.get("email")
name = request.form.get("name")
password = request.form.get("password")
if email == "" or name == "" or password == "":
flash("Insert your email, name and password")
return redirect(url_for("signup"))
if (
len(password) < 8
or not any(not c.isalnum() for c in password)
or not any(c.isupper() for c in password)
):
flash(
"Password must be at least 8 characters long and contain at least one special character and one uppercase letter"
)
return redirect(url_for("signup"))
if not re.match(r"[^@]+@[^@]+\.[^@]+", email):
flash("Use a valid email address")
return redirect(url_for("signup"))
user = check_login(email=email)
if (
user
): # if a user is found, we want to redirect back to signup page so user can try again
flash("This email address is already associated with a ReliefBox account")
return redirect(url_for("signup"))
# create a new user with the form data. Hash the password so the plaintext version isn't saved.
new_user = User(email=email, name=name, password=password)
# add the new user to the database
db.session.add(new_user)
db.session.commit()
return redirect(url_for("login"))
@app.route("/logout")
@login_required
def logout():
logout_user()
session.clear()
return redirect(url_for("login"))
@app.route("/index_distrib")
@login_required
def index_distrib():
return render_template("index_distrib.html", email=current_user.email)
@app.route("/name_distrib", methods=["GET"])
@login_required
def name_distrib():
return render_template("name_distrib.html")
@app.route("/create_distrib", methods=["POST"])
@login_required
def create_distrib():
if "distrib_name" in request.form.keys():
if (
request.form["distrib_name"] == ""
or request.form["distrib_place"] == ""
or request.form["distrib_date"] == ""
):
flash("Specify name, location and date of the distribution.")
return redirect(url_for("name_distrib"))
if (
datetime.strptime(request.form["distrib_date"], "%Y-%m-%d").date()
< datetime.now().date()
):
flash("Date of the distribution cannot be in the past.")
return redirect(url_for("name_distrib"))
# create distrib
distrib = Distribution.query.filter_by(
user_email=current_user.email, name=request.form["distrib_name"]
).first() # if this returns a distribution, then the name already exists in database
if distrib: # if a distribution is found, redirect back to name_distrib
flash(f"Distribution {request.form['distrib_name']} already exists")
return redirect(url_for("name_distrib"))
# create a new distribution
new_distrib = Distribution(
user_email=current_user.email,
name=request.form["distrib_name"],
place=request.form["distrib_place"],
date=datetime.strptime(request.form["distrib_date"], "%Y-%m-%d").date(),
items=request.form["distrib_items"],
donor=request.form["distrib_donor"],
)
# add the new distribution to the database
db.session.add(new_distrib)
db.session.commit()
db.session.flush()
session["distrib_id"] = new_distrib.id
for feature in ["name", "place", "date", "items", "donor"]:
session[f"distrib_{feature}"] = request.form[f"distrib_{feature}"]
return redirect(url_for("main.index"))
else:
return render_template("name_distrib.html")
def get_list_distrib():
distributions = Distribution.query.filter_by(user_email=current_user.email)
distrib_list = pd.DataFrame()
for distrib_ in distributions:
distrib_features = {}
distrib_features["distrib_id"] = distrib_.id
distrib_features["Name"] = distrib_.name
distrib_features["Location"] = distrib_.place
distrib_features["Date"] = distrib_.date
distrib_features["Items distributed"] = distrib_.items
distrib_features["Donor"] = distrib_.donor
df_dictionary = pd.DataFrame([distrib_features])
distrib_list = pd.concat([distrib_list, df_dictionary], ignore_index=True)
return distrib_list
@app.route("/list_distrib", methods=["GET"])
@login_required
def list_distrib():
distrib_list = get_list_distrib()
if len(distrib_list) == 0:
return render_template("no_distrib.html")
else:
columns, rows = pandas_to_html(distrib_list, titlecase=True)
return render_template("list_distrib.html", columns=columns, rows=rows)
@app.route("/select_distrib", methods=["POST"])
@login_required
def select_distrib():
if "distrib_id" in request.form.keys():
distrib_ = Distribution.query.filter_by(
user_email=current_user.email, id=int(float(request.form["distrib_id"]))
).first()
session[f"distrib_id"] = distrib_.id
session[f"distrib_name"] = distrib_.name
session[f"distrib_place"] = distrib_.place
session[f"distrib_date"] = distrib_.date
session[f"distrib_items"] = distrib_.items
session[f"distrib_donor"] = distrib_.donor
return redirect(url_for("main.index"))
else:
return render_template("list_distrib.html")
@app.route("/list_distrib_delete", methods=["GET"])
@login_required
def list_distrib_delete():
distrib_list = get_list_distrib()
if len(distrib_list) == 0:
return render_template("no_distrib.html")
else:
columns, rows = pandas_to_html(distrib_list, titlecase=True)
return render_template("list_distrib_delete.html", columns=columns, rows=rows)
@app.route("/delete_distrib_confirm", methods=["POST"])
@login_required
def delete_distrib_confirm():
if "distrib_id" in request.form.keys():
session["distrib_id"] = int(float(request.form["distrib_id"]))
return render_template(
"delete_distrib_confirm.html", distrib_id=session["distrib_id"]
)
else:
return render_template("list_distrib_delete.html")
@app.route("/delete_distrib", methods=["POST"])
@login_required
def delete_distrib():
if "distrib_id" in request.form.keys():
session["distrib_id"] = int(float(request.form["distrib_id"]))
# delete beneficiaries
delete_beneficiary_data(
user_email=current_user.email, distrib_id=session["distrib_id"]
)
# delete distribution
distrib_to_delete = Distribution.query.filter_by(
id=session["distrib_id"]
).first()
db.session.delete(distrib_to_delete)
db.session.commit()
return render_template("index_distrib.html", email=current_user.email)
else:
return render_template("list_distrib_delete.html")
from main import main as main_blueprint
app.register_blueprint(main_blueprint)
if __name__ == "__main__":
app.run()