-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathapp.py
57 lines (47 loc) · 2.43 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
from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.validators import InputRequired
from datetime import datetime
from os import environ
app = Flask(__name__)
# Configure secret key for flask - used for session sharing
app.config['SECRET_KEY'] = 'your-secret-key'
# Configure database details, these need to be changed depending on how you configured mysql container
db_user = environ.get('DB_USER', 'CHANGEME')
db_pass = environ.get('DB_PASS', 'CHANGEME')
db_host = environ.get('DB_HOST', 'CHANGEME')
db_name = environ.get('DB_NAME', 'CHANGEME')
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://'+db_user+':'+db_pass+'@'+db_host+'/'+db_name
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
student = environ.get('STUDENT', 'Default Student')
college = environ.get('COLLEGE', 'Default College')
db = SQLAlchemy(app)
class Temperature(db.Model):
id = db.Column(db.Integer, primary_key=True)
celsius = db.Column(db.Float, nullable=False)
fahrenheit = db.Column(db.Float, nullable=False)
date = db.Column(db.DateTime, default=datetime.utcnow)
ip_address = db.Column(db.String(20), nullable=False)
user_agent = db.Column(db.String(200), nullable=False)
with app.app_context():
db.create_all()
class TemperatureForm(FlaskForm):
celsius = StringField('Celsius', validators=[InputRequired()])
submit = SubmitField('Convert')
@app.route('/', methods=['GET', 'POST'])
def index():
form = TemperatureForm()
if form.validate_on_submit():
celsius = float(form.celsius.data)
fahrenheit = round(((celsius * 1.8) + 32),2)
temperature = Temperature(celsius=celsius, fahrenheit=fahrenheit, ip_address=request.remote_addr, user_agent=request.user_agent.string)
db.session.add(temperature)
db.session.commit()
temperatures = Temperature.query.order_by(Temperature.date.desc()).limit(10).all()
# log_entries = [(t.fahrenheit, t.ip_address, t.user_agent.split()[0], t.date.strftime('%m/%d/%Y %I:%M:%S %p')) for t in temperatures]
log_entries = [(t.date.strftime('%m/%d/%Y %I:%M:%S %p'), t.celsius, t.fahrenheit, t.ip_address, t.user_agent.split()[0], ) for t in temperatures]
return render_template('index.html', form=form, log_entries=log_entries, student=student, college=college)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)