|
| 1 | +#from flask.ext.sqlalchemy import SQLAlchemy |
| 2 | +#from flask_caching.backends.cache import SQLAlchemy |
| 3 | +from flask import Flask, render_template, request |
| 4 | +from flask_sqlalchemy import SQLAlchemy |
| 5 | +from send_email import send_email |
| 6 | +from sqlalchemy.sql import func |
| 7 | + |
| 8 | +app=Flask(__name__) |
| 9 | + |
| 10 | +# ------------------------------------DATABASE CODE START--------------------------------------------------------------------------------- |
| 11 | +# notice dict key and its value...This is your database connection... |
| 12 | +app.config['SQLALCHEMY_DATABASE_URI']='postgresql://postgres:1234@localhost/height_collector' |
| 13 | +db=SQLAlchemy(app) # now the app object will know where the database is... |
| 14 | + |
| 15 | +class Data(db.Model): # SQLAlchemy object now inheriting the Model class... |
| 16 | + __tablename__="data" |
| 17 | + id=db.Column(db.Integer, primary_key=True) # variables local to this class only... |
| 18 | + email_=db.Column(db.String(120), unique=True) |
| 19 | + height_=db.Column(db.Integer) # if user enters a decimal number you get error (you would have to use float data type...) |
| 20 | + |
| 21 | + # initialize the class instance variables... |
| 22 | + def __init__(self, email_, height_): # self is the object being instantiated...upon calling the class constructor... |
| 23 | + self.email_=email_ |
| 24 | + self.height_=height_ |
| 25 | +# ------------------------------------DATABASE CODE END------------------------------------------------------------------------------------- |
| 26 | + |
| 27 | +@app.route("/") # GET method is the default method for request object of HTTP class...(implicitly) |
| 28 | +def index(): |
| 29 | + return render_template("index.html") |
| 30 | + |
| 31 | +@app.route("/success", methods=['POST']) # explicitly specifying POST method... |
| 32 | +def success(): |
| 33 | + if request.method=='POST': # making sure this is a POST request and not a GET request... |
| 34 | + email=request.form['email_name'] # variables local to this function only... email_name is a key which holds a value (key/value pair...) |
| 35 | + height=request.form['height_name'] |
| 36 | + print(email, height) |
| 37 | + print(request.form) # print out the type attribute of the form for the email... |
| 38 | + if db.session.query(Data).filter(Data.email_==email).count() == 0: |
| 39 | + data=Data(email,height) # this maps the instance variables to the user input otherwise you get: sqlalchemy.orm.exc.UnmappedInstanceError |
| 40 | + db.session.add(data) # SQLAlchemy object... |
| 41 | + db.session.commit() # commit changes to database... |
| 42 | + # notice this line of code is after the db.session.commit() so it can also grab the last user entry... |
| 43 | + # .scalar() extracts the number out of this variable average_height...which is an SQL statement returned by db.session.query(func.avg(Data.height_)) |
| 44 | + average_height=db.session.query(func.avg(Data.height_)).scalar() # grabbing all the height data input from the database including the last user's entry! |
| 45 | + average_height=round(average_height,1) |
| 46 | + count=db.session.query(Data.height_).count() # counting the height values in the database table... |
| 47 | + send_email(email, height, average_height, count) # function call, invoking the email code... |
| 48 | + print(average_height) |
| 49 | + return render_template("success.html") # if email isn't already in the database, return success.html |
| 50 | + return render_template('index.html', # else, return index.html and have user have another go... |
| 51 | + text="Hummm...seems like we got something from that email address already.") |
| 52 | + |
| 53 | +if __name__ == '__main__': |
| 54 | + app.debug=True |
| 55 | + app.run() # default port is port 5000 if left empty app.run(port=5002)... |
0 commit comments