-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy path__init__.py
More file actions
484 lines (391 loc) · 16.2 KB
/
__init__.py
File metadata and controls
484 lines (391 loc) · 16.2 KB
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
"""Model classes that store extra information between individual event handling call-backs."""
import hashlib
import re
from typing import TYPE_CHECKING, override
import discord
from django.core.exceptions import ValidationError
from django.core.validators import MinValueValidator, RegexValidator
from django.db import models
from django.utils.translation import gettext_lazy as _
from django_stubs_ext.db.models import TypedModelMeta
from .utils import AsyncBaseModel, DiscordMember
if TYPE_CHECKING:
from collections.abc import Sequence
from collections.abc import Set as AbstractSet
from typing import ClassVar, Final
from django.db.models.constraints import BaseConstraint
from django_stubs_ext import StrOrPromise
__all__: "Sequence[str]" = (
"AssignedCommitteeAction",
"DiscordMember",
"DiscordMemberStrikes",
"DiscordReminder",
"GroupMadeMember",
"IntroductionReminderOptOutMember",
"LeftDiscordMember",
"SentGetRolesReminderMember",
"SentOneOffIntroductionReminderMember",
)
class AssignedCommitteeAction(AsyncBaseModel):
"""Model to represent an action that has been assigned to a Discord committee-member."""
class Status(models.TextChoices):
"""The named status and shortcode associated with the progress of each action."""
BLOCKED = "BLK", _("Blocked")
CANCELLED = "CND", _("Cancelled")
COMPLETE = "CMP", _("Complete")
IN_PROGRESS = "INP", _("In Progress")
NOT_STARTED = "NST", _("Not Started")
INSTANCES_NAME_PLURAL: str = "Assigned Committee Actions"
discord_member = models.ForeignKey(
DiscordMember,
on_delete=models.CASCADE,
related_name="assigned_committee_actions",
verbose_name=_("Discord Member"),
blank=False,
null=False,
unique=False,
)
description = models.TextField(_("Description"), max_length=200, null=False, blank=False)
status = models.CharField(
max_length=3, choices=Status, default=Status.NOT_STARTED, null=False, blank=False
)
class Meta(TypedModelMeta): # noqa: D106
verbose_name: "ClassVar[StrOrPromise]" = _("Assigned Committee Action")
constraints: "ClassVar[list[BaseConstraint] | tuple[BaseConstraint, ...]]" = (
models.UniqueConstraint(
fields=["discord_member", "description"], name="unique_user_action"
),
)
@override
def __repr__(self) -> str:
return f"<{self._meta.verbose_name}: {self.discord_member}, {self.description}"
@override
def __str__(self) -> str:
return f"{self.discord_member}: {self.description}"
class IntroductionReminderOptOutMember(AsyncBaseModel):
"""
Model to represent a Discord member that has opted out of introduction reminders.
Opting-out of introduction reminders means that they have requested to not be sent any
messages reminding them to introduce themselves in your group's Discord guild.
"""
INSTANCES_NAME_PLURAL: str = "Introduction Reminder Opt-Out Member objects"
discord_member = models.OneToOneField(
DiscordMember,
on_delete=models.CASCADE,
related_name="opted_out_of_introduction_reminders",
verbose_name=_("Discord Member"),
blank=False,
null=False,
primary_key=True,
)
class Meta(TypedModelMeta): # noqa: D106
verbose_name: "ClassVar[StrOrPromise]" = _(
"Discord Member that has Opted-Out of Introduction Reminders"
)
verbose_name_plural: "ClassVar[StrOrPromise]" = _(
"Discord Members that have Opted-Out of Introduction Reminders"
)
class SentOneOffIntroductionReminderMember(AsyncBaseModel):
"""
Represents a Discord member that has been sent a one-off introduction reminder.
A one-off introduction reminder sends a single message
reminding the Discord member to introduce themselves in your group's Discord guild,
when SEND_INTRODUCTION_REMINDERS is set to "Once".
"""
INSTANCES_NAME_PLURAL: str = "Sent One-Off Introduction Reminder Member objects"
discord_member = models.OneToOneField(
DiscordMember,
on_delete=models.CASCADE,
related_name="sent_one_off_introduction_reminder",
verbose_name=_("Discord Member"),
blank=False,
null=False,
primary_key=True,
)
class Meta(TypedModelMeta): # noqa: D106
verbose_name: "ClassVar[StrOrPromise]" = _(
"Discord Member that has had a one-off Introduction reminder sent to their DMs"
)
verbose_name_plural: "ClassVar[StrOrPromise]" = _(
"Discord Members that have had a one-off Introduction reminder sent to their DMs"
)
class SentGetRolesReminderMember(AsyncBaseModel):
"""
Represents a Discord member that has already been sent an opt-in roles reminder.
The opt-in roles reminder suggests the Discord member visit the #roles channel
to claim some opt-in roles within your group's Discord guild.
The Discord member is identified by their hashed Discord member ID.
Storing this prevents Discord members from being sent the same reminder to get their
opt-in roles multiple times, even if they have still not yet got their opt-in roles.
"""
INSTANCES_NAME_PLURAL: str = "Sent Get Roles Reminder Member objects"
discord_member = models.OneToOneField(
DiscordMember,
on_delete=models.CASCADE,
related_name="sent_get_roles_reminder",
verbose_name=_("Discord Member"),
blank=False,
null=False,
primary_key=True,
)
class Meta(TypedModelMeta): # noqa: D106
verbose_name: "ClassVar[StrOrPromise]" = _(
'Discord Member that has had a "Get Roles" reminder sent to their DMs'
)
verbose_name_plural: "ClassVar[StrOrPromise]" = _(
'Discord Members that have had a "Get Roles" reminder sent to their DMs'
)
class GroupMadeMember(AsyncBaseModel):
"""
Represents a Discord member that has successfully been given the Member role.
The group member is identified by their hashed group ID.
If your group stores your members-list on the Guild of students website,
the hashed group IDs will be hashed UoB IDs.
Storing the successfully made members prevents multiple people from getting the Member role
using the same purchased group membership.
"""
INSTANCES_NAME_PLURAL: str = "Group Made Members"
hashed_group_member_id = models.CharField(
_("Hashed Group Member ID"),
unique=True,
null=False,
blank=False,
max_length=64,
validators=[
RegexValidator(
r"\A[A-Fa-f\d]{64}\Z",
_("hashed_group_member_id must be a valid sha256 hex-digest."),
)
],
)
class Meta(TypedModelMeta): # noqa: D106
verbose_name: "ClassVar[StrOrPromise]" = _(
"Hashed Group ID of User that has been made Member"
)
verbose_name_plural: "ClassVar[StrOrPromise]" = _(
"Hashed Group IDs of Users that have been made Member"
)
@override
def __setattr__(self, name: str, value: object) -> None:
if name == "group_member_id":
if not isinstance(value, (str, int)):
INVALID_GROUP_MEMBER_ID_TYPE_MESSAGE: Final[str] = (
"group_member_id must be an instance of str or int."
)
raise TypeError(INVALID_GROUP_MEMBER_ID_TYPE_MESSAGE)
self.hashed_group_member_id = self.hash_group_member_id(value)
else:
super().__setattr__(name, value)
@override
def __str__(self) -> str:
return f"{self.hashed_group_member_id}"
@override
def __repr__(self) -> str:
return f"<{self._meta.verbose_name}: {self.hashed_group_member_id!r}>"
@classmethod
def hash_group_member_id(
cls, group_member_id: str | int, group_member_id_type: str = "community group"
) -> str:
"""
Hash the provided group_member_id.
The group_member_id value is hashed into the format
that hashed_group_member_ids are stored in the database
when new GroupMadeMember objects are created.
"""
if not re.fullmatch(r"\A\d{7}\Z", str(group_member_id)):
INVALID_GROUP_MEMBER_ID_MESSAGE: Final[str] = (
f"{group_member_id!r} is not a valid {group_member_id_type} ID."
)
raise ValueError(INVALID_GROUP_MEMBER_ID_MESSAGE)
return hashlib.sha256(str(group_member_id).encode()).hexdigest()
@classmethod
@override
def _get_proxy_field_names(cls) -> "AbstractSet[str]":
return {*super()._get_proxy_field_names(), "group_member_id"}
class DiscordReminder(AsyncBaseModel):
"""Represents a reminder that a Discord member has requested to be sent to them."""
INSTANCES_NAME_PLURAL: str = "Reminders"
discord_member = models.ForeignKey(
DiscordMember,
on_delete=models.CASCADE,
related_name="reminders",
verbose_name=_("Discord Member"),
blank=False,
null=False,
unique=False,
)
message = models.TextField(
_("Message to remind User"), max_length=1500, null=False, blank=True
)
_channel_id = models.CharField(
_("Discord Channel ID of the channel that the reminder needs to be sent in"),
unique=False,
null=False,
blank=False,
max_length=30,
validators=[
RegexValidator(
r"\A\d{17,20}\Z",
_(
"channel_id must be a valid Discord channel ID (see https://docs.pycord.dev/en/stable/api/abcs.html#discord.abc.Snowflake.id)"
),
)
],
)
_channel_type = models.IntegerField(
_("Discord Channel Type of the channel that the reminder needs to be sent in"),
choices=[
(channel_type.value, channel_type.name) for channel_type in discord.ChannelType
],
null=True,
blank=True,
)
send_datetime = models.DateTimeField(
_("Date & time to send reminder"), unique=False, null=False, blank=False
)
@property
def channel_id(self) -> int: # noqa: D102
return int(self._channel_id)
@channel_id.setter
def channel_id(self, channel_id: str | int) -> None:
self._channel_id = str(channel_id)
@property
def channel_type(self) -> discord.ChannelType: # noqa: D102
# NOTE: This finds the type of channel that the reminder needs to be sent in."""
return discord.ChannelType(self._channel_type)
@channel_type.setter
def channel_type(self, channel_type: discord.ChannelType | int) -> None:
if isinstance(channel_type, discord.ChannelType):
try:
channel_type = int(channel_type.value)
except ValueError:
INVALID_CHANNEL_TYPE_MESSAGE: Final[str] = (
"channel_type must be an integer or an instance of discord.ChannelType."
)
raise TypeError(INVALID_CHANNEL_TYPE_MESSAGE) from None
self._channel_type = channel_type
class Meta(TypedModelMeta): # noqa: D106
verbose_name: "ClassVar[StrOrPromise]" = _("A Reminder for a Discord Member")
verbose_name_plural: "ClassVar[StrOrPromise]" = _("Reminders for Discord Members")
constraints: "ClassVar[list[BaseConstraint] | tuple[BaseConstraint, ...]]" = (
models.UniqueConstraint(
fields=["discord_member", "message", "_channel_id"],
name="unique_user_channel_message",
),
)
@override
def __str__(self) -> str:
return (
f"{self.discord_member}"
f"{
''
if not self.message
else (f': {self.message[:50]}...' if len(self.message) > 50 else self.message)
}"
)
@override
def __repr__(self) -> str:
return (
f"<{self._meta.verbose_name}: {self.discord_member}, "
f"{self.channel_id!r}, {self.send_datetime!r}>"
)
def get_formatted_message(self, user_mention: str | None) -> str:
"""
Return the formatted message stored by this reminder.
Adds a mention to the Discord member that requested the reminder,
if passed in from the calling context.
"""
constructed_message: str = "This is your reminder"
if user_mention:
constructed_message += f", {user_mention}"
constructed_message += "!"
if self.message:
constructed_message = f"**{constructed_message}**\n{self.message}"
return constructed_message
@classmethod
@override
def _get_proxy_field_names(cls) -> "AbstractSet[str]":
return {*super()._get_proxy_field_names(), "channel_id", "channel_type"}
class LeftDiscordMember(AsyncBaseModel):
"""
Represents a list of roles that a member had when they left your group's Discord guild.
Storing this allows the stats commands to calculate which roles were most often held by
Discord members when they left your group's Discord guild.
"""
INSTANCES_NAME_PLURAL: str = "Left Discord Member objects"
_roles = models.JSONField("List of roles a Discord Member had")
@property
def roles(self) -> set[str]: # noqa: D102
return set(self._roles)
@roles.setter
def roles(self, roles: set[str]) -> None:
self._roles = list(roles)
class Meta(TypedModelMeta): # noqa: D106
verbose_name: "ClassVar[StrOrPromise]" = _(
"A List of Roles that a Discord Member had "
"when they left your group's Discord guild"
)
verbose_name_plural: "ClassVar[StrOrPromise]" = _(
"Lists of Roles that Discord Members had when they left your group's Discord guild"
)
@override
def __str__(self) -> str:
return f"{self.id}: {', '.join(self.roles)}"
@override
def __repr__(self) -> str:
return (
f"<{self._meta.verbose_name}: {{{', '.join(repr(role) for role in self.roles)}}}>"
)
@override
def clean(self) -> None:
if any(not isinstance(role, str) for role in self.roles):
raise ValidationError(
{"_roles": "Roles must be a set of strings representing the role names."},
code="invalid",
)
@classmethod
@override
def _get_proxy_field_names(cls) -> "AbstractSet[str]":
return {*super()._get_proxy_field_names(), "roles"}
class DiscordMemberStrikes(AsyncBaseModel):
"""
Represents a Discord member that has been given one or more strikes.
Being given a strike indicates that the Discord member has previously broken one or
more of your group's Discord guild rules, which resulted in a moderation action being taken
against them.
Storing the number of strikes a Discord member has allows future moderation actions
to be given with increasing severity (as outlined in your group's Discord guild
moderation document).
"""
INSTANCES_NAME_PLURAL: str = "Discord Member's Strikes"
discord_member = models.OneToOneField(
DiscordMember,
on_delete=models.CASCADE,
related_name="strikes",
verbose_name="Discord Member",
blank=False,
null=False,
primary_key=True,
)
strikes = models.PositiveIntegerField(
"Number of strikes",
null=False,
blank=True,
validators=[MinValueValidator(0)],
default=0,
)
class Meta(TypedModelMeta): # noqa: D106
verbose_name: "ClassVar[StrOrPromise]" = _(
"Discord Member that has been previously given one or more strikes "
"because they broke one or more of your group's Discord guild rules"
)
verbose_name_plural: "ClassVar[StrOrPromise]" = _(
"Discord Members that have been previously given one or more strikes "
"because they broke one or more of your group's Discord guild rules"
)
@override
def __str__(self) -> str:
return f"{self.discord_member}: {self.strikes}"
@override
def __repr__(self) -> str:
return f"<{self._meta.verbose_name}: {self.discord_member}, {self.strikes!r}>"