Skip to content

Commit d148e51

Browse files
committed
Fix various bugs
1 parent 924f1af commit d148e51

File tree

5 files changed

+69
-61
lines changed

5 files changed

+69
-61
lines changed

insalan/mailer.py

+52-52
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ class UserMailer:
3030
"""
3131
Send emails.
3232
"""
33-
def __init__(self, MAIL_HOST:str, MAIL_PORT:str, MAIL_FROM: str, MAIL_PASS: str, MAIL_SSL:bool, TEST: bool = False):
34-
self.MAIL_HOST = MAIL_HOST
35-
self.MAIL_PORT = MAIL_PORT
36-
self.MAIL_SSL = MAIL_SSL
37-
self.MAIL_FROM = MAIL_FROM
38-
self.MAIL_PASS = MAIL_PASS
39-
self.TEST = TEST
33+
def __init__(self, mail_host:str, mail_port:str, mail_from: str, mail_pass: str, mail_ssl:bool, test: bool = False):
34+
self.mail_host = mail_host
35+
self.mail_port = mail_port
36+
self.mail_ssl = mail_ssl
37+
self.mail_from = mail_from
38+
self.mail_pass = mail_pass
39+
self.test = test
4040
self.queue = []
4141

4242
def send_email_confirmation(self, user_object: User):
@@ -51,22 +51,22 @@ def send_email_confirmation(self, user_object: User):
5151
user = user_object.username
5252

5353
connection = get_connection(
54-
host=self.MAIL_HOST,
55-
port=self.MAIL_PORT,
56-
password=self.MAIL_PASS,
54+
host=self.mail_host,
55+
port=self.mail_port,
56+
password=self.mail_pass,
5757
fail_silently=False,
58-
use_ssl=self.MAIL_SSL,
59-
username=self.MAIL_FROM,
58+
use_ssl=self.mail_ssl,
59+
username=self.mail_from,
6060
)
6161
email = EmailMessage(
6262
insalan.settings.EMAIL_SUBJECT_PREFIX + _("Confirmez votre courriel"),
6363
_("Confirmez votre adresse de courriel en cliquant sur ") +
6464
f"{insalan.settings.PROTOCOL}://{insalan.settings.WEBSITE_HOST}/verification/{user}/{token}",
65-
self.MAIL_FROM,
65+
self.mail_from,
6666
[user_object.email],
6767
connection=connection,
6868
)
69-
if self.TEST:
69+
if self.test:
7070
email.send()
7171
else:
7272
self.queue.append(email)
@@ -80,11 +80,11 @@ def send_password_reset(self, user_object: User):
8080

