forked from wuhan2020/volunteer-certificate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
256 lines (241 loc) · 9.43 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
import os
import io
import json
import logging
import utils
import re
from flask import Flask, request, render_template, send_file
from flask import Response
from tinydb import Query, TinyDB
import pic_email as wc
from model import update_status, update_name
from model import insert_people
from app_instance import app
from app_instance import logger
from jobs import SendEmailJob
from utils import get_smtp_url, response_json
send_email_job = SendEmailJob()
# 0:KEY is not right
# 1:KEY is right
# 2: Make Image& Send email sucess
# 3: Have been used
# 4:
# 5:Send wrong
def return_msg(message):
if type(message) is dict:
message = json.dumps(message)
return message
def check_email(email):
regex = r'^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$'
if re.search(regex, email):
return True
return False
def confirm_token(token): #finish
if len(token) <= 0:
return False
db = TinyDB("data.json")
People = Query()
res = db.search(People.token == token)
db.close()
if len(res) != 0:
return res[0]
else:
return False
def confirm_admin_token(token):
orgconfig = utils.get_org_config()
admintoken = orgconfig["admin_token"]
if token == admintoken:
return True
else:
return False
def is_token_unused(token):#确定一下Key有没有被用过
db = TinyDB("data.json")
People = Query()
res = db.search(People.token == token)
db.close()
if res and res[0]["status"] <= 3: # 如果邮件发送失败,则都可以重试
return True
else:
return False
@app.route('/api/getUserInfo', methods=['get', 'OPTIONS'])
def token():
message = request.args.get('token')
person_info = confirm_token(message)
if person_info and person_info['status'] == 1:
return_json = {'code': 0, 'data': person_info,'message': 'success'}
elif person_info and person_info['status'] > 1:
return_json = {'code': 1, 'data':'','message': 'You have submitted your information successful, please check your email'}
else:
return_json = {'code': 1, 'data':'','message': 'User does not exist'}
response = Response()
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Headers'] = '*'
response.data = return_msg(return_json)
return response
@app.route('/api/submitUserInfo', methods=['POST', 'OPTIONS'])
def send_email():
response = Response()
response.headers['Content-Type'] = 'application/json'
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Headers'] = '*'
if request.method == 'OPTIONS':
return response
elif request.method == 'POST':
message = json.loads(request.get_data(as_text = True))
name = message.get('name')
token = message.get('token', '')
if name is None:
return response_json(code=1, message='Name is required')
result = confirm_token(token)
if result is False:
return response_json(code=2, message='Token is not available, plz check your latest email')
email = result['email']
if is_token_unused(token): # 先确定下是不是志愿者列表中的token 并且是否注册过 没问题的话开始做图片
update_status(email, 2)
update_name(email,name)
try:
wc.write_to_pic(name,email,token)
return_json = {'code': 0, 'message': 'You have submitted your information successful, the certificate is sent to you, please check your email', 'data': None}
response.data = return_msg(return_json)
return response
except Exception as e: # 发送邮件或者创建图片错误 可能是邮件有问题
logger.error(e)
return response_json(4, str(e))
else:
return response_json(3, 'You have submitted your information successful, please check your email')
@app.route('/api/uploadImage', methods=['POST', 'OPTIONS'])
def save_image():
if request.method == 'POST':
# get the token from the header
token = request.headers.get('token', '')
result = confirm_admin_token(token) # 没有每个人唯一的Key
response = Response()
response.headers['Content-Type'] = 'application/json'
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Headers'] = '*'
if request.method == 'OPTIONS':
return response
return_json = {'code': 1, 'message': '网络异常', 'data': None}
response.data = return_msg(return_json)
# get the image file path
image_file = request.files.get('template', False)
# Todo: security check of image_path
if result == False or image_file == False:
return response
# overwrite pic.jpg
basedir = os.path.dirname(__file__)
try:
image_file.save(os.path.join(basedir, 'pic.jpg'))
except Exception as e:
logger.info(e)
return_json = {'code': 0, 'message': 'upload successfully', 'data': None}
response.data = return_msg(return_json)
return response
@app.route('/api/addUserData', methods=['POST', 'OPTIONS'])
def add_data():
if request.method == 'POST':
message = json.loads(request.get_data(as_text=True))
email_list = message['email']
token = message['token']
result = confirm_admin_token(token) # 没有每个人唯一的Key
response = Response()
response.headers['Content-Type'] = 'application/json'
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Headers'] = '*'
if request.method == 'OPTIONS':
return response
return_json = {'code': 1, 'message': '网络异常', 'data': None}
response.data = return_msg(return_json)
if result == False:
return response
for email in email_list:
if email.find(',') > 0:
number = email.split(',')[1]
email = email.split(',')[0]
else:
number = ''
if check_email(email) == False:
logger.info('invalid email %s' % email)
continue
try:
insert_people(email, '', number=number)
except KeyError:
pass
except Exception as e:
logger.info(e)
return_json = {'code': 0, 'message': 'update emails successfully', 'data': None}
response.data = return_msg(return_json)
return response
@app.route('/api/updateOrgConfig', methods=['POST', 'OPTIONS'])
def update_config():
if request.method == 'POST':
message = json.loads(request.get_data(as_text=True))
token = message["token"]
result = confirm_admin_token(token) # 没有每个人唯一的Key
response = Response()
response.headers['Content-Type'] = 'application/json'
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Headers'] = '*'
if request.method == 'OPTIONS':
return response
return_json = {'code': 1, 'message': '网络异常', 'data': None}
response.data = return_msg(return_json)
if result == False:
return response
try:
referer = request.headers.get('Referer', None)
if referer is not None:
referer = referer.replace('admin', 'index')
message['frontend_url'] = referer
if message.get('username'): # email admin address
message['server_address'] = get_smtp_url(message.get('username')) # email smtp address
orgconfig = utils.get_org_config()
emailconfig = utils.get_email_config()
for domain in message:
if domain == "token" or len(str(message[domain])) == 0:
continue
if domain in orgconfig:
orgconfig[domain] = message[domain]
elif domain in emailconfig:
emailconfig[domain] = message[domain]
utils.update_org_config(orgconfig)
utils.update_email_config(emailconfig)
except Exception as e:
logger.error(e)
return_json = {'code': 0, 'message': 'update config successfully', 'data': None}
response.data = return_msg(return_json)
return response
@app.route('/api/email', methods=['POST', 'OPTIONS'])
def email_task():
if request.method == 'POST':
message = json.loads(request.get_data(as_text=True))
action = message.get('action', '')
token = message['token']
result = confirm_admin_token(token) # 没有每个人唯一的Key
response = Response()
response.headers['Content-Type'] = 'application/json'
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Headers'] = '*'
if request.method == 'OPTIONS':
return response
return_json = {'code': 1, 'message': '网络异常', 'data': None}
response.data = return_msg(return_json)
if result == False:
return response
if action == 'send':
send_email_job.start()
return_json = {'code': 0, 'message': 'start send email jobs successfully', 'data': None}
response.data = return_msg(return_json)
return response
if __name__ == '__main__':
logger.setLevel(logging.INFO)
if not os.path.isdir("images"):
os.mkdir("images")
host_name = '0.0.0.0'
if os.environ.get('host'):
host_name = os.environ['host']
if os.environ.get('port'):
port_name = int(os.environ['port'])
else:
port_name = 5000
app.run(host=host_name, port=port_name, debug=True)