From 00c15b4bb9267542f1a62a0c4ce770d9fba4647e Mon Sep 17 00:00:00 2001 From: Jean-Romain Garnier Date: Sun, 19 Apr 2020 20:15:23 +0200 Subject: [PATCH] Move mail util function to seperate file --- re2o/mail_utils.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ re2o/utils.py | 18 ------------------ tickets/models.py | 4 ++-- users/models.py | 14 +++++++------- 4 files changed, 54 insertions(+), 27 deletions(-) create mode 100644 re2o/mail_utils.py diff --git a/re2o/mail_utils.py b/re2o/mail_utils.py new file mode 100644 index 00000000..065a9506 --- /dev/null +++ b/re2o/mail_utils.py @@ -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, + }, + ) diff --git a/re2o/utils.py b/re2o/utils.py index 0e735720..7c49ff0a 100644 --- a/re2o/utils.py +++ b/re2o/utils.py @@ -39,10 +39,6 @@ from __future__ import unicode_literals from django.utils import timezone from django.db.models import Q 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 machines.models import Interface, Machine @@ -217,17 +213,3 @@ def remove_user_room(room, force=True): if force or not user.has_access(): user.room = None 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, - }, - ) diff --git a/tickets/models.py b/tickets/models.py index 17736978..a8adbe87 100644 --- a/tickets/models.py +++ b/tickets/models.py @@ -5,7 +5,7 @@ from django.db.models.signals import post_save from django.dispatch import receiver from re2o.mixins import AclMixin -import re2o.utils +from re2o.mail_utils import send_mail from preferences.models import GeneralOption @@ -64,7 +64,7 @@ class Ticket(AclMixin, models.Model): obj = "New ticket opened" template = loader.get_template("tickets/publication_mail_en") - re2o.utils.send_mail( + send_mail( request, obj, template.render(context), diff --git a/users/models.py b/users/models.py index 9fb6a4b8..42df3f5a 100755 --- a/users/models.py +++ b/users/models.py @@ -83,7 +83,7 @@ from re2o.settings import LDAP, GID_RANGES, UID_RANGES from re2o.field_permissions import FieldPermissionModelMixin from re2o.mixins import AclMixin, RevMixin 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 machines.models import Domain, Interface, Machine, regen @@ -763,7 +763,7 @@ class User( "pseudo": self.pseudo, } - re2o.utils.send_mail( + send_mail( request, "Bienvenue au %(name)s / Welcome to %(name)s" % {"name": AssoOption.get_cached_value("name")}, @@ -792,7 +792,7 @@ class User( "expire_in": str(GeneralOption.get_cached_value("req_expire_hrs")), } - re2o.utils.send_mail( + send_mail( request, "Changement de mot de passe de %(name)s / Password change for " "%(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"), } - re2o.utils.send_mail( + send_mail( request, "Confirmation du mail de %(name)s / Email confirmation for " "%(name)s" % {"name": AssoOption.get_cached_value("name")}, @@ -933,7 +933,7 @@ class User( "pseudo": self.pseudo, } - re2o.utils.send_mail( + send_mail( None, "Ajout automatique d'une machine / New machine autoregistered", "", @@ -953,7 +953,7 @@ class User( "site_name": GeneralOption.get_cached_value("site_name"), } - re2o.utils.send_mail( + send_mail( request, "Suspension automatique / Automatic suspension", template.render(context), @@ -1765,7 +1765,7 @@ class Ban(RevMixin, AclMixin, models.Model): "asso_name": AssoOption.get_cached_value("name"), } - re2o.utils.send_mail( + send_mail( request, "Déconnexion disciplinaire / Disciplinary disconnection", template.render(context),