Commit 8ad24fec authored by André Anjos's avatar André Anjos 💬

Merge branch 'fix_email_sending' into 'master'

Fix email sending

See merge request !261
parents a66b52b7 e7b58eca
Pipeline #25503 passed with stages
in 16 minutes and 5 seconds
This diff is collapsed.
......@@ -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()))
......
......@@ -30,7 +30,6 @@ from __future__ import unicode_literals
from django.db.models import Q
from django.core.urlresolvers import reverse
from django.template import loader
from django.template import Context
from django.conf import settings
from django.db import migrations, models
......@@ -71,23 +70,21 @@ def set_profile_state(apps, schema_editor):
parsed_url = urlparse(settings.URL_PREFIX)
server_address = '%s://%s' % (parsed_url.scheme, parsed_url.hostname)
c = Context({ 'user': user,
'prefix': server_address,
})
context = {
'user': user,
'prefix': server_address,
}
try:
t = loader.get_template('registration/mail.migration_10_accounts.subject.txt')
subject = t.render(c)
t = loader.get_template('registration/mail.migration_10_accounts.subject.txt')
subject = t.render(c)
# Note: e-mail subject *must not* contain newlines
subject = settings.EMAIL_SUBJECT_PREFIX + ''.join(subject.splitlines())
# Note: e-mail subject *must not* contain newlines
subject = settings.EMAIL_SUBJECT_PREFIX + ''.join(subject.splitlines())
t = loader.get_template('registration/mail.migration_10_accounts.message.txt')
message = t.render(c)
t = loader.get_template('registration/mail.migration_10_accounts.message.txt')
message = t.render(c)
send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [user.email])
except:
pass
send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [user.email])
user.profile.supervision_key = None
......
......@@ -26,7 +26,7 @@
###############################################################################
from django.shortcuts import render
from django.template import Context, loader
from django.template import loader
from django.conf import settings
from .middleware import remove_banner_message
......
......@@ -40,7 +40,6 @@ from django.http import Http404
from django.conf import settings
from django.shortcuts import render
from django.shortcuts import get_object_or_404
from django.template import Context
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.views.generic import TemplateView
......
......@@ -27,7 +27,6 @@
from django.shortcuts import render, redirect
from django.shortcuts import get_object_or_404
from django.template import Context
from django.conf import settings
from django.views.generic import TemplateView
from django.contrib.auth.models import User
......
......@@ -29,7 +29,6 @@
from django.shortcuts import get_object_or_404
from django.shortcuts import render
from django.template import loader
from django.template import Context
from django.contrib.auth.views import login as django_login
from django.contrib.auth.forms import PasswordChangeForm
from django.contrib.auth.models import User
......@@ -47,6 +46,8 @@ from rest_framework.authtoken.models import Token
from ..import __version__
from ..common.models import Shareable
from ..accounts.models import Profile, SupervisionTrack
from ..utils import mail
from .registration.forms import BlockedUserRevalidationForm
import datetime
......@@ -127,49 +128,25 @@ def blocked_user_reactivation(request):
supervisee.save()
#Inform by email the supervisor that he has a new supervisee request
from django.core.mail import send_mail
parsed_url = urlparse(settings.URL_PREFIX)
server_address = '%s://%s' % (parsed_url.scheme, parsed_url.hostname)
c = Context({ 'supervisor': supervisor,
'supervisee': supervisee,
'prefix': server_address,
})
try:
t = loader.get_template('registration/mail.supervisor_validation.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_validation_supervisee_add_request.message.txt')
message = t.render(c)
context = {
'supervisor': supervisor,
'supervisee': supervisee,
'prefix': server_address,
}
send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [supervisor.email])
except:
pass
mail.send_email('registration/mail.supervisor_validation.subject.txt',
'registration/mail.supervisor_validation_supervisee_add_request.message.txt',
context,
[supervisor.email])
# inform the supervisee of his request
c = Context({ 'supervisor': supervisor,
'supervisee': supervisee,
'prefix': server_address,
})
try:
t = loader.get_template('registration/mail.supervisee_blocked_validation_wait.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.supervisee_blocked_state_wait_for_activation.message.txt')
message = t.render(c)
send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [supervisee.email])
except:
pass
mail.send_email('registration/mail.supervisee_blocked_validation_wait.subject.txt',
'registration/mail.supervisee_blocked_state_wait_for_activation.message.txt',
context,
[supervisee.email])
messages.success(request, "Your supervision request has been successfully processed.")
else:
......
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