]> git.ipfire.org Git - pbs.git/commitdiff
messages: Refactor rendering messages
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 14 Oct 2022 14:03:13 +0000 (14:03 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 14 Oct 2022 14:03:13 +0000 (14:03 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/buildservice/messages.py
src/buildservice/users.py

index 79b5b67ccc1e14e3e387473773fe6eae48e8abe5..747385d341b5c52a461155f1e6a658e353d64daa 100644 (file)
@@ -2,18 +2,17 @@
 
 import email
 import email.charset
-import email.mime.multipart
 import email.mime.text
 import email.policy
+import email.utils
 import logging
-import markdown
+import re
 import smtplib
 import socket
 import tornado.locale
 import tornado.template
 
 from . import base
-from . import users
 
 from .constants import TEMPLATESDIR
 from .decorators import *
@@ -29,7 +28,7 @@ policy = email.policy.HTTP
 
 class Messages(base.Object):
        def init(self):
-               self.templates = tornado.template.Loader(TEMPLATESDIR)
+               self.template_loader = tornado.template.Loader(TEMPLATESDIR)
 
        @lazy_property
        def queue(self):
@@ -39,6 +38,10 @@ class Messages(base.Object):
                return Queue(self.backend)
 
        def send(self, message, priority=None):
+               # Check if To is set
+               if not "To" in message:
+                       raise ValueError("Message has no To: header")
+
                # Add a message ID if non existant
                if not "Message-ID" in message:
                        message.add_header("Message-ID", self.make_msgid())
@@ -99,7 +102,7 @@ class Messages(base.Object):
 
                # Render the message
                try:
-                       message_part = t.generate(**namespace)
+                       body = t.generate(**namespace)
 
                # Reset the rendered template when it could not be rendered
                except:
@@ -107,9 +110,9 @@ class Messages(base.Object):
                        raise
 
                # Parse the message and extract the header
-               body = email.message_from_string(message_part.decode(), policy=policy)
+               body = email.message_from_string(body.decode(), policy=policy)
 
-               for header in message_part:
+               for header in body:
                        value = body[header]
 
                        # Make sure addresses are properly encoded
@@ -123,11 +126,10 @@ class Messages(base.Object):
                payload = body.get_payload()
 
                # Replace any multiple chains of newlines with only one newline
-               if mimetype == "plain":
-                       payload = re.sub(r"\n{2,}", "\n\n", payload)
+               payload = re.sub(r"\n{2,}", "\n\n", payload)
 
                # Create a MIMEText object out of it
-               body = email.mime.text.MIMEText(payload, mimetype)
+               body = email.mime.text.MIMEText(payload, "text")
 
                # Collect all parts of this message
                message_parts = [
index 304d5079b966f8e5bdc490d3fd8e64831ebed73f..ac272b4819fda905cf32f3f88901bd4a02491415 100644 (file)
@@ -282,7 +282,12 @@ class User(base.DataObject):
                return user_email
 
        def send_email(self, *args, **kwargs):
-               return self.backend.messages.send_template(self, *args, **kwargs)
+               return self.backend.messages.send_template(
+                       *args,
+                       recipient=self,
+                       locale=self.locale,
+                       **kwargs,
+               )
 
        def is_admin(self):
                return self.data.admin is True