-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmail.py
132 lines (103 loc) · 4.32 KB
/
mail.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# -*- coding: utf-8 -*-
# Copyright (C) 2007 Taverne Sylvain <[email protected]>
# Copyright (C) 2010-2011 David Versmisse <[email protected]>
#
# 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 3 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, see <http://www.gnu.org/licenses/>.
# Import from itools
from itools.core import merge_dicts
from itools.datatypes import String
from itools.gettext import MSG
from itools.xml import START_ELEMENT
# import from ikaaro
from ikaaro.autoform import HTMLBody, RTEWidget, timestamp_widget
from ikaaro.autoform import MultilineWidget, TextWidget
from ikaaro.datatypes import Multilingual
from ikaaro.file_views import File_Download
from ikaaro.webpage import WebPage, HTMLEditView
def rewrite_css_to_attribute(stream):
"""
Replace for table and td
style="width=xxx" by width="xxx"
style="height=xxx" by height="xxx"
style="background-color=xxx" by bgcolor="xxx"
style="text-align=xxx" by align="xxx"
"""
style_map = {'width': 'width',
'height': 'height',
'background-color': 'bgcolor',
'text-align': 'align'}
for event in stream:
type, value, line = event
if type == START_ELEMENT:
tag_uri, tag_name, _attributes = value
if tag_name not in ('table', 'td'):
yield event
continue
# Rewrite attributes
attributes = _attributes.copy()
style = attributes.get((None, 'style'), None)
if not style:
# Do nothing
yield event
continue
for item in style.split(';'):
try:
key, value = item.split(':')
except ValueError:
continue
key = key.strip()
if key in style_map:
attributes[(None, style_map[key])] = value.strip()
yield type, (tag_uri, tag_name, attributes), line
else:
yield event
class EmailHTMLBody(HTMLBody):
def decode(cls, data):
events = super(EmailHTMLBody, cls).decode(data)
return list(rewrite_css_to_attribute(events))
class EmailResource_Edit(HTMLEditView):
schema = {'timestamp': HTMLEditView.schema['timestamp'],
'title': Multilingual,
'email_subject': Multilingual(mandatory=True),
'data': EmailHTMLBody(multilingual=True,
parameters_schema={'lang': String}),
'email_text': Multilingual}
widgets = [
TextWidget('title', title=MSG(u'Title')),
TextWidget('email_subject', title=MSG(u'Email subject')),
MultilineWidget('email_text', title=MSG(u'Email body (Text Version)')),
RTEWidget('data', title=MSG(u'Email body (HTML Version)')),
timestamp_widget]
def _get_schema(self, resource, context):
return self.schema
class EmailResource_Download(File_Download):
def get_bytes(self, resource, context):
return resource.get_email_html(context, web_version=True)
class EmailResource(WebPage):
class_id = 'email'
class_title = MSG(u'Email')
class_schema = merge_dicts(WebPage.class_schema,
email_subject=Multilingual(source='metadata'),
email_text=Multilingual(source='metadata'))
class_views = ['edit', 'download']
edit = EmailResource_Edit()
download = EmailResource_Download(title=MSG(u'View Email'))
def get_email_subject(self, context):
subject = self.get_property('email_subject')
return subject.encode('utf-8')
def get_email_text(self, context):
data = self.get_property('email_text')
return data.encode('utf-8')
def get_email_html(self, context, web_version=False):
return self.handler.to_str()