-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
294 lines (234 loc) · 10.6 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
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
from flask import Flask, request, render_template
from flask_cors import CORS
import firebase_admin
from firebase_admin import firestore
import qrcode
import dateutil
from dateutil import parser
from datetime import datetime
import smtplib
import ssl
cred_obj = firebase_admin.credentials.Certificate(
'reformbar-a4b02-firebase-adminsdk-y0re0-40695dbdf0.json')
default_app = firebase_admin.initialize_app(cred_obj)
app = Flask(__name__, static_url_path='/static')
CORS(app)
@app.route("/makeqrcodeandsetupfirebase", methods=["POST"])
def make_database_from_info_and_return_the_qrcode():
todays_date = datetime.today().strftime('%Y-%m-%d')
dob, email, gender, height, id, id_official, name, payment, photoURL, weight = request_fields_from_form()
db = firestore.client()
doc_ref = db.collection(u'customers').document(id)
data = {
u'name': name,
u'dob': dob,
u'gender': gender,
u'payment': payment,
u'height': height,
u'weight': weight,
u'drinks': "0",
u'id_official': id_official,
u'alcohol': "0",
u'photoURL': photoURL,
u'nuisance': "0",
u'email': email,
u'drinking_attempts': "0",
u'date_updated': todays_date,
}
doc_ref.set(data)
make_qr_codes(id)
print("Made qr code")
return "Made Image"
def request_fields_from_form():
name = request.form["name"]
dob = request.form["dob"]
gender = request.form["gender"]
payment = request.form["payment"]
height = request.form["height"]
weight = request.form["weight"]
id = request.form["id"]
id_official = request.form["id_official"]
photoURL = request.form["photoURL"]
email = request.form["email"]
return dob, email, gender, height, id, id_official, name, payment, photoURL, weight
def make_qr_codes(id):
import os
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_M,
box_size=10,
border=4,
)
qr.add_data(id)
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")
path_to_save_image = os.path.join(
os.getcwd(), "static", "id_images", f'{id}.png')
img.save(path_to_save_image)
@app.route('/')
def serve_login():
return render_template("main_page.html")
@app.route('/register')
def serve_register():
return render_template("index.html")
@app.route('/bar')
def serve_bar():
return render_template("menu.html")
@app.route('/drink/<variable>', methods=['GET'])
def serve_drinks(variable):
data = {"drink_id": variable, "abc": "abc"}
return render_template("qr_code.html", data=data, drink_name=variable)
@app.route('/profile/<id>', methods=['GET'])
def serve_profile(id):
existing_data = get_dict_for_document_and_collection(id, 'customers')
return render_template("profile.html", data=existing_data)
@app.route('/details')
def serve_details():
return render_template("details.html")
@app.route('/management')
def serve_management():
db = firestore.client()
doc_ref = db.collection(u'customers').stream()
all_users = []
for users in doc_ref:
all_users.append(users.to_dict())
return render_template("management.html", users=all_users)
@app.route('/add_drink', methods=['POST'])
def add_drink():
todays_date = get_todays_date()
password = "36fb75181c26195f01aff5144aa1464b"
name = request.form["drink"]
id = request.form["id"]
db = firestore.client()
accepted_nuicances, bac_accepted, days_after_database_refreshes, drinking_attemps_before_email, legal_drinking_age = make_limit_constants()
doc_ref, drink_data, existing_data = make_existing_data_dicts(
db, id, name)
date = parser.parse(existing_data["dob"])
date_updated = parser.parse(existing_data["date_updated"])
now = get_now_time()
number_of_days = get_days(date_updated, now)
age = get_age_difference(date, now)
bac = calculate_bac(float(existing_data["drinks"]), float(
existing_data["weight"]), existing_data["gender"], alcohol_consumed=float(existing_data["alcohol"])+float(drink_data["alcohol"]))
if number_of_days > days_after_database_refreshes:
doc_ref, drink_data, existing_data = flush_database(db, doc_ref, drink_data, existing_data, id, name,
todays_date)
if request.form["password"] == password and float(existing_data["nuisance"]) < accepted_nuicances and bac < bac_accepted and float(existing_data["payment"]) > float(drink_data["price"]) and age > legal_drinking_age:
send_successful_order(doc_ref, drink_data, existing_data)
return "Successful Order Placed"
else:
return_string = handle_unsuccessful_order(accepted_nuicances, age, bac, bac_accepted, doc_ref, drink_data,
drinking_attemps_before_email, existing_data, legal_drinking_age,
password)
return return_string
def handle_unsuccessful_order(accepted_nuicances, age, bac, bac_accepted, doc_ref, drink_data,
drinking_attemps_before_email, existing_data, legal_drinking_age, password):
update_drinking_attempts(doc_ref, existing_data)
return_string = "Unsuccessful Order"
if float(existing_data["drinking_attempts"]) > drinking_attemps_before_email:
message = '''Good evening
We have observed that you have been going out to the bar very often.
It should be noted that, while drinking in control is acceptable, alcohol intake can lead to a plethora of health issues, including liver failure, hypertension and anxiety.
We highly recommend you visit a counselor or doctor in order to escape this addiction that may be fueling you, both for the sake of your physical and mental well-being. We can assist in reaching out to an anti-addiction counselor who would walk you through transforming your drinking habits for a better future for yourself
If you would like assistance, kindly send a mail to us and we will inform the counselor of the same.
Thank you Sir/Ma'am
'''
SUBJECT = "High Number Of Drinking Attempts"
send_email(existing_data["email"], message, SUBJECT)
return_string = set_return_string(accepted_nuicances, age, bac, bac_accepted, drink_data, existing_data,
legal_drinking_age, password, return_string)
return return_string
def set_return_string(accepted_nuicances, age, bac, bac_accepted, drink_data, existing_data, legal_drinking_age,
password, return_string):
if bac > bac_accepted:
return_string = "Unsuccessful order: BAC levels too high"
elif float(existing_data["payment"]) < float(drink_data["price"]):
return_string = "Unsuccessful order: Not enough money"
elif age < legal_drinking_age:
return_string = "Unsuccessful order: Under the legal age of alcohol consumption"
elif float(existing_data["nuisance"]) >= accepted_nuicances:
return_string = "Unsuccessful order: Misbehaviour"
elif request.form["password"] != password:
return_string = "Unsuccessful order: Wrong Password"
return return_string
def flush_database(db, doc_ref, drink_data, existing_data, id, name, todays_date):
data = {
u'drinks': str(0),
u'drinking_attempts': str(0),
u'alcohol': str(0),
u'date_updated': todays_date,
}
doc_ref.set(data, merge=True)
doc_ref, drink_data, existing_data = make_existing_data_dicts(db, id, name)
return doc_ref, drink_data, existing_data
def get_days(date_updated, now):
number_of_days = dateutil.relativedelta.relativedelta(now, date_updated)
number_of_days = number_of_days.days
return number_of_days
def get_age_difference(date, now):
age = dateutil.relativedelta.relativedelta(now, date)
age = age.years
return age
def get_now_time():
now = datetime.utcnow()
now = now.date()
return now
def make_limit_constants():
days_after_database_refreshes = 4
drinking_attemps_before_email = 4
legal_drinking_age = 18
bac_accepted = 0.07
accepted_nuicances = 1
return accepted_nuicances, bac_accepted, days_after_database_refreshes, drinking_attemps_before_email, legal_drinking_age
def get_todays_date():
from datetime import datetime
todays_date = datetime.today().strftime('%Y-%m-%d')
return todays_date
def make_existing_data_dicts(db, id, name):
doc_ref = db.collection(u'customers').document(id)
return doc_ref, get_dict_for_document_and_collection(name, 'drink'), get_dict_for_document_and_collection(id, 'customers')
def get_dict_for_document_and_collection(document, collection):
db = firestore.client()
doc_ref = db.collection(collection).document(document)
doc = doc_ref.get()
existing_data = doc.to_dict()
return existing_data
def send_successful_order(doc_ref, drink_data, existing_data):
balance = str(float(existing_data["payment"]) - float(drink_data["price"]))
number_of_drinks = str(float(existing_data["drinks"]) + 1)
data = {
u'drinks': number_of_drinks,
u'drinking_attempts': str(float(existing_data["drinking_attempts"]) + 1),
u'alcohol': str(float(existing_data["alcohol"]) + float(drink_data["alcohol"])),
u'payment': balance,
}
doc_ref.set(data, merge=True)
message = f"Good Evening. \nYour order for your drink has been placed. Current Balance: {balance}\nCurrent Number of Drinks: {number_of_drinks}"
SUBJECT = "Order Successfully Placed"
send_email(existing_data["email"], message, SUBJECT)
def update_drinking_attempts(doc_ref, existing_data):
data = {
u'drinking_attempts': str(float(existing_data["drinking_attempts"]) + 1),
}
doc_ref.set(data, merge=True)
def send_email(email, message, SUBJECT):
port = 587 # For starttls
smtp_server = "smtp.gmail.com"
sender_email = '[email protected]'
receiver_email = email
password = "vigneshisbae123"
message = message
message = 'Subject: {}\n\n{}'.format(SUBJECT, message)
context = ssl.create_default_context()
with smtplib.SMTP(smtp_server, port) as server:
server.ehlo() # Can be omitted
server.starttls(context=context)
server.ehlo() # Can be omitted
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, message)
def calculate_bac(no_of_drinks, body_weight_in_kg, gender, r=0.55, alcohol_consumed=14.0):
if gender == 'Male':
r = 0.68
return no_of_drinks * alcohol_consumed * 100 / (body_weight_in_kg * r * 1000)
if __name__ == "__main__":
app.run(debug=True)