-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
48 lines (36 loc) · 1.46 KB
/
Copy pathapp.py
File metadata and controls
48 lines (36 loc) · 1.46 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
from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy
from send_email import send_email
from sqlalchemy.sql import func
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://******:********@localhost/height_collector'
db = SQLAlchemy(app)
class Data(db.Model):
__tablename__ = "data"
id = db.Column(db.Integer, primary_key=True)
email_ = db.Column(db.String(120), unique=True)
height_ = db.Column(db.Integer)
def __init__(self, email_, height_):
self.email_ = email_
self.height_ = height_
@app.route("/")
def index():
return render_template("index.html")
@app.route("/success", methods=['POST'])
def success():
if request.method == 'POST':
email = request.form["email_name"]
height = request.form["height_name"]
if db.session.query(Data).filter(Data.email_==email).count() == 0:
data = Data(email, height)
db.session.add(data)
db.session.commit()
average_height = db.session.query(func.avg(Data.height_)).scalar()
average_height = round(average_height, 1)
count = db.session.query(Data.height_).count()
send_email(email, height, average_height, count)
return render_template("success.html")
return render_template("index.html", text="Seems like we've got something from that email address already!")
if __name__ == "__main__":
app.debug=True
app.run()