This repository has been archived by the owner on Dec 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
serializers.py
186 lines (171 loc) · 6.6 KB
/
serializers.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# -*- coding: utf-8 -*-
import lxml.etree as ET
from StringIO import StringIO
from ispdb.config.models import Domain, DomainRequest
# The serializers in reverse order, newest at the top.
def xmlOneDotOne(data):
"""
Return the configuration using the XML document that Thunderbird is expecting.
"""
desiredOutput = {
"display_name": "displayName",
"display_short_name": "displayShortName",
"incoming_hostname": "hostname",
"incoming_port": "port",
"incoming_socket_type": "socketType",
"incoming_username_form": "username",
"incoming_authentication": "authentication",
"outgoing_hostname": "hostname",
"outgoing_port": "port",
"outgoing_socket_type": "socketType",
"outgoing_username_form": "username",
"outgoing_authentication": "authentication",
}
incoming = None
outgoing = None
config = ET.Element("clientConfig")
config.attrib["version"] = "1.1"
emailProvider = ET.SubElement(config, "emailProvider")
qs = Domain.objects.filter(config=data) or (
DomainRequest.objects.filter(config=data))
for domain in qs:
if not data.email_provider_id:
data.email_provider_id = domain.name
ET.SubElement(emailProvider, "domain").text = domain.name
emailProvider.attrib["id"] = data.email_provider_id
for field in data._meta.fields:
if field.name not in desiredOutput:
continue
if field.name.startswith("incoming"):
if incoming is None:
incoming = ET.SubElement(emailProvider, "incomingServer")
incoming.attrib["type"] = data.incoming_type
name = field.name
currParent = incoming
elif field.name.startswith("outgoing"):
if outgoing is None:
outgoing = ET.SubElement(emailProvider, "outgoingServer")
outgoing.attrib["type"] = "smtp"
name = field.name
currParent = outgoing
else:
name = field.name
currParent = emailProvider
if name == "incoming_username_form" and data.incoming_username_form == "":
data.incoming_username_form = data.outgoing_username_form
name = desiredOutput[name]
e = ET.SubElement(currParent, name)
text = getattr(data, field.name)
if type(text) is bool:
# Force boolean values to use lowercase.
text = unicode(text).lower()
else:
# Force other values to be converted into unicode strings.
text = unicode(text)
e.text = text
# EnableURL
for enableurl in data.enableurl_set.all():
enable = ET.SubElement(emailProvider, "enable")
enable.attrib["visiturl"] = enableurl.url
for inst in enableurl.instructions.all():
d = ET.SubElement(enable, "instruction")
d.attrib["lang"] = inst.language
d.text = unicode(inst.instruction)
# DocURL
for docurl in data.docurl_set.all():
doc = ET.SubElement(emailProvider, "documentation")
doc.attrib["url"] = docurl.url
for desc in docurl.descriptions.all():
d = ET.SubElement(doc, "descr")
d.attrib["lang"] = desc.language
d.text = unicode(desc.description)
retval = StringIO("w")
xml = ET.ElementTree(config)
xml.write(retval, encoding="UTF-8", xml_declaration=True)
return retval.getvalue()
def xmlOneDotZero(data):
"""
Return the configuration using the XML document that Thunderbird is expecting.
"""
desiredOutput = {
"display_name": "displayName",
"display_short_name": "displayShortName",
"incoming_hostname": "hostname",
"incoming_port": "port",
"incoming_socket_type": "socketType",
"incoming_username_form": "username",
"incoming_authentication": "authentication",
"outgoing_hostname": "hostname",
"outgoing_port": "port",
"outgoing_socket_type": "socketType",
"outgoing_username_form": "username",
"outgoing_authentication": "authentication",
"outgoing_add_this_server": "addThisServer",
"outgoing_use_global_preferred_server": "useGlobalPreferredServer",
}
authentication_values = {
"password-cleartext": "plain",
"password-encrypted": "secure",
"client-ip-address": "none",
"smtp-after-pop": "none",
}
incoming = None
outgoing = None
config = ET.Element("clientConfig")
config.attrib["version"] = "1.0"
emailProvider = ET.SubElement(config, "emailProvider")
for domain in Domain.objects.filter(config=data):
ET.SubElement(emailProvider, "domain").text = domain.name
if not data.email_provider_id:
data.email_provider_id = domain.name
emailProvider.attrib["id"] = data.email_provider_id
for field in data._meta.fields:
if field.name not in desiredOutput:
continue
if field.name.startswith("incoming"):
if incoming is None:
incoming = ET.SubElement(emailProvider, "incomingServer")
incoming.attrib["type"] = data.incoming_type
name = field.name
currParent = incoming
elif field.name.startswith("outgoing"):
if outgoing is None:
outgoing = ET.SubElement(emailProvider, "outgoingServer")
outgoing.attrib["type"] = "smtp"
name = field.name
currParent = outgoing
else:
name = field.name
currParent = emailProvider
if name == "incoming_username_form" and data.incoming_username_form == "":
data.incoming_username_form = data.outgoing_username_form
name = desiredOutput[name]
e = ET.SubElement(currParent, name)
text = getattr(data, field.name)
if type(text) is bool:
# Force boolean values to use lowercase.
text = unicode(text).lower()
else:
# Force other values to be converted into unicode strings.
text = unicode(text)
# Fix up the data to make it 1.0 compliant.
if name == "authentication":
text = authentication_values.get(text, text)
e.text = text
retval = StringIO("w")
xml = ET.ElementTree(config)
xml.write(retval, encoding="UTF-8", xml_declaration=True)
return retval.getvalue()
# This is the data format version, not the application version.
# It only needs to change when the data format needs to change in a way
# that breaks old clients, which are hopefully exceptional cases.
# In other words, this does *not* change with every TB major release.
_serializers = {
"1.0" : xmlOneDotZero,
"1.1" : xmlOneDotOne,
}
def get(version):
# If there is no version requested, return the most recent version.
if version == None:
return xmlOneDotZero
return _serializers.get(version, None)