-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
180 lines (114 loc) · 4.4 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
from flask import Flask, request, redirect, url_for, render_template
from cryptography.fernet import Fernet
import flask_login
from airtable import Airtable
from dotenv import load_dotenv
import os
import urllib
from urllib.request import urlopen, Request
import ast
import json
from twilio.rest import Client
from datetime import datetime
load_dotenv()
app = Flask(__name__)
app.secret_key = os.getenv("KEY") # Change this!
login_manager = flask_login.LoginManager()
login_manager.init_app(app)
f = Fernet(os.getenv("KEY"))
ACCOUNT_SID = os.getenv('TWILIO_ACCOUNT_SID')
AUTH_TOKEN = os.getenv('TWILIO_AUTH_TOKEN')
NOTIFY_SERVICE_SID = os.getenv('TWILIO_NOTIFY_SERVICE_SID')
client = Client(ACCOUNT_SID, AUTH_TOKEN)
users = {}
table = Airtable(os.getenv("AIRTABLE_BASE"),
'Admins', os.getenv("AIRTABLE_KEY"))
users_table = Airtable(os.getenv("AIRTABLE_BASE"),
'Users', os.getenv("AIRTABLE_KEY"))
class User(flask_login.UserMixin):
pass
@login_manager.user_loader
def user_loader(email):
if email not in users:
return
user = User()
user.id = email
return user
@login_manager.request_loader
def request_loader(request):
email = request.form.get('email')
if email not in users:
return
user = User()
user.id = email
user.is_authenticated = request.form['password'] == users[email]['password']
return user
@app.route('/login', methods=['GET', 'POST'])
def login():
airtableUsers = table.get_all(view='Grid view')
for i in airtableUsers:
users[i['fields']['Username']] = {'password': i['fields']['Password']}
if request.method == 'GET':
return render_template('login.html')
email = request.form['email']
if email not in users:
return 'Bad login'
elif request.form['password'] == f.decrypt(users[email]['password'].encode()).decode():
user = User()
user.id = email
flask_login.login_user(user)
return redirect(url_for('home'))
return 'Bad login'
@app.route('/logout')
def logout():
flask_login.logout_user()
return render_template('logout.html')
@app.route('/getContacts', methods=['GET', 'POST'])
@flask_login.login_required
def getContacts():
if request.method == 'GET':
return render_template('find_contacts.html')
list1 = users_table.get_all(
filterByFormula='IF(FIND("' + request.form['idNo'] + '",Contacts)>0,TRUE(),FALSE())')
list2 = users_table.get_all(
filterByFormula='IF(FIND("' + request.form['idNo'] + '",{ID Number})>0,TRUE(),FALSE())')
if 'Other Contacts' in list2[0]['fields']:
for i in list2[0]['fields']['Other Contacts']:
list1.extend(ast.literal_eval('[{"fields":'+i+'}]'))
res = []
for i in list1:
if ", 'ID Number': '" + i["fields"]['ID Number'] not in str(res):
res.append(i)
print(str(res))
return render_template('contacts.html', contacts=res, id=request.form['idNo'])
@app.route('/issueqo', methods=['POST', 'GET'])
@flask_login.login_required
def issueqo():
print(request.args.get('numbers').replace(",]", "]"))
numbers = ast.literal_eval(request.args.get('numbers').replace(",]", "]"))
bindings = list(map(lambda number: json.dumps(
{'binding_type': 'sms', 'address': number}), numbers))
print("=====> To Bindings :>", bindings, "<: =====")
notification = client.notify.services(NOTIFY_SERVICE_SID).notifications.create(
to_binding=bindings,
body="test"
)
print(notification.body)
for i in ast.literal_eval(request.args.get('contacts')):
users_table.update_by_field('ID Number', i["fields"]["ID Number"], {
'Quarantine Starting Date': datetime.today().strftime('%Y-%m-%d')})
return render_template('ordersplaced.html', contacts=ast.literal_eval(request.args.get('contacts')))
@app.route('/', methods=['GET'])
@flask_login.login_required
def home():
return render_template('home.html')
@app.route('/quarantined', methods=['GET'])
@flask_login.login_required
def quarantined():
return render_template('quarantined.html', people=users_table.search('Quarantined?', '1'))
@login_manager.unauthorized_handler
def unauthorized_handler():
return render_template('not_logged_in.html')
if __name__ == '__main__':
from os import environ
app.run(debug=False, host='0.0.0.0', port=environ.get("PORT", 5000))