-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
239 lines (187 loc) · 7.26 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
from datetime import datetime, date, timedelta
from flask import Flask, jsonify,render_template,flash,redirect, template_rendered,url_for,session,logging,request
from wtforms import Form,StringField,TextAreaField,PasswordField,validators
from flask_mysqldb import MySQL
from passlib.hash import sha256_crypt
from data import Mothers
from birthpredictionDays import predDays
from birthpredictionmodel import timePred
from functools import wraps
#instantiate Flask class
app=Flask(__name__)
#configure database (MYSQL)
app.config['MYSQL_HOST']='localhost'
app.config['MYSQL_USER']='root'
app.config['MYSQL_PASSWORD']='Dim)YxKK(ahtr2R'
app.config['MYSQL_DB']='iaca'
app.config['MYSQL_CURSORCLASS']='DictCursor'
#initialize mysql
mysql=MySQL(app)
#get mothers
Mothers=Mothers()
#create index page route
@app.route('/')
def index():
return render_template('home.html')
#getting single mother
@app.route('/mother/<string:id>/')
def mother(id):
#create cursor
cur=mysql.connection.cursor()
res=cur.execute("SELECT * FROM mothers WHERE id=%s",[id])
mother=cur.fetchone()
return render_template('mother.html',mother=mother)
class RegisterForm(Form):
employeename=StringField('Name',[validators.Length(min=4,max=100)])
hospital=StringField('Hospital',[validators.Length(min=4,max=100)])
department=StringField('Department',[validators.Length(min=1,max=100)])
position=StringField('Position',[validators.Length(min=3,max=100)])
team=StringField('Team',[validators.Length(min=1,max=100)])
phone_number=StringField('Phone Number',[validators.Length(min=9,max=10)])
email=StringField('Email ',[validators.Length(min=12,max=100)])
password=PasswordField(
'Password',[
validators.DataRequired(),
validators.EqualTo('confirm',message='Passwords mismatched')
]
)
confirm=PasswordField('Confirm Password')
@app.route('/register',methods=['GET','POST'])
def registerOfficial():
form=RegisterForm(request.form)
if request.method=='POST' and form.validate():
empname=form.employeename.data
pemail=form.email.data
pnum=form.phone_number.data
hosp=form.hospital.data
dept=form.department.data
post=form.position.data
tm=form.team.data
fpass=sha256_crypt.encrypt(str(form.password.data))
#create cursor
cur=mysql.connection.cursor()
cur.execute("INSERT INTO healthuser(ename, department, hospital, position, team, pnumber, email, upassword) VALUES (%s,%s,%s,%s,%s,%s,%s,%s)",(empname,dept,hosp,post,tm,pnum,pemail,fpass))
#commit to the Database the Script
mysql.connection.commit()
#close the connection
cur.close()
flash('Congratulations, you are now a member of staff.')
return redirect(url_for('login'))
return render_template('register.html',form=form)
#user login route page
@app.route('/login',methods=['GET', 'POST'])
def login():
if request.method=='POST':
uname=request.form['username']
upass=request.form['password']
hospital=request.form['hospital']
#create cursor
cur=mysql.connection.cursor()
result=cur.execute("SELECT * FROM healthuser WHERE ename= %s",[uname])
if result>0:
data=cur.fetchone()
dbpass=data['upassword']
dbhospital=data['hospital']
#compare passwords
if sha256_crypt.verify(upass,dbpass):
#pass in and create sessions
session['logged_in']=True
session['username']=uname
session['hospital']=hospital
flash('Welcome back to the platform','success')
return redirect(url_for('dashboard'))
else:
error='Please try retyping your password.'
return render_template('login.html',error=error)
#close connection
cur.close()
else:
error='Sorry, this user doesn\'t exist or nothing has been typed yet'
return render_template('login.html',error=error)
return render_template('login.html')
#check if user is logged_in
def is_logged_in(f):
@wraps(f)
def wrap(*args,**kwargs):
if 'logged_in' in session:
return f(*args,**kwargs)
else:
flash('You are supposed to be logged in','error')
return redirect(url_for('login'))
return wrap
#log out
@app.route('/logout')
@is_logged_in
def logout():
session.clear()
flash('You have closed your space. Thank you for your time, come back soon','success')
return redirect(url_for('index'))
@app.route('/dashboard')
@is_logged_in
def dashboard():
#create cursor
cur=mysql.connection.cursor()
res=cur.execute("SELECT * FROM mothers")
mothers=cur.fetchall()
if res>0:
return render_template('dashboard.html',mothers=mothers)
else:
msg='No mother was found in here'
return render_template('dashboard.html',msg=msg)
cur.close()
#get single mom
class RegisterFormPatients(Form):
name=StringField('Name',[validators.Length(min=4,max=100)])
address=StringField('Address',[validators.Length(min=4,max=150)])
pregDate=StringField('Date (mm-dd-yyyy)',[validators.Length(min=10,max=10)])
bpr=StringField('Blood Pressure',[validators.Length(min=6,max=8)])
number=StringField('Phone Number',[validators.Length(min=9,max=9)])
weight=StringField('Weight',[validators.Length(min=2,max=10)])
bmi=StringField('BMI',[validators.Length(min=2,max=10)])
@app.route('/add',methods=['GET','POST'])
@is_logged_in
def addmom():
form=RegisterFormPatients(request.form)
if request.method=='POST' and form.validate():
momName=form.name.data
predDate=form.pregDate.data
bmi=form.bmi.data
bmi_int=float(bmi)
wgt=form.weight.data
wgt_int=float(wgt)
bpr=form.bpr.data
addr=form.address.data
phone=form.number.data
brttime=timePred(bmi_int,wgt_int)
gestdays=predDays(bmi_int,wgt_int)
#create date object
predDt=datetime.strptime(predDate, '%m-%d-%Y')
dur=timedelta(days=gestdays)
finaldate=predDt+dur
cur=mysql.connection.cursor()
cur.execute("INSERT INTO mothers(motherName,pregDate,dys,bmi,mweight,bpr,addr,phone,birthTime,birthDate) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",(momName,predDate,gestdays,bmi,wgt,bpr,addr,phone,brttime,finaldate))
mysql.connection.commit()
cur.close()
flash(momName+' has been added to the database.','success')
return redirect(url_for('dashboard'))
return render_template('add.html',form=form)
#daily records page
@app.route('/daily')
@is_logged_in
def daily():
#create cursor
cur=mysql.connection.cursor()
now = datetime.now()
current_time = now.strftime("%m-%d-%Y")
strval=str(current_time)
res=cur.execute("SELECT * FROM mothers WHERE birthDate=%s",[strval])
mothers=cur.fetchall()
if res>0:
return render_template('daily.html',mothers=mothers)
else:
msg='No mother was found in here'
return render_template('daily.html',msg=msg)
cur.close()
if __name__=='__main__':
app.secret_key='keyD9090#'
app.run(debug=True)