8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-12-26 00:43:46 +00:00

Deplace les methodes d'envoie de mail dans models

This commit is contained in:
Gabriel Detraz 2017-08-28 21:51:12 +02:00 committed by root
parent 6c65605e7f
commit 65156db99d
3 changed files with 68 additions and 66 deletions

View file

@ -26,6 +26,9 @@ from django import forms
from django.db.models.signals import post_save, post_delete
from django.dispatch import receiver
from django.utils.functional import cached_property
from django.template import Context, RequestContext, loader
from django.core.mail import send_mail
from django.core.urlresolvers import reverse
from reversion import revisions as reversion
from django.db import transaction
@ -44,7 +47,7 @@ from django.core.validators import MinLengthValidator
from topologie.models import Room
from cotisations.models import Cotisation, Facture, Paiement, Vente
from machines.models import Interface, Machine
from preferences.models import GeneralOption, OptionalUser
from preferences.models import GeneralOption, AssoOption, OptionalUser
now = timezone.now()
@ -409,12 +412,52 @@ class User(AbstractBaseUser):
except LdapUser.DoesNotExist:
pass
def notif_inscription(self):
""" Prend en argument un objet user, envoie un mail de bienvenue """
t = loader.get_template('users/email_welcome')
options, created = AssoOption.objects.get_or_create()
general_options, created = GeneralOption.objects.get_or_create()
c = Context({
'nom': str(self.name) + ' ' + str(self.surname),
'asso_name': options.name,
'asso_email': options.contact,
'pseudo':self.pseudo,
})
send_mail('Bienvenue au %(name)s / Welcome to %(name)s' % {'name': options.name }, '',
general_options.email_from, [self.email], html_message=t.render(c))
return
def reset_passwd_mail(self, request):
""" Prend en argument un request, envoie un mail de réinitialisation de mot de pass """
req = Request()
req.type = Request.PASSWD
req.user = self
req.save()
t = loader.get_template('users/email_passwd_request')
options, created = AssoOption.objects.get_or_create()
general_options, created = GeneralOption.objects.get_or_create()
c = {
'name': str(req.user.name) + ' ' + str(req.user.surname),
'asso': options.name,
'asso_mail': options.contact,
'site_name': general_options.site_name,
'url': request.build_absolute_uri(
reverse('users:process', kwargs={'token': req.token})),
'expire_in': str(general_options.req_expire_hrs) + ' heures',
}
send_mail('Changement de mot de passe du %(name)s / Password renewal for %(name)s' % {'name': options.name }, t.render(c),
general_options.email_from, [req.user.email], fail_silently=False)
return
def __str__(self):
return self.pseudo
@receiver(post_save, sender=User)
def user_post_save(sender, **kwargs):
is_created = kwargs['created']
user = kwargs['instance']
if is_created:
user.notif_inscription()
user.ldap_sync(base=True, access_refresh=True, mac_refresh=False)
@receiver(post_delete, sender=User)
@ -571,14 +614,32 @@ class Ban(models.Model):
date_end = models.DateTimeField(help_text='%d/%m/%y %H:%M:%S')
state = models.IntegerField(choices=STATES, default=STATE_HARD)
def notif_ban(self):
""" Prend en argument un objet ban, envoie un mail de notification """
general_options, created = GeneralOption.objects.get_or_create()
t = loader.get_template('users/email_ban_notif')
options, created = AssoOption.objects.get_or_create()
c = Context({
'name': str(self.user.name) + ' ' + str(self.user.surname),
'raison': self.raison,
'date_end': self.date_end,
'asso_name' : options.name,
})
send_mail('Deconnexion disciplinaire', t.render(c),
general_options.email_from, [self.user.email], fail_silently=False)
return
def __str__(self):
return str(self.user) + ' ' + str(self.raison)
@receiver(post_save, sender=Ban)
def ban_post_save(sender, **kwargs):
ban = kwargs['instance']
is_created = kwargs['created']
user = ban.user
user.ldap_sync(base=False, access_refresh=True, mac_refresh=False)
if is_created:
ban.notif_ban()
@receiver(post_delete, sender=Ban)
def ban_post_delete(sender, **kwargs):

View file

