8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-25 22:22:26 +00:00

Move mail util function to seperate file

This commit is contained in:
Jean-Romain Garnier 2020-04-19 20:15:23 +02:00 committed by Gabriel Detraz
parent 91504e9688
commit 00c15b4bb9
4 changed files with 54 additions and 27 deletions

45
re2o/mail_utils.py Normal file
View file

@ -0,0 +1,45 @@
# -*- mode: python; coding: utf-8 -*-
# Re2o est un logiciel d'administration développé initiallement au rezometz. Il
# se veut agnostique au réseau considéré, de manière à être installable en
# quelques clics.
#
# Copyright © 2020 Jean-Romain Garnier
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
# -*- coding: utf-8 -*-
# Jean-Romain Garnier
"""
Regroupe les fonctions en lien avec les mails
"""
from django.utils.translation import ugettext_lazy as _
from django.core.mail import send_mail as django_send_mail
from django.contrib import messages
from smtplib import SMTPException
def send_mail(request, *args, **kwargs):
"""Wrapper for Django's send_mail which handles errors"""
try:
kwargs["fail_silently"] = request is None
django_send_mail(*args, **kwargs)
except SMTPException as e:
messages.error(
request,
_("Failed to send email: %(error)s.") % {
"error": e,
},
)

View file

@ -39,10 +39,6 @@ from __future__ import unicode_literals
from django.utils import timezone from django.utils import timezone
from django.db.models import Q from django.db.models import Q
from django.contrib.auth.models import Permission from django.contrib.auth.models import Permission
from django.utils.translation import ugettext_lazy as _
from django.core.mail import send_mail as django_send_mail
from django.contrib import messages
from smtplib import SMTPException
from cotisations.models import Cotisation, Facture, Vente from cotisations.models import Cotisation, Facture, Vente
from machines.models import Interface, Machine from machines.models import Interface, Machine
@ -217,17 +213,3 @@ def remove_user_room(room, force=True):
if force or not user.has_access(): if force or not user.has_access():
user.room = None user.room = None
user.save() user.save()
def send_mail(request, *args, **kwargs):
"""Wrapper for Django's send_mail which handles errors"""
try:
kwargs["fail_silently"] = request is None
django_send_mail(*args, **kwargs)
except SMTPException as e:
messages.error(
request,
_("Failed to send email: %(error)s.") % {
"error": e,
},
)

View file

@ -5,7 +5,7 @@ from django.db.models.signals import post_save
from django.dispatch import receiver from django.dispatch import receiver
from re2o.mixins import AclMixin from re2o.mixins import AclMixin
import re2o.utils from re2o.mail_utils import send_mail
from preferences.models import GeneralOption from preferences.models import GeneralOption
@ -64,7 +64,7 @@ class Ticket(AclMixin, models.Model):
obj = "New ticket opened" obj = "New ticket opened"
template = loader.get_template("tickets/publication_mail_en") template = loader.get_template("tickets/publication_mail_en")
re2o.utils.send_mail( send_mail(
request, request,
obj, obj,
template.render(context), template.render(context),

View file

@ -83,7 +83,7 @@ from re2o.settings import LDAP, GID_RANGES, UID_RANGES
from re2o.field_permissions import FieldPermissionModelMixin from re2o.field_permissions import FieldPermissionModelMixin
from re2o.mixins import AclMixin, RevMixin from re2o.mixins import AclMixin, RevMixin
from re2o.base import smtp_check from re2o.base import smtp_check
import re2o.utils from re2o.mail_utils import send_mail
from cotisations.models import Cotisation, Facture, Paiement, Vente from cotisations.models import Cotisation, Facture, Paiement, Vente
from machines.models import Domain, Interface, Machine, regen from machines.models import Domain, Interface, Machine, regen
@ -763,7 +763,7 @@ class User(
"pseudo": self.pseudo, "pseudo": self.pseudo,
} }
re2o.utils.send_mail( send_mail(
request, request,
"Bienvenue au %(name)s / Welcome to %(name)s" "Bienvenue au %(name)s / Welcome to %(name)s"
% {"name": AssoOption.get_cached_value("name")}, % {"name": AssoOption.get_cached_value("name")},
@ -792,7 +792,7 @@ class User(
"expire_in": str(GeneralOption.get_cached_value("req_expire_hrs")), "expire_in": str(GeneralOption.get_cached_value("req_expire_hrs")),
} }
re2o.utils.send_mail( send_mail(
request, request,
"Changement de mot de passe de %(name)s / Password change for " "Changement de mot de passe de %(name)s / Password change for "
"%(name)s" % {"name": AssoOption.get_cached_value("name")}, "%(name)s" % {"name": AssoOption.get_cached_value("name")},
@ -877,7 +877,7 @@ class User(
"confirm_before_en": self.confirm_email_before_date().strftime("%Y-%m-%d"), "confirm_before_en": self.confirm_email_before_date().strftime("%Y-%m-%d"),
} }
re2o.utils.send_mail( send_mail(
request, request,
"Confirmation du mail de %(name)s / Email confirmation for " "Confirmation du mail de %(name)s / Email confirmation for "
"%(name)s" % {"name": AssoOption.get_cached_value("name")}, "%(name)s" % {"name": AssoOption.get_cached_value("name")},
@ -933,7 +933,7 @@ class User(
"pseudo": self.pseudo, "pseudo": self.pseudo,
} }
re2o.utils.send_mail( send_mail(
None, None,
"Ajout automatique d'une machine / New machine autoregistered", "Ajout automatique d'une machine / New machine autoregistered",
"", "",
@ -953,7 +953,7 @@ class User(
"site_name": GeneralOption.get_cached_value("site_name"), "site_name": GeneralOption.get_cached_value("site_name"),
} }
re2o.utils.send_mail( send_mail(
request, request,
"Suspension automatique / Automatic suspension", "Suspension automatique / Automatic suspension",
template.render(context), template.render(context),
@ -1765,7 +1765,7 @@ class Ban(RevMixin, AclMixin, models.Model):
"asso_name": AssoOption.get_cached_value("name"), "asso_name": AssoOption.get_cached_value("name"),
} }
re2o.utils.send_mail( send_mail(
request, request,
"Déconnexion disciplinaire / Disciplinary disconnection", "Déconnexion disciplinaire / Disciplinary disconnection",
template.render(context), template.render(context),