forked from vsaase/covid19selfreport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
203 lines (167 loc) · 6.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
import sys
import random
from flask import Flask, render_template, jsonify, redirect
from flask_sqlalchemy import SQLAlchemy
from flask import request, flash
from flask_bootstrap import Bootstrap
from models import QuizForm, DeleteForm
import json
from copy import deepcopy
import firebase_admin
import urllib
import datetime
from firebase_admin import credentials, firestore
from itsdangerous import URLSafeTimedSerializer, Signer
from plz.plz2kreis import plz2kreis, plz2longlat
testing_mode = True
if testing_mode:
from config_test import Config
cred = credentials.Certificate("covid19test-218a3-firebase-adminsdk-o6s3e-40e98ea53d.json")
else:
from config import Config
cred = credentials.Certificate("covid19-selfreport-firebase-adminsdk-jfup1-8a45aedc76.json")
app = Flask(__name__)
app.config.from_object(Config)
Bootstrap(app)
firebase_admin.initialize_app(cred)
db = firestore.client()
report_ref = db.collection("Report")
rki_ref = db.collection("RKI_Laender")
landkreise_ref = db.collection("Landkreise")
BASECOORDS = [51.3150172,9.3205287]
def generate_confirmation_token(signature):
serializer = URLSafeTimedSerializer(app.config['SECRET_KEY'])
return serializer.dumps(signature, salt=app.config['SECURITY_PASSWORD_SALT'])
def confirm_token(token, expiration=3600):
serializer = URLSafeTimedSerializer(app.config['SECRET_KEY'])
try:
signature = serializer.loads(
token,
salt=app.config['SECURITY_PASSWORD_SALT'],
max_age=expiration
)
except:
return False
return signature
def createreportdict(form):
dct = {}
dct["plz"] = form.plz.data
crd = json.loads(form.geolocation.data)
dct["latitude"] = crd["latitude"]
dct["longitude"] = crd["longitude"]
dct["accuracy"] = crd["accuracy"]
if dct["latitude"] == 0:
dct["longitude"], dct["latitude"] = plz2longlat(dct["plz"])
dct["latitude_rand"] = dct["latitude"] + 0.01 * random.random()
dct["longitude_rand"] = dct["longitude"] + 0.01 * random.random()
dct["test"] = form.test.data
dct["arzt"] = form.arzt.data
dct["quarantine"] = form.quarantine.data
dct["datetest"] = None if form.datetest.data is None else form.datetest.data.strftime("%d.%m.%Y")
dct["notherstest"] = form.notherstest.data
dct["symptoms"] = ', '.join(form.symptoms.data)
dct["dayssymptoms"] = form.dayssymptoms.data
dct["notherssymptoms"] = form.notherssymptoms.data
dct["age"] = form.age.data
dct["sex"] = form.sex.data
dct["plz"] = form.plz.data
try:
dct["kreis"] = plz2kreis[dct["plz"]]
except:
dct["kreis"] = ""
print(f"error converting PLZ {dct['plz']} to Kreisebene")
dct["email_addr"] = form.email_addr.data
s = Signer(form.password.data)
dct["signature"] = str(s.sign(form.email_addr.data))
dct["timestamp"] = firestore.SERVER_TIMESTAMP
dct["email_confirmed"] = False
dct["overwritten"] = False
dct["source"] = "report"
dct["token"] = str(generate_confirmation_token(dct["signature"]))
return dct
@app.route('/landkreis/<name>', methods=['GET', 'POST'])
def landkreis(name):
return render_template('landkreis.html', name=name)
@app.route('/report', methods=['GET', 'POST'])
def report():
form = QuizForm(request.form)
if not form.validate_on_submit():
return render_template('report.html', form=form)
if request.method == 'POST':
dct = createreportdict(form)
report_ref.document(dct["token"]).set(dct)
oldreports = report_ref.where("signature", '==', dct["signature"]).stream()
for oldreport in oldreports:
oldreport = oldreport.to_dict()
if oldreport["token"] != dct["token"]:
oldreport["overwritten"] = True
report_ref.document(oldreport["token"]).set(oldreport)
#return render_template('confirm_mail.html', mail=dct["email_addr"])
return render_template('success.html', mail=dct["email_addr"], risk="mittleres")
@app.route('/delete', methods=['GET', 'POST'])
def delete():
form = DeleteForm(request.form)
if not form.validate_on_submit():
return render_template('delete.html', form=form)
if request.method == 'POST':
s = Signer(form.password.data)
signature = str(s.sign(form.email_addr.data))
oldreports = report_ref.where("signature", '==', signature).stream()
ndel = 0
for oldreport in oldreports:
oldreport = oldreport.to_dict()
if oldreport["overwritten"] == False:
ndel += 1
oldreport["overwritten"] = True
oldreport["deleted"] = True
report_ref.document(oldreport["token"]).set(oldreport)
return render_template('delete_success.html', ndel=ndel)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/impressum')
def impressum():
return render_template('impressum.html')
@app.route('/info')
def info():
return render_template('info.html')
@app.route('/getreports')
def getreports():
reports = [doc.to_dict() for doc in report_ref.where("overwritten", '==', False).stream()]
data = [{
"latitude": report["latitude_rand"],
"longitude": report["longitude_rand"],
"symptoms": report["symptoms"],
"dayssymptoms": report["dayssymptoms"],
"test": report["test"],
"source": report["source"],
"nothers": report["notherssymptoms"],
"date": report["timestamp"].strftime("%d.%m.%Y")
} for report in reports]
return jsonify({"data": data})
@app.route('/getrki')
def getrki():
reports = [doc.to_dict() for doc in rki_ref.stream()]
data = [{
"latitude": report["latitude"],
"longitude": report["longitude"],
"test": report["test"],
"ncases": report["ncases"],
"source": report["source"],
"popup": report["popup"]
} for report in reports]
return jsonify({"data": data})
@app.route('/getlaender')
def getlaender():
reports = [doc.to_dict() for doc in landkreise_ref.where("overwritten", '==', False).stream()]
data = [{
"latitude": report["latitude"],
"longitude": report["longitude"],
"test": report["test"],
"ncases": report["ncases"],
"source": report["source"],
"popup": report["popup"]
} for report in reports]
return jsonify({"data": data})
if __name__ == '__main__':
app.run(debug=True)