@ -1,8 +1,8 @@
Bonjour {{name}},
Vous avez été banni par un administrateur du {{ name }} en raison de {{raison}}. Vous n'avez plus accès au réseau jusqu'au {{date_end}}.
Vous avez été banni par un administrateur du {{ asso_name }} en raison de {{raison}}. Vous n'avez plus accès au réseau jusqu'au {{date_end}}.
Pour de plus amples informations, rendez-vous à l'accueil du {{ name }}.
Pour de plus amples informations, rendez-vous à l'accueil du {{ asso_name }}.
Cordialement,
L'équipe du {{ name }}.
L'équipe du {{ asso_name }}.

View file

@ -70,55 +70,6 @@ def password_change_action(u_form, user, request, req=False):
return redirect("/")
return redirect("/users/profil/" + str(user.id))
def reset_passwd_mail(req, request):
""" Prend en argument un request, envoie un mail de réinitialisation de mot de pass """
t = loader.get_template('users/email_passwd_request')
options, created = AssoOption.objects.get_or_create()
general_options, created = GeneralOption.objects.get_or_create()
c = {
'name': str(req.user.name) + ' ' + str(req.user.surname),
'asso': options.name,
'asso_mail': options.contact,
'site_name': general_options.site_name,
'url': request.build_absolute_uri(
reverse('users:process', kwargs={'token': req.token})),
'expire_in': str(general_options.req_expire_hrs) + ' heures',
}
send_mail('Changement de mot de passe du %(name)s / Password renewal for %(name)s' % {'name': options.name }, t.render(c),
general_options.email_from, [req.user.email], fail_silently=False)
return
def notif_ban(ban):
general_options, created = GeneralOption.objects.get_or_create()
""" Prend en argument un objet ban, envoie un mail de notification """
t = loader.get_template('users/email_ban_notif')
options, created = AssoOption.objects.get_or_create()
c = Context({
'name': str(ban.user.name) + ' ' + str(ban.user.surname),
'raison': ban.raison,
'date_end': ban.date_end,
'name' : options.name,
})
send_mail('Deconnexion disciplinaire', t.render(c),
general_options.email_from, [ban.user.email], fail_silently=False)
return
def notif_inscription(user):
""" Prend en argument un objet user, envoie un mail de bienvenue """
t = loader.get_template('users/email_welcome')
options, created = AssoOption.objects.get_or_create()
general_options, created = GeneralOption.objects.get_or_create()
c = Context({
'nom': str(user.name) + ' ' + str(user.surname),
'asso_name': options.name,
'asso_email': options.contact,
'pseudo':user.pseudo,
})
send_mail('Bienvenue au %(name)s / Welcome to %(name)s' % {'name': options.name }, '',
general_options.email_from, [user.email], html_message=t.render(c))
return
@login_required
@permission_required('cableur')
def new_user(request):
@ -130,12 +81,7 @@ def new_user(request):
user.save()
reversion.set_user(request.user)
reversion.set_comment("Création")
req = Request()
req.type = Request.PASSWD
req.user = user
req.save()
reset_passwd_mail(req, request)
notif_inscription(user)
user.reset_passwd_mail(request)
messages.success(request, "L'utilisateur %s a été crée, un mail pour l'initialisation du mot de passe a été envoyé" % user.pseudo)
return redirect("/users/profil/" + str(user.id))
return form({'userform': user}, 'users/user.html', request)
@ -323,7 +269,6 @@ def add_ban(request, userid):
if ban.is_valid():
with transaction.atomic(), reversion.create_revision():
ban_object = ban.save()
notif_ban(ban_object)
reversion.set_user(request.user)
reversion.set_comment("Création")
messages.success(request, "Bannissement ajouté")
@ -719,11 +664,7 @@ def reset_password(request):
except User.DoesNotExist:
messages.error(request, "Cet utilisateur n'existe pas")
return form({'userform': userform}, 'users/user.html', request)
req = Request()
req.type = Request.PASSWD
req.user = user
req.save()
reset_passwd_mail(req, request)
user.reset_passwd_mail(request)
messages.success(request, "Un mail pour l'initialisation du mot de passe a été envoyé")
redirect("/")
return form({'userform': userform}, 'users/user.html', request)