Commit b968b343 authored by Samuel GAIST's avatar Samuel GAIST Committed by Andre Anjos

[accounts][commands] Refactor email sending using helper

parent be263d4d
......@@ -36,13 +36,12 @@ from django.db import models
from django.db.models import Q
from django.contrib.auth.models import User
from django.conf import settings
from django.core.mail import send_mail
from django.template import loader
from django.template import Context
from ...models import SupervisionTrack
from ...models import Profile
from ....ui.registration.models import RegistrationProfile
from ....utils import mail
import sys
import random
......@@ -97,29 +96,21 @@ class Command(BaseCommand):
if now > supervisiontrack.expiration_date:
#if supervisor account reject all supervisees and inform them
if user.profile.is_supervisor:
c = Context({ 'supervisor': user,
'supervisee': user,
'prefix': server_address,
})
context = {
'supervisor': user,
'supervisee': user,
'prefix': server_address,
}
# Transform supervisor account to normal account and inform by email
user.profile.is_supervisor = False
user.profile.save()
user.save()
try:
t = loader.get_template('registration/mail.beatadmin_supervisor_rejected.subject.txt')
subject = t.render(c)
# Note: e-mail subject *must not* contain newlines
subject = settings.EMAIL_SUBJECT_PREFIX + ''.join(subject.splitlines())
t = loader.get_template('registration/mail.beatadmin_supervisor_rejected_blocked.message.txt')
message = t.render(c)
send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [user.email])
except:
pass
mail.send_email('registration/mail.beatadmin_supervisor_rejected.subject.txt',
'registration/mail.beatadmin_supervisor_rejected_blocked.message.txt',
context,
[user.email])
# Reject all his supervisees and inform by email
# Make sure all supervision tracks are invalid for rejected supervisor
......@@ -146,24 +137,16 @@ class Command(BaseCommand):
track_supervisee.profile.save()
track_supervisee.save()
c = Context({ 'supervisor': track_supervisor,
'supervisee': track_supervisee,
'prefix': server_address,
})
try:
t = loader.get_template('registration/mail.supervisor_failed_rejected.subject.txt')
subject = t.render(c)
# Note: e-mail subject *must not* contain newlines
subject = settings.EMAIL_SUBJECT_PREFIX + ''.join(subject.splitlines())
t = loader.get_template('registration/mail.supervisor_failed_rejected.message.txt')
message = t.render(c)
context = {
'supervisor': track_supervisor,
'supervisee': track_supervisee,
'prefix': server_address,
}
send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [track_supervisee.email])
except:
pass
mail.send_email('registration/mail.supervisor_failed_rejected.subject.txt',
'registration/mail.supervisor_failed_rejected.message.txt',
context,
[track_supervisee.email])
if track_supervisee.profile.status == Profile.WAITINGVALIDATION:
registration_profile = RegistrationProfile.objects.get(user=track_supervisee)
......@@ -180,25 +163,17 @@ class Command(BaseCommand):
track_supervisee = track.supervisee
track_supervisor = track.supervisor
c = Context({ 'supervisor': track_supervisor,
'supervisee': track_supervisee,
'prefix': server_address,
})
context = {
'supervisor': track_supervisor,
'supervisee': track_supervisee,
'prefix': server_address,
}
#New user account waiting validation, so delete this account and inform by email the user
try:
t = loader.get_template('registration/mail.supervisor_rejected.subject.txt')
subject = t.render(c)
# Note: e-mail subject *must not* contain newlines
subject = settings.EMAIL_SUBJECT_PREFIX + ''.join(subject.splitlines())
t = loader.get_template('registration/mail.supervisor_rejected_delete_account.message.txt')
message = t.render(c)
send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [track_supervisee.email])
except:
pass
mail.send_email('registration/mail.supervisor_rejected.subject.txt',
'registration/mail.supervisor_rejected_delete_account.message.txt',
context,
[track_supervisee.email])
registration_profile = RegistrationProfile.objects.get(user=track_supervisee)
track_supervisee.profile.delete()
......@@ -235,23 +210,15 @@ class Command(BaseCommand):
if supervisiontrack.expiration_date.date() - now.date() == datetime.timedelta(days=expiration_reminder):
warned_count += 1
c = Context({ 'user': user,
'expiration_date': supervisiontrack.expiration_date.date(),
})
try:
t = loader.get_template('registration/mail.account_revalidation.subject.txt')
subject = t.render(c)
# Note: e-mail subject *must not* contain newlines
subject = settings.EMAIL_SUBJECT_PREFIX + ''.join(subject.splitlines())
t = loader.get_template('registration/mail.account_revalidation.message.txt')
message = t.render(c)
context = {
'user': user,
'expiration_date': supervisiontrack.expiration_date.date(),
}
send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [supervisiontrack.supervisee.email])
except:
pass
mail.send_email('registration/mail.account_revalidation.subject.txt',
'registration/mail.account_revalidation.message.txt',
context,
[supervisiontrack.supervisee.email])
self.stdout.write('{} blocked user(s) and '.format(blocked_count)+'{} warned user(s)/'.format(warned_count) + '{} Total user(s) that need revalidation'.format(torevalidate_profiles.count()))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment