10
10
from module .sender import SenderCampaign , SenderLogs , SenderReceiver
11
11
from module .team import Team
12
12
from module .users import User
13
+ from structs .teams import TeamUsers
13
14
from view .utils import check_the_team_and_project_are_existed
14
15
15
16
VIEW_SENDER = Blueprint ('sender' , __name__ , url_prefix = '/sender' )
@@ -20,11 +21,12 @@ def index(pid, tid):
20
21
''' Index page '''
21
22
team , project , _redirect = check_the_team_and_project_are_existed (
22
23
pid = pid , tid = tid )
23
- if _redirect :
24
+ if team is None or project is None or _redirect :
24
25
return _redirect
25
26
26
- is_admin = (g .user ['account' ]['_id' ] in team ['chiefs' ] or
27
- g .user ['account' ]['_id' ] in team ['owners' ] or
27
+ teamusers = TeamUsers .parse_obj (team )
28
+ is_admin = (g .user ['account' ]['_id' ] in teamusers .chiefs or
29
+ g .user ['account' ]['_id' ] in teamusers .owners or
28
30
g .user ['account' ]['_id' ] in project .owners )
29
31
30
32
if not is_admin :
@@ -44,7 +46,7 @@ def index(pid, tid):
44
46
45
47
if 'casename' in data and data ['casename' ] == 'get' :
46
48
campaigns = list (SenderCampaign .get_list (
47
- pid = team [ ' pid' ] , tid = team [ 'tid' ] ))
49
+ pid = team . pid , tid = team . id ))
48
50
raw_users_info = User .get_info (
49
51
uids = [c ['created' ]['uid' ] for c in campaigns ])
50
52
users_info = {}
@@ -56,7 +58,7 @@ def index(pid, tid):
56
58
57
59
if 'casename' in data and data ['casename' ] == 'create' :
58
60
resp = SenderCampaign .create (
59
- name = data ['name' ], pid = team [ ' pid' ] , tid = team [ 'tid' ] , uid = g .user ['account' ]['_id' ])
61
+ name = data ['name' ], pid = team . pid , tid = team . id , uid = g .user ['account' ]['_id' ])
60
62
61
63
return jsonify ({'cid' : resp ['_id' ]})
62
64
@@ -68,48 +70,52 @@ def campaign(pid, tid, cid):
68
70
''' campaign '''
69
71
team , project , _redirect = check_the_team_and_project_are_existed (
70
72
pid = pid , tid = tid )
71
- if _redirect :
73
+ if team is None or project is None or _redirect :
72
74
return _redirect
73
75
74
- is_admin = (g .user ['account' ]['_id' ] in team ['chiefs' ] or
75
- g .user ['account' ]['_id' ] in team ['owners' ] or
76
+ teamusers = TeamUsers .parse_obj (team )
77
+ is_admin = (g .user ['account' ]['_id' ] in teamusers .chiefs or
78
+ g .user ['account' ]['_id' ] in teamusers .owners or
76
79
g .user ['account' ]['_id' ] in project .owners )
77
80
78
81
if not is_admin :
79
82
return redirect ('/' )
80
83
81
84
campaign_data = SenderCampaign .get (cid = cid , pid = pid , tid = tid )
82
85
83
- return render_template ('./sender_campaign_index.html' , campaign = campaign_data , team = team )
86
+ return render_template ('./sender_campaign_index.html' ,
87
+ campaign = campaign_data , team = team .dict (by_alias = True ))
84
88
85
89
86
90
@VIEW_SENDER .route ('/<pid>/<tid>/campaign/<cid>/content' , methods = ('GET' , 'POST' ))
87
91
def campaign_content (pid , tid , cid ):
88
92
''' Campaign content '''
89
93
team , project , _redirect = check_the_team_and_project_are_existed (
90
94
pid = pid , tid = tid )
91
- if _redirect :
95
+ if team is None or project is None or _redirect :
92
96
return _redirect
93
97
94
- is_admin = (g .user ['account' ]['_id' ] in team ['chiefs' ] or
95
- g .user ['account' ]['_id' ] in team ['owners' ] or
98
+ teamusers = TeamUsers .parse_obj (team )
99
+ is_admin = (g .user ['account' ]['_id' ] in teamusers .chiefs or
100
+ g .user ['account' ]['_id' ] in teamusers .owners or
96
101
g .user ['account' ]['_id' ] in project .owners )
97
102
98
103
if not is_admin :
99
104
return redirect ('/' )
100
105
101
106
if request .method == 'GET' :
102
107
campaign_data = SenderCampaign .get (
103
- cid = cid , pid = team [ ' pid' ] , tid = team [ 'tid' ] )
108
+ cid = cid , pid = team . pid , tid = team . id )
104
109
105
- return render_template ('./sender_campaign_content.html' , campaign = campaign_data , team = team )
110
+ return render_template ('./sender_campaign_content.html' ,
111
+ campaign = campaign_data , team = team .dict (by_alias = True ))
106
112
107
113
if request .method == 'POST' :
108
114
data = request .get_json ()
109
115
110
116
if 'casename' in data and data ['casename' ] == 'get' :
111
117
campaign_data = SenderCampaign .get (
112
- cid = cid , pid = team [ ' pid' ] , tid = team [ 'tid' ] )
118
+ cid = cid , pid = team . pid , tid = team . id )
113
119
return jsonify ({'mail' : campaign_data ['mail' ]})
114
120
115
121
if 'casename' in data and data ['casename' ] == 'save' :
@@ -131,20 +137,22 @@ def campaign_receiver(pid, tid, cid):
131
137
# pylint: disable=too-many-branches,too-many-locals,too-many-return-statements
132
138
team , project , _redirect = check_the_team_and_project_are_existed (
133
139
pid = pid , tid = tid )
134
- if _redirect :
140
+ if team is None or project is None or _redirect :
135
141
return _redirect
136
142
137
- is_admin = (g .user ['account' ]['_id' ] in team ['chiefs' ] or
138
- g .user ['account' ]['_id' ] in team ['owners' ] or
143
+ teamusers = TeamUsers .parse_obj (team )
144
+ is_admin = (g .user ['account' ]['_id' ] in teamusers .chiefs or
145
+ g .user ['account' ]['_id' ] in teamusers .owners or
139
146
g .user ['account' ]['_id' ] in project .owners )
140
147
141
148
if not is_admin :
142
149
return redirect ('/' )
143
150
144
151
campaign_data = SenderCampaign .get (
145
- cid = cid , pid = team [ ' pid' ] , tid = team [ 'tid' ] )
152
+ cid = cid , pid = team . pid , tid = team . id )
146
153
if request .method == 'GET' :
147
- return render_template ('./sender_campaign_receiver.html' , campaign = campaign_data , team = team )
154
+ return render_template ('./sender_campaign_receiver.html' ,
155
+ campaign = campaign_data , team = team .dict (by_alias = True ))
148
156
149
157
if request .method == 'POST' :
150
158
data = {}
@@ -154,19 +162,19 @@ def campaign_receiver(pid, tid, cid):
154
162
155
163
if data and 'casename' in data and data ['casename' ] == 'getinit' :
156
164
teams = []
157
- for _team in Team .list_by_pid (pid = team [ ' pid' ] ):
158
- teams .append ({'tid' : _team [ 'tid' ] , 'name' : _team [ ' name' ] })
165
+ for _team in Team .list_by_pid (pid = team . pid ):
166
+ teams .append ({'tid' : _team . id , 'name' : _team . name })
159
167
160
168
team_w_tags = []
161
169
if 'tag_members' in team :
162
170
team_w_tags = team ['tag_members' ]
163
171
164
- sender_receiver = SenderReceiver .get (pid = team [ ' pid' ] , cid = cid )
172
+ sender_receiver = SenderReceiver .get (pid = team . pid , cid = cid )
165
173
166
174
picktags = []
167
175
if 'team_w_tags' in campaign_data ['receiver' ] and \
168
- team [ 'tid' ] in campaign_data ['receiver' ]['team_w_tags' ]:
169
- picktags = campaign_data ['receiver' ]['team_w_tags' ][team [ 'tid' ] ]
176
+ team . id in campaign_data ['receiver' ]['team_w_tags' ]:
177
+ picktags = campaign_data ['receiver' ]['team_w_tags' ][team . id ]
170
178
171
179
return jsonify ({'teams' : teams ,
172
180
'team_w_tags' : team_w_tags ,
@@ -178,26 +186,26 @@ def campaign_receiver(pid, tid, cid):
178
186
})
179
187
180
188
if data and 'casename' in data and data ['casename' ] == 'save' :
181
- tids = [team [ 'tid' ] for team in Team .list_by_pid (pid = team [ ' pid' ] )]
189
+ tids = [team . id for team in Team .list_by_pid (pid = team . pid )]
182
190
183
191
_result = []
184
192
for tid_info in tids :
185
193
if tid_info in data ['pickteams' ]:
186
194
_result .append (tid_info )
187
195
188
196
_team_w_tags = []
189
- if 'tag_members' in team :
190
- for tag in team [ ' tag_members' ] :
191
- if tag [ 'id' ] in data ['picktags' ]:
192
- _team_w_tags .append (tag [ 'id' ] )
197
+ if team . tag_members :
198
+ for tag in team . tag_members :
199
+ if tag . id in data ['picktags' ]:
200
+ _team_w_tags .append (tag . id )
193
201
194
202
return jsonify (SenderCampaign .save_receiver (
195
203
cid = cid , teams = _result , team_w_tags = {
196
- team [ 'tid' ] : _team_w_tags },
204
+ team . id : _team_w_tags },
197
205
all_users = bool (data ['is_all_users' ]))['receiver' ])
198
206
199
207
if request .form ['uploadtype' ] == 'remove' :
200
- SenderReceiver .remove (pid = team [ ' pid' ] , cid = cid )
208
+ SenderReceiver .remove (pid = team . pid , cid = cid )
201
209
202
210
return jsonify ({'file' : [],
203
211
'uploadtype' : request .form ['uploadtype' ],
@@ -208,9 +216,9 @@ def campaign_receiver(pid, tid, cid):
208
216
request .files ['file' ].read ().decode ('utf8' ))))
209
217
if request .form ['uploadtype' ] == 'replace' :
210
218
SenderReceiver .replace (
211
- pid = team [ ' pid' ] , cid = cid , datas = csv_file )
219
+ pid = team . pid , cid = cid , datas = csv_file )
212
220
elif request .form ['uploadtype' ] == 'update' :
213
- SenderReceiver .update (pid = team [ ' pid' ] , cid = cid , datas = csv_file )
221
+ SenderReceiver .update (pid = team . pid , cid = cid , datas = csv_file )
214
222
215
223
return jsonify ({'file' : csv_file ,
216
224
'uploadtype' : request .form ['uploadtype' ],
@@ -225,20 +233,22 @@ def campaign_schedule(pid, tid, cid):
225
233
# pylint: disable=too-many-locals,too-many-branches,too-many-statements,too-many-return-statements
226
234
team , project , _redirect = check_the_team_and_project_are_existed (
227
235
pid = pid , tid = tid )
228
- if _redirect :
236
+ if team is None or project is None or _redirect :
229
237
return _redirect
230
238
231
- is_admin = (g .user ['account' ]['_id' ] in team ['chiefs' ] or
232
- g .user ['account' ]['_id' ] in team ['owners' ] or
239
+ teamusers = TeamUsers .parse_obj (team )
240
+ is_admin = (g .user ['account' ]['_id' ] in teamusers .chiefs or
241
+ g .user ['account' ]['_id' ] in teamusers .owners or
233
242
g .user ['account' ]['_id' ] in project .owners )
234
243
235
244
if not is_admin :
236
245
return redirect ('/' )
237
246
238
247
campaign_data = SenderCampaign .get (
239
- cid = cid , pid = team [ ' pid' ] , tid = team [ 'tid' ] )
248
+ cid = cid , pid = team . pid , tid = team . id )
240
249
if request .method == 'GET' :
241
- return render_template ('./sender_campaign_schedule.html' , campaign = campaign_data , team = team )
250
+ return render_template ('./sender_campaign_schedule.html' ,
251
+ campaign = campaign_data , team = team .dict (by_alias = True ))
242
252
243
253
if request .method == 'POST' :
244
254
data = request .get_json ()
@@ -265,16 +275,16 @@ def campaign_schedule(pid, tid, cid):
265
275
for raw in raws :
266
276
user_datas .append (dict (zip (fields , raw )))
267
277
268
- fields , raws = SenderReceiver .get (pid = team [ ' pid' ] , cid = cid )
278
+ fields , raws = SenderReceiver .get (pid = team . pid , cid = cid )
269
279
for raw in raws :
270
280
user_datas .append (dict (zip (fields , raw )))
271
281
272
282
if 'team_w_tags' in campaign_data ['receiver' ] and \
273
- team [ 'tid' ] in campaign_data ['receiver' ]['team_w_tags' ] and \
274
- campaign_data ['receiver' ]['team_w_tags' ][team [ 'tid' ] ]:
283
+ team . id in campaign_data ['receiver' ]['team_w_tags' ] and \
284
+ campaign_data ['receiver' ]['team_w_tags' ][team . id ]:
275
285
fields , raws = SenderReceiver .get_by_tags (
276
- pid = team [ ' pid' ] , tid = team [ 'tid' ] ,
277
- tags = campaign_data ['receiver' ]['team_w_tags' ][team [ 'tid' ] ])
286
+ pid = team . pid , tid = team . id ,
287
+ tags = campaign_data ['receiver' ]['team_w_tags' ][team . id ])
278
288
279
289
for raw in raws :
280
290
user_datas .append (dict (zip (fields , raw )))
@@ -290,8 +300,8 @@ def campaign_schedule(pid, tid, cid):
290
300
291
301
source = None
292
302
if campaign_data ['mail' ]['layout' ] == '2' :
293
- if 'mailling' in team and team [ ' mailling' ] :
294
- source = {'name' : team [ ' name' ] , 'mail' : team [ ' mailling' ] }
303
+ if team . mailling :
304
+ source = {'name' : team . name , 'mail' : team . mailling }
295
305
if not (source ['name' ].startswith ('COSCUP' ) or
296
306
source ['name' ].startswith ('coscup' )):
297
307
source ['name' ] = f"COSCUP { source ['name' ]} "
@@ -300,7 +310,7 @@ def campaign_schedule(pid, tid, cid):
300
310
'mail' : 'attendee@coscup.org' }
301
311
302
312
sender_mailer_start .apply_async (kwargs = {
303
- 'campaign_data' : campaign_data , 'team_name' : team [ ' name' ] , 'source' : source ,
313
+ 'campaign_data' : campaign_data , 'team_name' : team . name , 'source' : source ,
304
314
'user_datas' : user_datas , 'layout' : campaign_data ['mail' ]['layout' ]})
305
315
306
316
return jsonify (data )
@@ -310,20 +320,20 @@ def campaign_schedule(pid, tid, cid):
310
320
user_datas = []
311
321
312
322
fields , raws = SenderReceiver .get_from_user (
313
- pid = team [ ' pid' ] , tids = campaign_data ['receiver' ]['teams' ])
323
+ pid = team . pid , tids = campaign_data ['receiver' ]['teams' ])
314
324
if raws :
315
325
user_datas .append (dict (zip (fields , random .choice (raws ))))
316
326
317
- fields , raws = SenderReceiver .get (pid = team [ ' pid' ] , cid = cid )
327
+ fields , raws = SenderReceiver .get (pid = team . pid , cid = cid )
318
328
if raws :
319
329
user_datas .append (dict (zip (fields , random .choice (raws ))))
320
330
321
331
if 'team_w_tags' in campaign_data ['receiver' ] and \
322
- team [ 'tid' ] in campaign_data ['receiver' ]['team_w_tags' ] and \
323
- campaign_data ['receiver' ]['team_w_tags' ][team [ 'tid' ] ]:
332
+ team . id in campaign_data ['receiver' ]['team_w_tags' ] and \
333
+ campaign_data ['receiver' ]['team_w_tags' ][team . id ]:
324
334
fields , raws = SenderReceiver .get_by_tags (
325
- pid = team [ ' pid' ] , tid = team [ 'tid' ] ,
326
- tags = campaign_data ['receiver' ]['team_w_tags' ][team [ 'tid' ] ])
335
+ pid = team . pid , tid = team . id ,
336
+ tags = campaign_data ['receiver' ]['team_w_tags' ][team . id ])
327
337
if raws :
328
338
user_datas .append (dict (zip (fields , random .choice (raws ))))
329
339
@@ -346,8 +356,8 @@ def campaign_schedule(pid, tid, cid):
346
356
347
357
source = None
348
358
if campaign_data ['mail' ]['layout' ] == '2' :
349
- if 'mailling' in team and team [ ' mailling' ] :
350
- source = {'name' : team [ ' name' ] , 'mail' : team [ ' mailling' ] }
359
+ if team . mailling :
360
+ source = {'name' : team . name , 'mail' : team . mailling }
351
361
if not (source ['name' ].startswith ('COSCUP' ) or
352
362
source ['name' ].startswith ('coscup' )):
353
363
source ['name' ] = f"COSCUP { source ['name' ]} "
@@ -356,7 +366,7 @@ def campaign_schedule(pid, tid, cid):
356
366
'mail' : 'attendee@coscup.org' }
357
367
358
368
sender_mailer_start .apply_async (kwargs = {
359
- 'campaign_data' : campaign_data , 'team_name' : team [ ' name' ] , 'source' : source ,
369
+ 'campaign_data' : campaign_data , 'team_name' : team . name , 'source' : source ,
360
370
'user_datas' : user_datas , 'layout' : campaign_data ['mail' ]['layout' ]})
361
371
362
372
return jsonify (data )
0 commit comments