This repository has been archived by the owner on Oct 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
393 lines (335 loc) · 15.4 KB
/
main.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
from flask import Flask, flash, request, session
from flask.templating import render_template
from werkzeug.utils import redirect, secure_filename
import bcrypt
from functions.users import check_existing_user, add_new_user, check_user_credentials
from functions.scheduler import add_medicine, fetch_user_schedule, card, no_data_check
from functions.dashboard import user_name, today_card, tomorrow_card, day_after_card
from functions.medicines import get_all_medicines, medicine_card
from functions.medscraper import get_medicines
from functions.prescription import add_prescription, add_prescription_record, fetch_prescriptions, prescription_card
from functions.notifications import *
from datetime import datetime, timedelta
import os
app = Flask(__name__)
app.secret_key = 'subhogay'
app.config['UPLOAD_FOLDER'] = 'uploads'
app.config['MAX_CONTENT_LENGTH'] = 20 * 1000 * 1000
@app.route('/')
def index():
try:
if session['login']:
login = True
else:
login = False
except KeyError:
session['login'] = False
login = session['login']
return render_template('index.html', login=login)
# Authentication
@app.route('/signup')
def signup():
return render_template('signup.html')
@app.route('/signup/verify', methods=['GET', 'POST'])
def signup_verify():
if request.method == 'POST':
data = request.form
name = data['name']
email = data['email']
contact = data['contact']
emergency_contact = data['emergency_contact']
password = data['password'].encode()
hashed = bcrypt.hashpw(password, bcrypt.gensalt())
if not check_existing_user(email):
add_new_user(name, email, hashed, contact, emergency_contact)
flash('Registered')
return redirect('/')
else:
flash('User with this email already exists.', 'error')
return render_template('signup.html')
@app.route('/login')
def login():
try:
if session['login']:
return redirect('/dashboard')
else:
return render_template('login.html')
except:
return render_template('login.html')
@app.route('/login/verify', methods=["GET", "POST"])
def login_verify():
if request.method == "POST":
data = request.form
email = data['email']
password = data['password']
if check_user_credentials(email, password):
session['user'] = email
session['login'] = True
flash('Logged in')
return redirect('/dashboard')
else:
flash('Incorrect username or password!', 'error')
return render_template('login.html')
@app.route('/logout')
def logout():
session.clear()
session['login'] = False
session['user'] = None
return redirect('/login')
@app.route('/dashboard')
def dashboard():
try:
if session['user']:
if not no_data_check(session['user']):
data = fetch_user_schedule(datetime.now(), session['user'])
now = str(datetime.now()).split('.')[0][-8:-3]
data['current_time'] = now
upcoming = {}
completed = {}
data = dict(sorted(data.items(), key=lambda item: item[1]))
upcoming_count = 0
completed_count = 0
checkpoint = False
for medicine in data:
if medicine == 'current_time':
checkpoint = True
elif checkpoint:
upcoming_count += 1
upcoming[medicine] = data[medicine]
else:
completed_count += 1
completed[medicine] = data[medicine]
today_card_html = ''
for medicine in upcoming:
today_card_html += today_card(medicine, upcoming[medicine])
first = 0
for medicine in upcoming:
first += 1
if first < 2:
first_medicine = medicine
first_medicine_time = upcoming[medicine]
card1 = f"""<div class="md:p-7 p-4">
<h2 class=" text-xl text-center text-primary-green-dark capitalize">Next Dose</h2>
<h3 class="text-sm text-primary-green-dark text-center">{first_medicine_time} - {first_medicine}</h3>
</div>"""
card2 = f"""<div class="md:p-7 p-4">
<h2 class="text-xl text-center text-primary-blue-dark capitalize">Today</h2>
<h3 class="text-sm text-primary-blue-dark text-center">{upcoming_count + completed_count} doses</h3>
</div>"""
card3 = f"""<div class="md:p-7 p-4">
<h2 class="text-lg text-center text-primary-yellow-dark capitalize">
<span>{first_medicine}</span>
</h2>
<h3 class="text-sm text-primary-yellow-dark text-center">{first_medicine_time}</h3>
</div>"""
count = 0
tomorrow_card_html = ''
data = fetch_user_schedule(datetime.now() + timedelta(days=1), session['user'])
data = dict(sorted(data.items(), key=lambda item: item[1]))
for medicine in data:
count += 1
if count < 4:
tomorrow_card_html += tomorrow_card(medicine, data[medicine])
count = 0
day_after_card_html = ''
data = fetch_user_schedule(datetime.now() + timedelta(days=2), session['user'])
data = dict(sorted(data.items(), key=lambda item: item[1]))
for medicine in data:
count += 1
if count < 4:
day_after_card_html += day_after_card(medicine, data[medicine])
session['upcoming_count'] = upcoming_count
if not upcoming and not completed:
card1 = f"""<div class="md:p-7 p-4">
<h2 class=" text-xl text-center text-primary-green-dark capitalize">Add some data from scheduler</h2>
</div>"""
card2 = f"""<div class="md:p-7 p-4">
<h2 class="text-xl text-center text-primary-blue-dark capitalize">Today</h2>
<h3 class="text-sm text-primary-blue-dark text-center">{upcoming_count + completed_count} doses</h3>
</div>"""
return render_template('app/dashboard.html', name=user_name(session['user']).title(), card1=card1,
card2=card2, upcoming_count=upcoming_count, today_card_html=today_card_html,
tomorrow_card_html=tomorrow_card_html,
day_after_card_html=day_after_card_html)
elif not upcoming:
card2 = f"""<div class="md:p-7 p-4">
<h2 class="text-xl text-center text-primary-blue-dark capitalize">Today</h2>
<h3 class="text-sm text-primary-blue-dark text-center">{upcoming_count + completed_count} doses</h3>
</div>"""
return render_template('app/dashboard.html', name=user_name(session['user']).title(),
card2=card2, upcoming_count=upcoming_count,
today_card_html=today_card_html,
tomorrow_card_html=tomorrow_card_html,
day_after_card_html=day_after_card_html)
else:
return render_template('app/dashboard.html', name=user_name(session['user']).title(), card1=card1,
card2=card2,
card3=card3, upcoming_count=upcoming_count, today_card_html=today_card_html,
tomorrow_card_html=tomorrow_card_html,
day_after_card_html=day_after_card_html)
else:
card1 = f"""<div class="md:p-7 p-4">
<h2 class=" text-xl text-center text-primary-green-dark capitalize">Add some data from scheduler</h2>
</div>"""
return render_template('app/dashboard.html', name=user_name(session['user']).title(), card1=card1,
upcoming_count=0)
else:
return redirect('/login')
except KeyError:
return redirect('/login')
@app.route('/schedule')
def schedule():
try:
if session['user']:
data = fetch_user_schedule(datetime.now(), session['user'])
now = str(datetime.now()).split('.')[0][-8:-3]
data['current_time'] = now
upcoming = {}
completed = {}
data = dict(sorted(data.items(), key=lambda item: item[1]))
checkpoint = False
for medicine in data:
if medicine == 'current_time':
checkpoint = True
elif checkpoint:
upcoming[medicine] = data[medicine]
else:
completed[medicine] = data[medicine]
upcoming_count = 0
upcoming_card_html = ""
for medicine in upcoming:
upcoming_count += 1
upcoming_card_html += card(medicine.title(), upcoming[medicine])
completed_count = 0
completed_card_html = ""
for medicine in completed:
completed_count += 1
completed_card_html += card(medicine.title(), completed[medicine])
session['upcoming_count'] = upcoming_count
return render_template('app/schedule.html', completed_card_html=completed_card_html,
upcoming_card_html=upcoming_card_html, upcoming_count=upcoming_count,
completed_count=completed_count)
else:
return redirect('/login')
except KeyError:
return redirect('/login')
@app.route('/schedule/add', methods=["GET", "POST"])
def add_schedule():
if session['user']:
try:
upcoming_count = session['upcoming_count']
return render_template('app/add-medicine.html', upcoming_count=upcoming_count)
except KeyError:
return render_template('app/add-medicine.html')
else:
return redirect('/login')
@app.route('/schedule/submit', methods=['GET', "POST"])
def submit_schedule():
data = request.form
data = data.copy()
print(data)
dose_time = datetime.strptime(f"{data['hours']}:{data['minutes']} {data['halftime']}", '%I:%M %p')
schedule_data = {
'medicine_name': data['medicine-name'],
'dose_time': dose_time.strftime("%H:%M:%S"),
'start_date': datetime.strptime(data['start-date'].replace('-', ''), '%Y%m%d'),
'end_date': datetime.strptime(data['end-date'].replace('-', ''), '%Y%m%d')
}
if schedule_data['start_date'] < schedule_data['end_date']:
data['medicine-name'] = None
data['hours'] = None
data['minutes'] = None
data['halftime'] = None
data['start-date'] = None
data['end-date'] = None
days = []
for key in data:
if data[key]:
days.append(key)
schedule_data['notification'] = days
add_medicine(session['user'], schedule_data)
return redirect('/schedule')
else:
flash("Start date can't be before end date")
return redirect('/schedule/add')
@app.route('/prescription')
def prescription_view():
if session['user']:
data, count = fetch_prescriptions(session['user'])
prescription_card_html = ''
for index in range(len(data)):
prescription_card_html += prescription_card(data[index]['name'], data[index]['link'])
try:
upcoming_count = session['upcoming_count']
return render_template('app/prescriptions.html', upcoming_count=upcoming_count, total=count,
prescription_card_html=prescription_card_html)
except KeyError:
return render_template('app/prescriptions.html', total=count, prescription_card_html=prescription_card_html)
else:
return redirect('/login')
@app.route('/prescription/add', methods=["GET", "POST",'PUT'])
def add_prescription_page():
if request.method == 'POST':
try:
data = request.form
prescription_title = data.get('prescription-name')
file = request.files['file']
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
path = f"{app.config['UPLOAD_FOLDER']}/{filename}"
session['filename'] = path
link = add_prescription(path, session['user'], filename)
add_prescription_record(session['user'], prescription_title.title(), link)
return redirect('/prescription')
except:
try:
os.remove(session['filename'])
except KeyError:
pass
return redirect('/prescription/add')
try:
upcoming_count = session['upcoming_count']
return render_template('app/add-prescription.html', upcoming_count=upcoming_count)
except KeyError:
return render_template('app/add-prescription.html')
@app.route('/medicines')
def medicines():
try:
if session['user']:
medicines = get_all_medicines(session['user'])
medicine_card_html = ''
total = 0
for medicine in medicines:
results = get_medicines(medicine)
count = 0
if results:
for index in range(len(results['names'])):
count += 1
if count < 3:
total += 1
try:
medicine_card_html += medicine_card(results['names'][index], results['prices'][index],
results['order links'][index])
except IndexError:
pass
try:
upcoming_count = session['upcoming_count']
return render_template('app/medicines.html', medicine_card_html=medicine_card_html, total=total,
upcoming_count=upcoming_count)
except:
return render_template('app/medicines.html', medicine_card_html=medicine_card_html, total=total)
else:
return redirect('/login')
except KeyError:
return redirect('/login')
@app.errorhandler(404)
def error(error):
return '<h1>Error 404</h1>'
@app.errorhandler(500)
def error(error):
return '<h1>Error 500</h1>'
@app.errorhandler(502)
def error(error):
return '<h1>error502</h1>'
if __name__ == '__main__':
app.run(debug=True)