changeset 6:e7e6f3ae5555 5.2

Validate content and make it required to send
author Cédric Krier <ced@b2ck.com>
date Wed, 30 Oct 2019 11:22:38 +0100
parents a676f9084d1a
children a80770f82311
files exceptions.py marketing.py message.xml
diffstat 3 files changed, 39 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/exceptions.py	Wed Oct 30 11:22:38 2019 +0100
@@ -0,0 +1,8 @@
+# This file is part of Tryton.  The COPYRIGHT file at the top level of
+# this repository contains the full copyright notices and license terms.
+
+from trytond.model.exceptions import ValidationError
+
+
+class TemplateError(ValidationError):
+    pass
--- a/marketing.py	Tue Oct 29 23:39:02 2019 +0100
+++ b/marketing.py	Wed Oct 30 11:22:38 2019 +0100
@@ -19,6 +19,7 @@
 
 from trytond import backend
 from trytond.config import config
+from trytond.i18n import gettext
 from trytond.ir.session import token_hex
 from trytond.model import (
     DeactivableMixin, Workflow, ModelSQL, ModelView, Unique, fields)
@@ -32,6 +33,8 @@
 from trytond.url import HOSTNAME
 from trytond.wizard import Wizard, StateView, StateTransition, Button
 
+from .exceptions import TemplateError
+
 if not config.get(
         'html', 'plugins-marketing.email.message-content'):
     config.set(
@@ -325,7 +328,12 @@
     title = fields.Char(
         "Title", required=True, states=_states, depends=_depends)
     content = fields.Text(
-        "Content", states=_states, depends=_depends)
+        "Content",
+        states={
+            'required': Eval('state') != 'draft',
+            'readonly': _states['readonly'],
+            },
+        depends=['state'] + _depends)
     state = fields.Selection([
             ('draft', "Draft"),
             ('sending', "Sending"),
@@ -361,6 +369,24 @@
         return 'draft'
 
     @classmethod
+    def validate(cls, messages):
+        super().validate(messages)
+        for message in messages:
+            message.check_content()
+
+    def check_content(self):
+        if not self.content:
+            return
+        try:
+            MarkupTemplate(self.content)
+        except Exception as exception:
+            raise TemplateError(
+                gettext('marketing_email'
+                    '.msg_message_invalid_content',
+                    message=self.rec_name,
+                  exception=exception)) from exception
+
+    @classmethod
     @ModelView.button
     @Workflow.transition('draft')
     def draft(cls, messages):
--- a/message.xml	Tue Oct 29 23:39:02 2019 +0100
+++ b/message.xml	Wed Oct 30 11:22:38 2019 +0100
@@ -6,5 +6,9 @@
         <record model="ir.message" id="msg_email_list_unique">
             <field name="text">Email address can only be subscribed once on each list.</field>
         </record>
+
+        <record model="ir.message" id="msg_message_invalid_content">
+            <field name="text">Invalid content in message "%(message)s" with exception "%(exception)s".</field>
+        </record>
     </data>
 </tryton>