summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAleksander Machniak <machniak@kolabsys.com>2021-11-03 15:46:27 +0100
committerAleksander Machniak <machniak@kolabsys.com>2021-11-03 15:46:27 +0100
commit2f9201192934bad08e188001cd6e7622d9a1f453 (patch)
tree470c5be02759a4d6ad9b9873bd11b0e21fe9384a
parentf0a02b4484360617baa434bada6c651b8b0b5d30 (diff)
downloadpykolab-2f9201192934bad08e188001cd6e7622d9a1f453.tar.gz
Add footer_position option, so the added footer content can be also placed on top
Reviewers: #pykolab_developers, vanmeeuwen Reviewed By: #pykolab_developers, vanmeeuwen Subscribers: #pykolab_developers Differential Revision: https://git.kolab.org/D2996
-rw-r--r--tests/unit/test-012-wallace_footer.py47
-rw-r--r--wallace/module_footer.py33
2 files changed, 74 insertions, 6 deletions
diff --git a/tests/unit/test-012-wallace_footer.py b/tests/unit/test-012-wallace_footer.py
new file mode 100644
index 0000000..b044852
--- /dev/null
+++ b/tests/unit/test-012-wallace_footer.py
@@ -0,0 +1,47 @@
+# -*- coding: utf-8 -*-
+
+import os
+import pykolab
+import unittest
+
+from wallace import module_footer as Footer
+
+conf = pykolab.getConf()
+
+if not hasattr(conf, 'defaults'):
+ conf.finalize_conf()
+
+
+class TestWallaceFooter(unittest.TestCase):
+
+ def test_001_append_footer_plain(self):
+ # bottom
+ content = Footer.append_footer('test', 'footer')
+ self.assertEqual('test\n\n-- \nfooter', content)
+
+ # top
+ content = Footer.append_footer('test', 'footer', 'top')
+ self.assertEqual('footer\n\ntest', content)
+
+ def test_001_append_footer_html(self):
+ foot = "\n<!-- footer appended by Wallace -->\nfooter\n<!-- footer end -->\n"
+
+ # bottom
+ content = Footer.append_footer('<p>test</p>', 'footer', None, True)
+ self.assertEqual('<html><body><p>test</p>' + foot + '</body></html>', content)
+
+ content = Footer.append_footer('<body><p>test</p></body>', 'footer', None, True)
+ self.assertEqual('<body><p>test</p>' + foot + '</body>', content)
+
+ content = Footer.append_footer('<BODY><p>test</p></BODY>', 'footer', None, True)
+ self.assertEqual('<BODY><p>test</p>' + foot + '</BODY>', content)
+
+ # top
+ content = Footer.append_footer('<p>test</p>', 'footer', 'top', True)
+ self.assertEqual('<html><body>' + foot + '<p>test</p></body></html>', content)
+
+ content = Footer.append_footer('<body color=red"><p>test</p>', 'footer', 'top', True)
+ self.assertEqual('<body color=red">' + foot + '<p>test</p>', content)
+
+ content = Footer.append_footer('<BODY\ncolor=red"><p>test</p>', 'footer', 'top', True)
+ self.assertEqual('<BODY\ncolor=red">' + foot + '<p>test</p>', content)
diff --git a/wallace/module_footer.py b/wallace/module_footer.py
index 7544652..73916fb 100644
--- a/wallace/module_footer.py
+++ b/wallace/module_footer.py
@@ -18,6 +18,7 @@
#
import os
+import re
import tempfile
import time
@@ -42,6 +43,29 @@ def __init__():
def description():
return """Append a footer to messages."""
+def append_footer(content, footer, position=None, isHtml=False):
+ if (isHtml):
+ append = "\n<!-- footer appended by Wallace -->\n" + footer + "\n<!-- footer end -->\n"
+ if position == 'top':
+ match = re.search('(<body[^>]*>)', content, re.IGNORECASE | re.DOTALL)
+ if match:
+ content = content.replace(match.group(0), match.group(0) + append)
+ else:
+ content = "<html><body>" + append + content + "</body></html>"
+ else:
+ match = re.search('(</body>)', content, re.IGNORECASE | re.DOTALL)
+ if match:
+ content = content.replace(match.group(0), append + match.group(0))
+ else:
+ content = "<html><body>" + content + append + "</body></html>"
+ else:
+ if position == 'top':
+ content = footer + "\n\n" + content
+ else:
+ content += "\n\n-- \n" + footer
+
+ return content
+
def set_part_content(part, content):
# Reset old encoding and use quoted-printable (#5414)
del part['Content-Transfer-Encoding']
@@ -87,6 +111,7 @@ def execute(*args, **kw):
footer = {}
+ footer_position = conf.get('wallace', 'footer_position')
footer_html_file = conf.get('wallace', 'footer_html')
footer_text_file = conf.get('wallace', 'footer_text')
@@ -140,16 +165,12 @@ def execute(*args, **kw):
if content_type == "text/plain":
content = part.get_payload(decode=True)
- content += "\n\n-- \n%s" % (footer['plain'])
+ content = append_footer(content, footer['plain'], footer_position, false)
footer_added = set_part_content(part, content)
elif content_type == "text/html":
content = part.get_payload(decode=True)
- append = "\n<!-- footer appended by Wallace -->\n" + footer['html']
- if "</body>" in content:
- content = content.replace("</body>", append + "</body>")
- else:
- content = "<html><body>" + content + append + "</body></html>"
+ content = append_footer(content, footer['html'], footer_position, true)
footer_added = set_part_content(part, content)
if footer_added: