-
Notifications
You must be signed in to change notification settings - Fork 0
/
group.py
339 lines (288 loc) · 12.9 KB
/
group.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import tornado.web
from access_database import *
from login import Authenticated
from utils import randomString
from send_email import send_group_email
from tornado.escape import linkify
n_max_members = 256
class GroupHandler(Authenticated):
@tornado.web.authenticated
def get(self,groupId):
if not is_group_member(self.current_user['id'],groupId):
log_user(self.current_user['id'],f'group id {groupId} unauthorized get')
self.redirect("/home")
return
log_user(self.current_user['id'],f'group id {groupId} get')
user = get_user(self.current_user['id'])
group = get_group(groupId)
members = get_group_members(self.current_user['id'],groupId)
n_group_admins = get_n_group_admins(groupId)
polls = get_polls(groupId,phase=[0,1,2])
is_admin = is_group_admin(self.current_user['id'],groupId)
comments = add_tags_to_comments(get_comments_of_group(groupId),exclude=["group"])
self.render("group.html",group=group,polls=polls,members=members,is_admin=is_admin,comments=comments,user=user,n_group_admins=n_group_admins)
class GroupCreateHandler(Authenticated):
@tornado.web.authenticated
def get(self):
log_user(self.current_user['id'],'group create get')
self.render("group_create.html")
@tornado.web.authenticated
def post(self):
try:
pollCreatorLevel = 1 if self.get_body_argument('mccp') else 2
except:
pollCreatorLevel = 2
icon = save_image(request=self.request,file_object="picture",username=self.current_user["username"],folder="images/groups",default="icons/group")
group = create_group(userId=self.current_user['id'],name=self.get_body_argument('name'),
motto=self.get_body_argument('motto'),description=self.get_body_argument('description'),pollCreatorLevel=pollCreatorLevel,icon=icon)
self.redirect(f"/group/{group['id']}")
log_user(self.current_user['id'],f'group id {group["id"]} {pollCreatorLevel} create post')
class GroupAddMemberHandler(Authenticated):
@tornado.web.authenticated
def get(self,groupId):
if not is_group_admin(self.current_user['id'],groupId):
self.redirect(f"/group/{groupId}")
log_user(self.current_user['id'],f'group id {groupId} unauthorized add member post')
return
group = get_group(groupId)
n_members = len(get_group_members(self.current_user['id'],groupId))+1
members = get_group_member_suggestions(self.current_user['id'],groupId)
self.render("group_add_member.html",group=group,members=members,n_members=n_members,n_max_members=n_max_members)
log_user(self.current_user['id'],f'group id {groupId} {n_members} add member post')
@tornado.web.authenticated
def post(self,groupId):
if is_group_admin(self.current_user['id'],groupId):
user = get_user_by_username(self.get_body_argument("username"))
n_members = len(get_group_members(self.current_user['id'],groupId))+1
if n_members >= n_max_members:
group = get_group(groupId)
self.render("group_add_member.html",group=group,alert="too many members")
return
if user is not None and not is_group_member(user['id'],groupId):
group_add_member(userId=user['id'],groupId=groupId,granterId=self.current_user['id'])
self.redirect(f"/group/{groupId}")
log_user(self.current_user['id'],f'group id {groupId} add member post')
class GroupAddMemberAjaxHandler(Authenticated):
@tornado.web.authenticated
def post(self,groupId):
if not is_group_admin(self.current_user['id'],groupId):
self.write("/home")
return
dic = tornado.escape.json_decode(self.request.body)
n_members = len(get_group_members(self.current_user['id'],groupId))+1
if n_members + len(dic['members'])>n_max_members:
self.write(f"/group_add_member/{groupId}")
return
for username in dic['members']:
user = get_user_by_username(username)
if user is not None and not is_group_member(user['id'],groupId):
group_add_member(userId=user['id'],groupId=groupId,granterId=self.current_user['id'])
self.write(f"/group/{groupId}")
log_user(self.current_user['id'],f'group id {groupId} add member ajax post')
class GroupAddMemberSuggestionsHandler(Authenticated):
@tornado.web.authenticated
def post(self,groupId):
if not is_group_admin(self.current_user['id'],groupId):
self.write("error")
return
dic = tornado.escape.json_decode(self.request.body)
members = get_group_member_suggestions(self.current_user['id'],groupId,name=dic['name'])
data = self.render_string("users_update.html",members=members)
self.write(data)
class GroupRemoveMemberHandler(Authenticated):
@tornado.web.authenticated
def get(self,groupId):
if not is_group_admin(self.current_user['id'],groupId):
self.redirect(f"/group/{groupId}")
return
group = get_group(groupId)
members = get_group_members(self.current_user['id'],groupId)
user = get_user(self.current_user['id'])
self.render("group_remove_member.html",user=user,group=group,members=members)
log_user(self.current_user['id'],f'group id {groupId} remove member get')
@tornado.web.authenticated
def post(self,groupId):
if not is_group_admin(self.current_user['id'],groupId):
self.write("/home")
return
dic = tornado.escape.json_decode(self.request.body)
for username in dic['members']:
user = get_user_by_username(username)
if not is_group_admin(user['id'],groupId):
group_remove_member(userId=user['id'],groupId=groupId)
self.write(f"/group/{groupId}")
log_user(self.current_user['id'],f'group id {groupId} {len(dic["members"])} remove member post')
class GroupMakeAdminHandler(Authenticated):
@tornado.web.authenticated
def get(self,groupId):
if not is_group_admin(self.current_user['id'],groupId):
self.redirect(f"/group/{groupId}")
return
group = get_group(groupId)
members = get_group_members(self.current_user['id'],groupId)
self.render("group_make_admin.html",group=group,users=members)
log_user(self.current_user['id'],f'group id {groupId} make admin get')
@tornado.web.authenticated
def post(self,groupId):
if not is_group_admin(self.current_user['id'],groupId):
self.write("/home")
return
dic = tornado.escape.json_decode(self.request.body)
for username in dic['members']:
user = get_user_by_username(username)
group_make_admin(userId=user['id'],groupId=groupId,granterId=self.current_user['id'])
self.write(f"/group/{groupId}")
log_user(self.current_user['id'],f'group id {groupId} {len(dic["members"])} make admin post')
class GroupDeleteHandler(Authenticated):
@tornado.web.authenticated
def get(self,groupId):
if is_group_admin(self.current_user['id'],groupId) and get_n_group_admins(groupId) <= 1:
delete_group(groupId)
self.redirect(f"/group/{groupId}")
log_user(self.current_user['id'],f'group id {groupId} delete get')
class GroupLeaveHandler(Authenticated):
@tornado.web.authenticated
def get(self,groupId):
if is_group_member(self.current_user['id'],groupId):
leave_group(self.current_user['id'],groupId)
self.redirect("/home")
log_user(self.current_user['id'],f'group id {groupId} leave get')
class GroupInvitationHandler(Authenticated):
@tornado.web.authenticated
def get(self,groupId,key):
if is_group_member(self.current_user['id'],groupId):
self.redirect(f"/group/{groupId}")
return
alert = None
group = get_group(groupId)
if key != group["invitationKey"]:
alert = "wrong invitation link"
n_members = len(get_group_members(self.current_user['id'],groupId))
if n_members >= n_max_members:
alert = "maximum number of members already reached"
if alert is not None:
user = get_user(self.current_user['id'])
groups = get_groups(self.current_user['id'])
polls = get_polls_of_user(self.current_user['id'],phase=[0,1,2])
self.render("home.html",user=user,groups=groups,polls=polls,alert=alert)
return
group_add_member(userId=self.current_user['id'],groupId=groupId,granterId=group['authorId'])
self.redirect(f"/group/{groupId}")
log_user(self.current_user['id'],f'group id {groupId} invitation get')
return
class GroupInvitationSettingsHandler(Authenticated):
@tornado.web.authenticated
def get(self,groupId):
if is_group_admin(self.current_user['id'],groupId):
group = get_group(groupId)
link = ""
if group["invitationKey"]:
link = f"https://electric.vote/group_invitation/{groupId}/{group['invitationKey']}"
self.render("group_invitation.html",group=group,link=link)
log_user(self.current_user['id'],f'group id {groupId} invitation settings get')
return
self.redirect(f"/group/{groupId}")
log_user(self.current_user['id'],f'group id {groupId} invitation settings unauthorized get')
class GroupNewInvitationKeyHandler(Authenticated):
@tornado.web.authenticated
def get(self,groupId):
if is_group_admin(self.current_user['id'],groupId):
update_group(groupId,invitationKey=randomString(10))
log_user(self.current_user['id'],f'group id {groupId} new invitation key get')
else:
log_user(self.current_user['id'],f'group id {groupId} new invitation key unauthorized get')
self.redirect(f"/group_invitation_settings/{groupId}")
class GroupResetInvitationKeyHandler(Authenticated):
@tornado.web.authenticated
def get(self,groupId):
if is_group_admin(self.current_user['id'],groupId):
reset_group_invitation_key(groupId)
log_user(self.current_user['id'],f'group id {groupId} reset invitation key get')
else:
log_user(self.current_user['id'],f'group id {groupId} reset invitation key unauthorized get')
self.redirect(f"/group_invitation_settings/{groupId}")
class GroupResignHandler(Authenticated):
@tornado.web.authenticated
def get(self,groupId):
if is_group_admin(self.current_user['id'],groupId):
resign_group(self.current_user['id'],groupId)
self.redirect(f"/group/{groupId}")
log_user(self.current_user['id'],f'group id {groupId} resign get')
else:
log_user(self.current_user['id'],f'group id {groupId} resign unauthorized get')
self.redirect("/home")
class GroupSettingsHandler(Authenticated):
@tornado.web.authenticated
def get(self,groupId):
if not is_group_admin(self.current_user['id'],groupId):
self.redirect(f"/group/{groupId}")
log_user(self.current_user['id'],f'group id {groupId} settings unauthorized get')
return
group = get_group(groupId)
self.render("group_settings.html",group=group)
log_user(self.current_user['id'],f'group id {groupId} settings get')
@tornado.web.authenticated
def post(self,groupId):
if is_group_admin(self.current_user['id'],groupId):
name = self.get_body_argument('name')
if name == "":
name = None
motto = self.get_body_argument('motto')
if motto == "":
motto = None
description = self.get_body_argument('description')
if description == "":
description = None
icon = save_image(request=self.request,file_object="picture",username=self.current_user["username"],folder="images/groups",default=None)
try:
pollCreatorLevel = 1 if self.get_body_argument('mccp') else 2
except:
pollCreatorLevel = 2
group = update_group(groupId,name=name,motto=motto,description=description,pollCreatorLevel=pollCreatorLevel,icon=icon)
log_user(self.current_user['id'],f'group id {groupId} settings post')
else:
log_user(self.current_user['id'],f'group id {groupId} settings unauthorized post')
self.redirect(f"/group/{group['id']}")
class GroupSendCircularHandler(Authenticated):
@tornado.web.authenticated
def get(self,groupId):
if not is_group_admin(self.current_user['id'],groupId):
self.redirect(f"/group/{groupId}")
log_user(self.current_user['id'],f'group id {groupId} send circular unauthorized get')
return
group = get_group(groupId)
self.render("group_send_circular.html",group=group)
log_user(self.current_user['id'],f'group id {groupId} send circular get')
@tornado.web.authenticated
def post(self,groupId):
group = get_group(groupId)
if is_group_admin(self.current_user['id'],groupId):
content = self.get_body_argument('content')
user = get_user(self.current_user['id'])
subject = f"{group['name']} ({self.translate()('Circular')})"
text = f"{group['name']}\n\n{user['name']}:\n{content}"
html = self.render_string("circular_mail.html",group=group,user=user,content=esc_m(content)).decode('utf-8')
send_group_email(group,subject=subject,text=text,html=html)
log_user(self.current_user['id'],f'group id {groupId} send circular post')
else:
log_user(self.current_user['id'],f'group id {groupId} send circular unauthorized post')
self.redirect(f"/group/{group['id']}")
class GroupAjaxHandler(Authenticated):
@tornado.web.authenticated
def post(self,groupId):
if is_group_member(self.current_user['id'],groupId):
dic = tornado.escape.json_decode(self.request.body)
phase = []
if dic['open']:
phase.append(0)
if dic['voting']:
phase.append(1)
if dic['closed']:
phase.append(2)
polls = get_polls(groupId,phase=phase)
data = self.render_string("polls_update.html",polls=polls)
self.write(data)
log_user(self.current_user['id'],f'group id {groupId} filter {phase} ajax post')
else:
log_user(self.current_user['id'],f'group id filter ajax unauthorized post')
self.write("error")