8181
connection = get_connection(
8282
fail_silently=False,
83-
username=self.MAIL_FROM,
84-
password=self.MAIL_PASS,
85-
host=self.MAIL_HOST,
86-
port=self.MAIL_PORT,
87-
use_ssl=self.MAIL_SSL,
83+
username=self.mail_from,
84+
password=self.mail_pass,
85+
host=self.mail_host,
86+
port=self.mail_port,
87+
use_ssl=self.mail_ssl,
8888
)
8989
email = EmailMessage(
9090
insalan.settings.EMAIL_SUBJECT_PREFIX + _("Demande de ré-initialisation de mot de passe"),
@@ -94,11 +94,11 @@ def send_password_reset(self, user_object: User):
9494
"vous pouvez cliquer sur le lien suivant: "
9595
) +
9696
f"{insalan.settings.PROTOCOL}://{insalan.settings.WEBSITE_HOST}/reset-password/{user}/{token}",
97-
self.MAIL_FROM,
97+
self.mail_from,
9898
[user_object.email],
9999
connection=connection,
100100
)
101-
if self.TEST:
101+
if self.test:
102102
email.send()
103103
else:
104104
self.queue.append(email)
@@ -109,43 +109,43 @@ def send_kick_mail(self, user_object: User, team_name: str):
109109
"""
110110
connection = get_connection(
111111
fail_silently=False,
112-
username=self.MAIL_FROM,
113-
password=self.MAIL_PASS,
114-
host=self.MAIL_HOST,
115-
port=self.MAIL_PORT,
116-
use_ssl=self.MAIL_SSL,
112+
username=self.mail_from,
113+
password=self.mail_pass,
114+
host=self.mail_host,
115+
port=self.mail_port,
116+
use_ssl=self.mail_ssl,
117117
)
118118
email = EmailMessage(
119119
insalan.settings.EMAIL_SUBJECT_PREFIX + _("Vous avez été exclu.e de votre équipe"),
120120
_("Vous avez été exclu.e de l'équipe %s.") % team_name,
121-
self.MAIL_FROM,
121+
self.mail_from,
122122
[user_object.email],
123123
connection=connection,
124124
)
125-
if self.TEST:
125+
if self.test:
126126
email.send()
127127
else:
128128
self.queue.append(email)
129129

130130
def send_ticket_mail(self, user_object: User, ticket: str):
131131
"""
132-
Send a mail to a user that has been kicked.
132+
Send a mail with the ticket in attachment.
133133
"""
134134

135135
ticket_pdf = TicketManager.generate_ticket_pdf(ticket)
136136

137137
connection = get_connection(
138138
fail_silently=False,
139-
username=self.MAIL_FROM,
140-
password=self.MAIL_PASS,
141-
host=self.MAIL_HOST,
142-
port=self.MAIL_PORT,
143-
use_ssl=self.MAIL_SSL,
139+
username=self.mail_from,
140+
password=self.mail_pass,
141+
host=self.mail_host,
142+
port=self.mail_port,
143+
use_ssl=self.mail_ssl,
144144
)
145145
email = EmailMessage(
146146
insalan.settings.EMAIL_SUBJECT_PREFIX + _("Votre billet pour l'InsaLan"),
147147
_("Votre inscription pour l'Insalan a été payée. Votre billet est disponible en pièce jointe. Vous pouvez retrouver davantages d'informations sur l'évènement sur le site internet de l'InsaLan."),
148-
self.MAIL_FROM,
148+
self.mail_from,
149149
[user_object.email],
150150
connection=connection,
151151
)
@@ -155,7 +155,7 @@ def send_ticket_mail(self, user_object: User, ticket: str):
155155
"application/pdf"
156156
)
157157

158-
if self.TEST:
158+
if self.test:
159159
email.send()
160160
else:
161161
self.queue.append(email)
@@ -166,23 +166,23 @@ def send_tournament_mail(self, user_object: User, title: str, content: str, atta
166166
"""
167167
connection = get_connection(
168168
fail_silently=False,
169-
username=self.MAIL_FROM,
170-
password=self.MAIL_PASS,
171-
host=self.MAIL_HOST,
172-
port=self.MAIL_PORT,
173-
use_ssl=self.MAIL_SSL,
169+
username=self.mail_from,
170+
password=self.mail_pass,
171+
host=self.mail_host,
172+
port=self.mail_port,
173+
use_ssl=self.mail_ssl,
174174
)
175175
email = EmailMessage(
176176
insalan.settings.EMAIL_SUBJECT_PREFIX + title,
177177
content,
178-
self.MAIL_FROM,
178+
self.mail_from,
179179
[user_object.email],
180180
connection=connection,
181181
)
182182
if attachment:
183183
email.attach(attachment.name, attachment.read())
184184

185-
if self.TEST:
185+
if self.test:
186186
email.send()
187187
else:
188188
self.queue.append(email)
@@ -203,13 +203,13 @@ class MailManager:
203203
mailers = {}
204204

205205
@staticmethod
206-
def get_mailer(MAIL_FROM: str) -> UserMailer:
206+
def get_mailer(mail_from: str) -> UserMailer:
207207
"""
208208
Get a mailer for a specific email address.
209209
"""
210-
if MAIL_FROM not in MailManager.mailers:
210+
if mail_from not in MailManager.mailers:
211211
return None
212-
return MailManager.mailers[MAIL_FROM]
212+
return MailManager.mailers[mail_from]
213213

214214
@staticmethod
215215
def get_default_mailer() -> UserMailer:
@@ -221,11 +221,11 @@ def get_default_mailer() -> UserMailer:
221221
return list(MailManager.mailers.values())[0]
222222

223223
@staticmethod
224-
def add_mailer(MAIL_HOST:str, MAIL_PORT: str, MAIL_FROM: str, MAIL_PASS: str, MAIL_SSL:bool, TEST: bool = False):
224+
def add_mailer(mail_host:str, mail_port: str, mail_from: str, mail_pass: str, mail_ssl:bool, test: bool = False):
225225
"""
226226
Add a mailer for a specific email address.
227227
"""
228-
MailManager.mailers[MAIL_FROM] = UserMailer(MAIL_HOST, MAIL_PORT, MAIL_FROM, MAIL_PASS, MAIL_SSL, TEST=TEST)
228+
MailManager.mailers[mail_from] = UserMailer(mail_host, mail_port, mail_from, mail_pass, mail_ssl, test=test)
229229

230230
@staticmethod
231231
def send_queued_mail():
@@ -240,16 +240,16 @@ def start_scheduler():
240240
logging.getLogger('apscheduler.executors.default').setLevel(logging.WARNING)
241241

242242
# Check if we are in test mode
243-
TEST = 'test' in sys.argv
244-
print(TEST, file=sys.stderr)
243+
test = 'test' in sys.argv
244+
print(test, file=sys.stderr)
245245

