-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.py
294 lines (248 loc) · 9.36 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from flask import Flask
from flask import flash
from flask import render_template
from flask import make_response
from flask import request
from flask import url_for
from flask import redirect
from flask import session
from functools import wraps
from setting import ALLOWED_EXTENSIONS
from setting import LEADER_SMS
from setting import FLASK_KEY
from setting import FLASK_SESSION
from setting import LOGIN_ID
from setting import LOGIN_PWD
from setting import LOGIN_CHOICE
from setting import STAFF_CNO
from setting import STAFF_SMS
from setting import QUEUE_NAME_SENDFIRST
from setting import QUEUE_NAME_LIST
from setting import QUEUE_NAME_SMSLEADER
from util import read_csv
import t
import sns
import sqs
import ujson as json
app = Flask(__name__)
app.session_cookie_name = FLASK_SESSION
app.secret_key = FLASK_KEY
app.config['ALLOWED_EXTENSIONS'] = ALLOWED_EXTENSIONS
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in app.config['ALLOWED_EXTENSIONS']
def login_required(f):
@wraps(f)
def cklogin(*args, **kwargs):
if session.get('user') == 1:
pass
else:
return redirect(url_for('login'))
return f(*args, **kwargs)
return cklogin
@app.route("/")
@login_required
def hello():
return make_response(render_template('base.htm'))
@app.route("/send_welcome", methods=['POST', 'GET'])
@login_required
def send_welcome():
title = u'Send Welcome'
if request.method == "POST":
t.template = t.env.get_template('./coscup_welcome.htm')
t.send_welcome(request.form.to_dict())
flash(u'已寄送歡迎信:{nickname} / {mail} / {leaderno}'.format(
**request.form.to_dict()))
return redirect(url_for('send_welcome'))
else:
return make_response(render_template('t_sendwelcome.htm',
title=title, send_welcome=1))
@app.route("/send_sms", methods=['POST', 'GET'])
@login_required
def send_sms():
title = u'Send SMS'
if request.method == "POST":
leaderno = request.form.getlist('leaderno')
body = request.form.get('msg')
if body:
msgs = []
if 'all' in leaderno:
for i in LEADER_SMS.values():
msgs.append({'to': i, 'body': body})
flash(u'{0} {1}'.format(i, body))
sqs.add(QUEUE_NAME_SMSLEADER, msgs)
else:
if leaderno:
for i in leaderno:
msgs.append({'to': LEADER_SMS[i], 'body': body})
flash(u'{0} {1}'.format(LEADER_SMS[i], body))
sqs.add(QUEUE_NAME_SMSLEADER, msgs)
else:
flash(u'沒有選擇組別!')
else:
flash(u'沒有內容!')
return redirect(url_for('send_sms'))
else:
return make_response(render_template('t_sendsms.htm', title=title,
send_sms=1))
@app.route("/send_sms_coll", methods=['POST', 'GET'])
@login_required
def send_sms_coll():
title = u'Send SMS by Coll'
if request.method == "POST":
cno = request.form.getlist('cno')
body = request.form.get('msg')
cno = STAFF_SMS if 'all' in cno else cno
if body:
for i in cno:
msgs = []
for u in STAFF_SMS[i]:
msgs.append({'to': u['phone'], 'body': body})
flash(u'{0} {1}'.format(u['phone'], body))
sqs.add(QUEUE_NAME_SMSLEADER, msgs)
else:
flash(u'沒有內容!')
return redirect(url_for('send_sms_coll'))
else:
coll = []
for i in STAFF_SMS:
coll.append({'CNO': i,
'CNO_NAME': STAFF_CNO[i], })
return make_response(render_template('t_sendsmscoll.htm', coll=coll,
title=title, send_sms_coll=1))
@app.route("/send_first", methods=['POST', 'GET'])
@login_required
def send_first():
title = u'Send First'
if request.method == "POST":
t.template = t.env.get_template('./coscup_first.htm')
t.send_first(request.form.to_dict())
flash(u'已寄送登錄信:{nickname} / {mail}'.format(**request.form.to_dict()))
return redirect(url_for('send_first'))
else:
return make_response(render_template('t_sendfirst.htm',
title=title, send_first=1))
@app.route("/send_all_first", methods=['POST', 'GET'])
@login_required
def send_all_first():
title = u'Send All People First'
if request.method == "POST":
f = request.files.get('file')
if f and allowed_file(f.filename):
if request.form.get('sendby') == 'sqs':
sqs.add(QUEUE_NAME_SENDFIRST, read_csv(f))
flash(u'丟到 AWS SQS')
elif request.form.get('sendby') == 'mail':
#t.template = t.env.get_template('./coscup_first.htm')
#t.sendall(read_csv(f), t.send_first)
flash(u'寄送大量登錄信')
else:
flash(u'錯誤選擇!')
return redirect(url_for('send_all_first'))
else:
return make_response(render_template('t_sendallfirst.htm',
title=title, send_all_first=1))
@app.route("/send_transportation_fare", methods=['POST', 'GET'])
@login_required
def send_transportation_fare():
title = u'Send Transportation Fare'
if request.method == "POST":
t.template = t.env.get_template('./coscup_transportation_fare.htm')
t.send_transportation_fare(request.form.to_dict())
flash(u'已交通費確認信:{nickname} / {mail}'.format(**request.form.to_dict()))
return redirect(url_for('send_transportation_fare'))
else:
return make_response(render_template('t_sendtransportation_fare.htm',
title=title,
send_transportation_fare=1))
@app.route("/awssqs", methods=['POST', 'GET'])
@login_required
def awssqs():
title = u'AWS SQS'
if request.method == "POST":
f = request.files.get('file')
if f and allowed_file(f.filename):
sendby = request.form.get('sendby')
if sendby:
sqs.add(sendby, read_csv(f))
flash(u'丟到 AWS SQS {0}'.format(sendby))
else:
flash(u'錯誤選擇!')
return redirect(url_for('awssqs'))
else:
return make_response(render_template('t_awssqs.htm', title=title,
qlist=QUEUE_NAME_LIST, awssqs=1))
@app.route("/awssns", methods=['POST', 'GET'])
@login_required
def awssns():
title = u'AWS SNS'
if request.method == "POST":
sendby = request.form.get('sendby')
if sendby:
sns.publish(sendby, 'COSCUPSNS')
flash(u'啟動 AWS SQS {0}'.format(sendby))
else:
flash(u'錯誤選擇!')
return redirect(url_for('awssns'))
else:
return make_response(render_template('t_awssns.htm', title=title,
qlist=sqs.AWSSQSLIST, awssns=1))
@app.route("/send_weekly", methods=['POST', 'GET'])
@login_required
def send_weekly():
title = u'Send Weekly'
if request.method == "POST":
f = request.files.get('file')
if f and allowed_file(f.filename):
no = int(request.form.get('no'))
mail = request.form.get('mail')
t.send_weekly(no, f.stream.read(), mail)
flash(u'已寄送週報:#{0:02} / {1}'.format(no, mail))
return redirect(url_for('send_weekly'))
else:
flash(u'沒有檔案!')
return redirect(url_for('send_weekly'))
else:
return make_response(render_template('t_sendweekly.htm', title=title,
send_weekly=1))
@app.route("/api", methods=['POST', ])
def api():
if request.method == "POST":
## For getting AWS SNS comfirm msgs.
## print request.headers, request.data
sqsmessage = json.loads(request.data).get('Message')
getattr(sqs, sqsmessage)() if hasattr(sqs, sqsmessage) else None
return ''
@app.route("/aws", methods=['POST', ])
def aws():
if request.method == "POST":
## For getting AWS SNS comfirm msgs.
print request.headers, request.data
return ''
@app.route("/login", methods=['POST', 'GET'])
def login():
title = u'Login'
if request.method == "POST":
u = request.form.get('user')
pwd = request.form.get('pwd')
pwdw = request.form.get('pwdw')
if u == LOGIN_ID and pwd == LOGIN_PWD and pwdw == LOGIN_CHOICE:
session['user'] = 1
#return u'IN POST {0}'.format(request.form)
return redirect(url_for('hello'))
else:
return redirect(url_for('logout'))
else:
if session.get('user'):
return redirect(url_for('hello'))
else:
return make_response(render_template('t_login.htm', title=title))
@app.route("/logout")
def logout():
session.pop('user', None)
return redirect(url_for('hello'))
if __name__ == "__main__":
#app.run(host='127.0.0.1',debug=True)
app.run(host='0.0.0.0', port=6666, debug=True)