-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.py
More file actions
335 lines (274 loc) · 10.3 KB
/
app.py
File metadata and controls
335 lines (274 loc) · 10.3 KB
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
from flask import Flask, render_template, request,jsonify, redirect, url_for, send_file
import sqlite3
import qrcode
import io
import base64
import secrets
from datetime import datetime
# import requests
from flask import Flask, render_template
# from twilio.rest import Client
import re
# from dotenv import load_dotenv
import os
# load_dotenv()
# TWILIO_ACCOUNT_SID = os.getenv("TWILIO_ACCOUNT_SID")
# TWILIO_AUTH_TOKEN = os.getenv("TWILIO_AUTH_TOKEN")
# TWILIO_PHONE_NUMBER = os.getenv("TWILIO_PHONE_NUMBER")
# from flask import Flask, render_template, request, jsonify
# import sqlite3
# import qrcode
# import io
# import base64
# import secrets
# from flask_pwa import PWA
app = Flask(__name__)
app.config['PWA_APP_NAME'] = "Emergency QR"
app.config['PWA_APP_DESCRIPTION'] = "Offline support for emergency QR code details"
app.config['PWA_APP_THEME_COLOR'] = "#ffffff"
app.config['PWA_APP_BACKGROUND_COLOR'] = "#000000"
app.config['PWA_APP_DISPLAY'] = "standalone"
app.config['PWA_APP_SCOPE'] = "/"
app.config['PWA_APP_START_URL'] = "/"
#pwa = PWA(app)
def init_db():
"""Initialize the SQLite database and create tables if they don't exist"""
conn = sqlite3.connect('profiles.db')
c = conn.cursor()
c.execute('''
CREATE TABLE IF NOT EXISTS profiles (
id TEXT PRIMARY KEY,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
name TEXT NOT NULL,
blood_group TEXT,
template TEXT,
phone TEXT,
password TEXT,
emergency_contact TEXT,
medical_conditions TEXT,
allergies TEXT,
medications TEXT
)
''')
conn.commit()
conn.close()
def get_db():
"""Get database connection"""
conn = sqlite3.connect('profiles.db')
conn.row_factory = sqlite3.Row # This enables name-based access to columns
return conn
# Initialize the database when the app is created
with app.app_context():
init_db()
@app.route('/')
def index():
return render_template('index.html')
@app.route('/service-worker.js')
def service_worker():
return app.send_static_file('service-worker.js')
@app.route('/select_template/<template_name>')
def select_template(template_name):
return render_template('profile_form.html', template=template_name)
@app.route('/generate_profile', methods=['POST'])
def generate_profile():
# Generate a secure random ID
profile_id = secrets.token_hex(16)
conn = get_db()
try:
conn.execute('''
INSERT INTO profiles (
id, name, phone, blood_group, template, password,
emergency_contact, medical_conditions, allergies, medications
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
''', (
profile_id,
request.form['name'],
request.form['phone'],
request.form['blood_group'],
request.form['template'],
request.form['password'],
request.form.get('emergency_contact'),
request.form.get('medical_conditions'),
request.form.get('allergies'),
request.form.get('medications')
))
conn.commit()
# Generate QR code with just the profile ID
profile_url = url_for('view_profile',
profile_id=profile_id,
_external=True)
# Create QR code in memory
qr = qrcode.QRCode(version=1, box_size=10, border=5)
qr.add_data(profile_url)
qr.make(fit=True)
qr_image = qr.make_image(fill_color="black", back_color="white")
# Convert QR code to base64 for displaying in HTML
buffered = io.BytesIO()
qr_image.save(buffered, format="PNG")
qr_base64 = base64.b64encode(buffered.getvalue()).decode()
return render_template('qr_display.html',
qr_base64=qr_base64,
profile_id=profile_id)
except Exception as e:
print(f"Error: {e}")
return "Error creating profile", 500
finally:
conn.close()
@app.route('/profile/<profile_id>')
def view_profile(profile_id):
conn = get_db()
try:
profile = conn.execute(
'SELECT * FROM profiles WHERE id = ?',
(profile_id,)
).fetchone()
if profile is None:
return "Profile not found", 404
# Check for password if provided
provided_password = request.args.get('password')
show_sensitive = provided_password and provided_password == profile['password']
return render_template('profile_view.html',
profile=dict(profile), # Convert Row to dict
show_sensitive=show_sensitive)
finally:
conn.close()
@app.route('/download_qr/<profile_id>')
def download_qr(profile_id):
conn = get_db()
try:
# Verify profile exists
profile = conn.execute(
'SELECT id FROM profiles WHERE id = ?',
(profile_id,)
).fetchone()
if profile is None:
return "Profile not found", 404
# Generate QR code
profile_url = url_for('view_profile',
profile_id=profile_id,
_external=True)
qr = qrcode.QRCode(version=1, box_size=10, border=5)
qr.add_data(profile_url)
qr.make(fit=True)
qr_image = qr.make_image(fill_color="black", back_color="white")
# Save to BytesIO object
img_io = io.BytesIO()
qr_image.save(img_io, 'PNG')
img_io.seek(0)
return send_file(
img_io,
mimetype='image/png',
as_attachment=True,
download_name=f"qr_code_{profile_id}.png"
)
except Exception as e:
print(f"Download error: {e}")
return "Error generating QR code", 500
finally:
conn.close()
# Add these new routes to your app.py
@app.route('/scanner')
def scanner():
"""Route to show QR scanner page"""
return render_template('scanner.html')
@app.route('/edit_profile/<profile_id>', methods=['GET', 'POST'])
def edit_profile(profile_id):
conn = get_db()
try:
if request.method == 'POST':
# Verify password first
profile = conn.execute(
'SELECT password FROM profiles WHERE id = ?',
(profile_id,)
).fetchone()
if not profile or profile['password'] != request.form.get('password'):
return "Invalid password", 403
# Update profile
conn.execute('''
UPDATE profiles
SET name = ?,
phone = ?,
blood_group = ?,
emergency_contact = ?,
medical_conditions = ?,
allergies = ?,
medications = ?
WHERE id = ?
''', (
request.form['name'],
request.form['phone'],
request.form['blood_group'],
request.form.get('emergency_contact'),
request.form.get('medical_conditions'),
request.form.get('allergies'),
request.form.get('medications'),
profile_id
))
conn.commit()
return redirect(url_for('view_profile', profile_id=profile_id))
# GET request - show edit form
profile = conn.execute(
'SELECT * FROM profiles WHERE id = ?',
(profile_id,)
).fetchone()
if profile is None:
return "Profile not found", 404
return render_template('edit_profile.html', profile=dict(profile))
finally:
conn.close()
@app.route('/verify_password/<profile_id>', methods=['POST'])
def verify_password(profile_id):
"""API endpoint to verify password"""
conn = get_db()
try:
profile = conn.execute(
'SELECT password FROM profiles WHERE id = ?',
(profile_id,)
).fetchone()
if not profile:
return {"valid": False}, 404
provided_password = request.json.get('password')
is_valid = profile['password'] == provided_password
return {"valid": is_valid}
finally:
conn.close()
# client = Client(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)
def is_valid_phone_number(phone_number):
"""Validate phone number format (E.164)"""
pattern = r"^\+\d{10,15}$"
return bool(re.match(pattern, phone_number))
# def send_sms(emergency_contact, location_link):
# """Send emergency SMS with live location"""
# try:
# # Validate phone number format
# if not is_valid_phone_number(emergency_contact):
# print(f"❌ Invalid Phone Number: {emergency_contact}")
# return 400 # Bad request
# message = client.messages.create(
# body=f"🚨 Emergency Alert! Live Location: {location_link}",
# from_=TWILIO_PHONE_NUMBER,
# to=emergency_contact
# )
# print(f"✅ SMS Sent! Message SID: {message.sid}")
# return 200 # Success
# except Exception as e:
# print(f"❌ Error sending SMS: {e}")
# return 500 # Failure
@app.route('/send_emergency', methods=['POST'])
def send_emergency():
data = request.json
latitude = data.get("latitude")
longitude = data.get("longitude")
emergency_contact = data.get("emergency_contact")
if not latitude or not longitude or not emergency_contact:
return jsonify({"error": "Missing data"}), 400
# Ensure phone number is valid
if not is_valid_phone_number(emergency_contact):
return jsonify({"error": "Invalid phone number format"}), 400
# Generate Google Maps Link
location_link = f"https://www.google.com/maps?q={latitude},{longitude}"
# Just simulate success without sending SMS
print(f"🚨 Simulated emergency message to {emergency_contact} with location: {location_link}")
return jsonify({"message": "Simulated emergency message sent!"}), 200
if __name__ == '__main__':
app.run(debug=True)