246246
# Add mailers
247247
for auth in insalan.settings.EMAIL_AUTH:
248248
mailer = insalan.settings.EMAIL_AUTH[auth]
249-
MailManager.add_mailer(mailer["host"], mailer["port"], mailer["from"], mailer["pass"], mailer["ssl"], TEST=TEST)
249+
MailManager.add_mailer(mailer["host"], mailer["port"], mailer["from"], mailer["pass"], mailer["ssl"], test=test)
250250

251251
# Start scheduler
252-
if not TEST:
252+
if not test:
253253
scheduler = BackgroundScheduler()
254254
scheduler.add_job(MailManager.send_queued_mail, 'interval', seconds=30)
255255
scheduler.start()

insalan/settings.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,9 @@
222222

223223
]
224224
CSRF_COOKIE_DOMAIN = '.' + getenv("WEBSITE_HOST", "localhost")
225-
# MAILER SETTINGS
225+
# MAILER SETTINGS - Mail are not sent with test mode
226226
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
227-
228227
EMAIL_AUTH = json.loads(getenv("MAIL_AUTH", '{"contact": {"from":"[email protected]", "pass":"password", "host":"localhost", "port":587, "ssl":true}, "tournament": {"from":"[email protected]", "pass":"password", "host":"localhost", "port":587, "ssl":true}}'))
229-
230-
EMAIL_PORT = int(getenv("MAIL_PORT", "465"))
231-
EMAIL_USE_SSL = getenv("MAIL_SSL", "true").lower() in ["true", "1", "t", "y", "yes"]
232228
EMAIL_SUBJECT_PREFIX = "[InsaLan] "
233229

234230
# Payment variables

insalan/tickets/admin.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def send_tickets_to_mail(self, request, queryset):
2424
Action to send tickets to mail
2525
"""
2626
for ticket in queryset:
27-
MailManager.get_mailer(EMAIL_AUTH["contact"]["from"]).send_ticket_mail(ticket.user, ticket)
27+
MailManager.get_mailer(EMAIL_AUTH["tournament"]["from"]).send_ticket_mail(ticket.user, ticket)
2828
messages.info(request, _("Les tickets sélectionnés sont en cours d'envoi."))
2929

3030
admin.site.register(Ticket, TicketAdmin)

insalan/tournament/models.py

+12
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,18 @@ class Caster(models.Model):
862862

863863

864864
class TournamentMailer(models.Model):
865+
"""
866+
The TournamentMailer model is used to send emails to players of a tournament with filters.
867+
868+
The filters are:
869+
- tournament: the tournament of the players
870+
- team_validated: if the players are in validated teams
871+
- captains: if the players are captains
872+
873+
The save method is overriden to send the mail to every players matching the filters and not
874+
actually save the object. The database table should be empty at all time.
875+
876+
"""
865877
class Meta:
866878
verbose_name_plural = 'mailers' # The name displayed in the admin sidebar
867879

insalan/tournament/views.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ def patch(self, request, *args, **kwargs):
408408
player = Player.objects.get(id=uid)
409409
# if player hasn't paid, remove him from the team
410410
if player.as_user().id != user.id and player.payment_status == PaymentStatus.NOT_PAID:
411-
MailManager.get_mailer(EMAIL_AUTH["contact"]["from"]).send_kick_mail(player.as_user(), team.name)
411+
MailManager.get_mailer(EMAIL_AUTH["tournament"]["from"]).send_kick_mail(player.as_user(), team.name)
412412
player.delete()
413413

414414
# manager edit
@@ -420,7 +420,7 @@ def patch(self, request, *args, **kwargs):
420420
manager = Manager.objects.get(id=uid)
421421
# if manager hasn't paid, remove him from the team
422422
if manager.as_user().id != user.id and manager.payment_status == PaymentStatus.NOT_PAID:
423-
MailManager.get_mailer(EMAIL_AUTH["contact"]["from"]).send_kick_mail(manager.as_user(), team.name)
423+
MailManager.get_mailer(EMAIL_AUTH["tournament"]["from"]).send_kick_mail(manager.as_user(), team.name)
424424
manager.delete()
425425

426426
# substitute edit
@@ -432,7 +432,7 @@ def patch(self, request, *args, **kwargs):
432432
substitute = Substitute.objects.get(id=uid)
433433
# if substitute hasn't paid, remove him from the team
434434
if substitute.as_user().id != user.id and substitute.payment_status == PaymentStatus.NOT_PAID:
435-
MailManager.get_mailer(EMAIL_AUTH["contact"]["from"]).send_kick_mail(substitute.as_user(), team.name)
435+
MailManager.get_mailer(EMAIL_AUTH["tournament"]["from"]).send_kick_mail(substitute.as_user(), team.name)
436436
substitute.delete()
437437

438438
team.save()

0 commit comments

Comments
 (0)