Redid the index to use flask forms. Removed un-needed html pages. Started adding database.

This commit is contained in:
dan
2022-01-20 02:22:36 -05:00
parent fa58df3f43
commit 2cbed43b29
7 changed files with 127 additions and 83 deletions

75
app.py
View File

@@ -1,11 +1,80 @@
from flask import Flask, render_template
from flask import Flask, render_template, request, flash, redirect
import datetime
from sqlalchemy import create_engine, Column, Integer, Date, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
app = Flask(__name__)
app.config['SECRET_KEY'] = 'fa6166b0218186fb852429060105aca18e0f979240be8ebf'
Base = declarative_base()
@app.route('/')
class Dates(Base):
__tablename__ = 'Dates'
id = Column(Integer, primary_key=True, autoincrement=True)
dateType = Column(Integer),
dateValue = Column(Date)
class DatesMetadata(Base):
__tablename__ = 'DatesMetadata'
id = Column(Integer, primary_key=True, autoincrement=True)
dateTypeID = Column(Integer)
DateTypeName = Column(String)
class Settings(Base):
__tablename__ = 'Settings'
id = Column(Integer, primary_key=True, autoincrement=True)
settingType = Column(Integer)
settingVale = Column(String)
class SettingsMetadata(Base):
__tablename__ = 'SettingsMetadata'
id = Column(Integer, primary_key=True, autoincrement=True)
settingTypeID = Column(Integer)
settingTypeName = Column(String)
engine = create_engine('sqlite:///static/db.db')
Dates.__table__.create(bind=engine, checkfirst=True)
Settings.__table__.create(bind=engine, checkfirst=True)
DatesMetadata.__table__.create(bind=engine, checkfirst=True)
SettingsMetadata.__table__.create(bind=engine, checkfirst=True)
Session = sessionmaker(bind=engine)
session = Session()
@app.route('/', methods=('GET', 'POST'))
@app.route('/index.html', methods=('GET', 'POST'))
def index():
return render_template("index.html")
plantdate = datetime.datetime(2022, 1,19)
last = datetime.datetime(2022, 1,19)
freq = 7
next = last + datetime.timedelta(days=freq)
alerts = {'text': False, 'email': True}
if request.method == 'POST':
plantdate = request.form['plantDate']
last = request.form['lastDate']
next = datetime.datetime.strptime(last,'%Y-%m-%d') + datetime.timedelta(days=freq)
plant = {'plant': plantdate, 'last': last, 'next': next, 'freq': freq}
return render_template("index.html", plant=plant, alerts=alerts)
else:
dates = {'plant': '', 'last': '', 'next':'', 'freq': ''}
dates['plant'] = session.query(Dates.dateValue).filter(Dates.dateType == 1).first()
dates['last'] = session.query(Dates.dateValue).filter(Dates.dateType == 2).first()
dates['next'] = session.query(Dates.dateValue).filter(Dates.dateType == 3).first()
dates['freq'] = session.query(Dates.dateValue).filter(Dates.dateType == 4).first()
flash(dates['plant'])
return render_template("index.html", plant=dates, alerts=alerts)
if __name__ == '__main__':

18
templates/base.html Normal file
View File

@@ -0,0 +1,18 @@
<html>
<head>
<title>Hydro</title>
</head>
<body>
<div>Hydro: <a href="index.html">home</a></div>
<br>
{% with messages = get_flashed_messages() %}
{% if messages %}
{% for message in messages %}
{{ message }}
{% endfor %}
{% endif %}
{% endwith %}
<br>
{% block content %}{% endblock %}
</body>
</html>

View File

@@ -1,10 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
</body>
</html>

View File

@@ -1,41 +1,38 @@
<!DOCTYPE html>
<html lang="en">
<script src="../static/scripts.js"></script>
<head>
<meta charset="UTF-8">
<title>Hydro</title>
</head>
{% extends "base.html" %}
{% block content %}
<body>
<h1>Information</h1>
<div>
Plant Date:
1/19/2022
<a href="plantdate.html">Change</a>
</div>
<div>
Last Feed Date:
<a href="lastfeeddate.html">Change</a>
</div>
<div>
Next Feed Date:
<a href="nextfeeddate.html">Change</a>
</div>
<div>
Feed Frequency:
<a href="freedfreq.html">Change</a>
</div>
<form method="post">
<label>Plant Date</label>
<input type="date" name="plantDate"
value= {{ plant.plant }}>
<br>
<label>Last Feed Date</label>
<input type="date" name="lastDate"
value= {{ plant.last }}>
<br>
<label>Next Feed Date</label>
<input type="date" name="nextDate"
value= {{ plant.next }}>
<br>
Feed Frequency: {{ plant.freq }} days
<br>
<button type="submit">Update</button>
</form>
<h1>Alerts</h1>
<div>
<input type="checkbox" id="alert_text" name="AlertText" value=true>
<form method="post">
<input type="checkbox" id="alert_text" name="AlertText" value=alerts.text {{ "checked" if alerts.text else "" }}>
<label for="alert_text">Text Message</label>
<input type="checkbox" id="alert_email" name="AlertEmail" value=true>
<input type="checkbox" id="alert_email" name="AlertEmail" value=alerts.email {{ "checked" if alerts.email else "" }}>
<label for="alert_email">Email</label><br>
<button class=button onclick="updateAlerts()">Save</button>
<button type="submit">Save</button>
</form>
</div>
<br><br>
<div>
<button class=button onclick="updateFeed()">Update Feed</button>
</div>
</body>
</html>
{% endblock %}

View File

@@ -1,10 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
</body>
</html>

View File

@@ -1,10 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
</body>
</html>

View File

@@ -1,10 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
</body>
</html>