-
Notifications
You must be signed in to change notification settings - Fork 161
/
commons.py
361 lines (292 loc) · 10.5 KB
/
commons.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
from datetime import datetime, timedelta
import uuid
from django.conf import settings
from django.contrib.auth.models import User
from django.core.mail import EmailMultiAlternatives
from django.shortcuts import get_object_or_404
from django.template.loader import render_to_string
from django.template.defaultfilters import pluralize
from django.utils.translation import gettext_lazy as _
from zds.member.models import Profile, TokenRegister, Ban
from zds.member.utils import get_bot_account
from zds.utils.models import get_hat_from_settings
from zds.mp.utils import send_mp
class ProfileCreate:
def create_profile(self, data):
"""
Creates an inactive profile in the database.
:param data: Array about a user.
:type data: array
:return: instance of a profile inactive
:rtype: Profile object
"""
username = data.get("username")
email = data.get("email")
password = data.get("password")
user = User.objects.create_user(username, email, password)
user.set_password(password)
user.is_active = False
user.backend = "django.contrib.auth.backends.ModelBackend"
profile = Profile(user=user)
return profile
def save_profile(self, profile):
"""
Saves the profile of a member.
:param profile: The profile of a member.
:type data: Profile object
:return: nothing
:rtype: None
"""
profile.save()
profile.user.save()
class TokenGenerator:
def generate_token(self, user):
"""
Generates a token for member registration.
:param user: A User object.
:type user: User object
:return: A token object
:rtype: Token object
"""
uuid_token = str(uuid.uuid4())
date_end = datetime.now() + timedelta(hours=1)
token = TokenRegister(user=user, token=uuid_token, date_end=date_end)
token.save()
return token
def send_email(self, token, user):
"""
Sends an email with a confirmation a registration which contain a link for registration validation.
:param token: The token for registration validation.
:type token: Token Object
:param user: The user just registered.
:type user: User object
:return: nothing
:rtype: None
"""
subject = _("{} - Confirmation d'inscription").format(settings.ZDS_APP["site"]["literal_name"])
from_email = "{} <{}>".format(
settings.ZDS_APP["site"]["literal_name"], settings.ZDS_APP["site"]["email_noreply"]
)
context = {
"username": user.username,
"url": settings.ZDS_APP["site"]["url"] + token.get_absolute_url(),
"site_name": settings.ZDS_APP["site"]["literal_name"],
"site_url": settings.ZDS_APP["site"]["url"],
}
message_html = render_to_string("email/member/confirm_registration.html", context)
message_txt = render_to_string("email/member/confirm_registration.txt", context)
msg = EmailMultiAlternatives(subject, message_txt, from_email, [user.email])
msg.attach_alternative(message_html, "text/html")
msg.send()
class MemberSanctionState:
"""
Super class of the enumeration to know which sanction it is.
"""
array_infos = None
def __init__(self, array_infos):
super().__init__()
self.array_infos = array_infos
def get_type(self):
"""
Gets the type of a sanction.
:return: type of the sanction.
:rtype: gettext_lazy
"""
raise NotImplementedError("`get_type()` must be implemented.")
def get_text(self):
"""
Gets the text of a sanction.
:return: text of the sanction.
:rtype: string
"""
raise NotImplementedError("`get_text()` must be implemented.")
def get_detail(self):
"""
Gets detail of a sanction.
:return: detail of the sanction.
:rtype: gettext_lazy
"""
raise NotImplementedError("`get_detail()` must be implemented.")
def apply_sanction(self, profile, ban):
"""
Applies the sanction with the `ban` object on a member with the `profile` object
and saves these objects.
:param profile: Member concerned by the sanction.
:type profile: Profile object
:param ban: Sanction.
:type ban: Ban object
:return: nothing
:rtype: None
"""
raise NotImplementedError("`apply_sanction()` must be implemented.")
def get_sanction(self, moderator, user):
"""
Gets the sanction according to the type of the sanction.
:param moderator: Moderator who applies the sanction.
:type moderator: User object
:param user: User sanctioned.
:type user: User object
:return: sanction
:rtype: Ban object
"""
ban = Ban()
ban.moderator = moderator
ban.user = user
ban.pubdate = datetime.now()
ban.type = self.get_type()
ban.note = self.get_text()
return ban
def get_message_unsanction(self):
"""
Gets the message for an un-sanction.
:return: message of the un-sanction.
:rtype: gettext_lazy
"""
return _(
"Bonjour **{0}**,\n\n"
"**Bonne nouvelle**, la sanction qui "
"pesait sur vous a été levée par **{1}** pour la raison suivante :\n\n"
"> {4}\n\n"
"Cela signifie que {3}\n\n"
"Cordialement, \n\n L'équipe {5}."
)
def get_message_sanction(self):
"""
Gets the message for a sanction.
:return: message of the sanction.
:rtype: gettext_lazy
"""
return _(
"Bonjour **{0}**,\n\n"
"Vous avez été sanctionné par **{1}** pour la raison suivante :\n\n"
"> {4}\n\n"
"La sanction est de type *{2}*, ce qui signifie que {3}\n\n"
"Cordialement, \n\nL'équipe {5}."
)
def notify_member(self, ban, msg):
"""
Notify the member sanctioned with a MP.
:param ban: Sanction.
:type ban: Ban object
:param msg: message send at the user sanctioned.
:type msg: string object
:return: nothing
:rtype: None
"""
bot = get_bot_account()
send_mp(
bot,
[ban.user],
ban.type,
"",
msg,
send_by_mail=True,
force_email=True,
hat=get_hat_from_settings("moderation"),
)
class ReadingOnlySanction(MemberSanctionState):
"""
State of the sanction reading only.
"""
def get_type(self):
return _("Lecture seule illimitée")
def get_text(self):
return self.array_infos.get("ls-text", "")
def get_detail(self):
return _(
"vous ne pouvez plus poster dans les forums, ni dans les "
"commentaires d'articles et de tutoriels jusqu'à nouvel ordre."
)
def apply_sanction(self, profile, ban):
profile.end_ban_write = None
profile.can_write = False
profile.save()
ban.save()
class TemporaryReadingOnlySanction(MemberSanctionState):
"""
State of the sanction reading only temporary.
"""
def get_type(self):
jrs = int(self.array_infos.get("ls-jrs"))
return _("Lecture seule temporaire ({0} jour{1})").format(jrs, pluralize(jrs))
def get_text(self):
return self.array_infos.get("ls-text", "")
def get_detail(self):
jrs = int(self.array_infos.get("ls-jrs"))
return _(
"vous ne pouvez plus poster dans les forums, ni dans les "
"commentaires d'articles et de tutoriels pendant {0} jour{1}."
).format(jrs, pluralize(jrs))
def apply_sanction(self, profile, ban):
day = int(self.array_infos.get("ls-jrs"))
profile.end_ban_write = datetime.now() + timedelta(days=day, hours=0, minutes=0, seconds=0)
profile.can_write = False
profile.save()
ban.save()
class DeleteReadingOnlySanction(MemberSanctionState):
"""
State of the un-sanction reading only.
"""
def get_type(self):
return _("Levée de la lecture seule")
def get_text(self):
return self.array_infos.get("unls-text", "")
def get_detail(self):
return _("vous pouvez désormais poster sur les forums, dans les " "commentaires d'articles et tutoriels.")
def apply_sanction(self, profile, ban):
profile.can_write = True
profile.end_ban_write = None
profile.save()
ban.save()
class BanSanction(MemberSanctionState):
"""
State of the sanction ban.
"""
def get_type(self):
return _("Bannissement illimité")
def get_text(self):
return self.array_infos.get("ban-text", "")
def get_detail(self):
return _("vous ne pouvez plus vous connecter sur {0} jusqu'à nouvel ordre.").format(
settings.ZDS_APP["site"]["literal_name"]
)
def apply_sanction(self, profile, ban):
profile.end_ban_read = None
profile.can_read = False
profile.save()
ban.save()
class TemporaryBanSanction(MemberSanctionState):
"""
State of the sanction ban temporary.
"""
def get_type(self):
jrs = int(self.array_infos.get("ban-jrs"))
return _("Bannissement temporaire ({0} jour{1})").format(jrs, pluralize(jrs))
def get_text(self):
return self.array_infos.get("ban-text", "")
def get_detail(self):
jrs = int(self.array_infos.get("ban-jrs"))
return _("vous ne pouvez plus vous connecter sur {0} pendant {1} jour{2}.").format(
settings.ZDS_APP["site"]["literal_name"], jrs, pluralize(jrs)
)
def apply_sanction(self, profile, ban):
day = int(self.array_infos.get("ban-jrs"))
profile.end_ban_read = datetime.now() + timedelta(days=day, hours=0, minutes=0, seconds=0)
profile.can_read = False
profile.save()
ban.save()
class DeleteBanSanction(MemberSanctionState):
"""
State of the un-sanction ban.
"""
def get_type(self):
return _("Levée du bannissement")
def get_text(self):
return self.array_infos.get("unban-text", "")
def get_detail(self):
return _("vous pouvez désormais vous connecter sur le site.")
def apply_sanction(self, profile, ban):
profile.can_read = True
profile.end_ban_read = None
profile.save()
ban.save()