1 | from engine.models import Template, Link, Placeholder, ShipmentData, Domain
|
---|
2 | from lxml import etree
|
---|
3 | import base64, os
|
---|
4 | from mailinglist import settings
|
---|
5 |
|
---|
6 | class MailingListTemplateParser():
|
---|
7 | """
|
---|
8 | Classe che recupera i link e i placeholder
|
---|
9 | """
|
---|
10 | template_id = -1
|
---|
11 | new_doc = None
|
---|
12 | placeholder_tag = "ph"
|
---|
13 |
|
---|
14 |
|
---|
15 | def __init__(self, template_id):
|
---|
16 | self.template_id = template_id
|
---|
17 |
|
---|
18 |
|
---|
19 | def link_exists(self, attrib):
|
---|
20 | href = attrib["href"]
|
---|
21 | category = attrib["category"] if ("category" in attrib) else ""
|
---|
22 | name = attrib["name"] if ("name" in attrib) else ""
|
---|
23 | tmp = Link.objects.filter(original = href, template_id = self.template_id)
|
---|
24 | if (len(tmp) > 0):
|
---|
25 | return tmp[0]
|
---|
26 | else:
|
---|
27 | return Link()
|
---|
28 | # return Link.objects.get_or_create(original = href, category = category, name = name, template_id = self.template_id)
|
---|
29 |
|
---|
30 |
|
---|
31 | def placeholder_exists(self, attrib):
|
---|
32 | name = attrib["name"]
|
---|
33 | # tmp = Placeholder.objects.filter(name = name, template_id = self.template_id)
|
---|
34 | tmp = Placeholder.objects.filter(template_id = self.template_id)
|
---|
35 | if (len(tmp) > 0):
|
---|
36 | return tmp[0]
|
---|
37 | else:
|
---|
38 | return Placeholder()
|
---|
39 | # return Placeholder.objects.get_or_create(name = name, template_id = self.template_id)
|
---|
40 |
|
---|
41 |
|
---|
42 | def start(self, tag, attrib):
|
---|
43 | if (tag == "a"):
|
---|
44 | l = self.link_exists(attrib)
|
---|
45 | # l = Link()
|
---|
46 | l.template_id = self.template_id
|
---|
47 | l.original = attrib["href"]
|
---|
48 | l.destination = attrib["href"]
|
---|
49 | if ("category" in attrib):
|
---|
50 | l.category = attrib["category"]
|
---|
51 | if ("name" in attrib):
|
---|
52 | l.name = attrib["name"]
|
---|
53 | l.save()
|
---|
54 |
|
---|
55 | if (tag == self.placeholder_tag):
|
---|
56 | if ("name" in attrib):
|
---|
57 | p = self.placeholder_exists(attrib)
|
---|
58 | # p = Placeholder()
|
---|
59 | p.template_id = self.template_id
|
---|
60 | p.name = attrib["name"]
|
---|
61 | p.save()
|
---|
62 |
|
---|
63 |
|
---|
64 | def end(self, tag):
|
---|
65 | pass
|
---|
66 |
|
---|
67 |
|
---|
68 | def data(self, data):
|
---|
69 | pass
|
---|
70 |
|
---|
71 |
|
---|
72 | def comment(self, text):
|
---|
73 | pass
|
---|
74 |
|
---|
75 |
|
---|
76 | def close(self):
|
---|
77 | pass
|
---|
78 |
|
---|
79 |
|
---|
80 | class TemplateUtility():
|
---|
81 | """
|
---|
82 | Classe per la gestione dei template:
|
---|
83 | """
|
---|
84 | template_id = -1
|
---|
85 | template_file = None
|
---|
86 | template_file_path = ""
|
---|
87 | placeholder_tag = "ph"
|
---|
88 |
|
---|
89 | # TODO : rivedere il tutto con un costruttore senza template_id
|
---|
90 |
|
---|
91 | def __init__(self, template_id):
|
---|
92 | self.template_id = template_id
|
---|
93 | template_files = Template.objects.filter(id = template_id)
|
---|
94 | if (template_files is not None and len(template_files)>0):
|
---|
95 | self.template_file = template_files[0]
|
---|
96 | self.template_file_path = os.path.join(settings.MEDIA_ROOT, self.template_file.path.name)
|
---|
97 |
|
---|
98 | def parse(self):
|
---|
99 | if (self.template_id > 0 and self.template_file is not None):
|
---|
100 | parser_instance = MailingListTemplateParser(self.template_id)
|
---|
101 | parser = etree.HTMLParser(target = parser_instance)
|
---|
102 | f = open(self.template_file_path, "r")
|
---|
103 | s = f.read()
|
---|
104 | f.close()
|
---|
105 | parser.feed(s)
|
---|
106 |
|
---|
107 |
|
---|
108 | def get_remote_template(self, src):
|
---|
109 | # TODO : implementare il download del template da un sito remoto
|
---|
110 | pass
|
---|
111 |
|
---|
112 |
|
---|
113 | def create_mail(self, shipment_id, recipient_id):
|
---|
114 | """
|
---|
115 | Crea il testo di una mail a partire da:
|
---|
116 | a) template
|
---|
117 | b) dati dell'invio relativi al destinatario
|
---|
118 | c) modifica i link. Codice = base64(base64(url) | recipient_id | shipment_id | link_id)
|
---|
119 | """
|
---|
120 | mail = ""
|
---|
121 | if (self.template_file is not None):
|
---|
122 | try:
|
---|
123 | template_file = open(self.template_file_path, "r")
|
---|
124 | except:
|
---|
125 | template_file = open(os.path.join(settings.MEDIA_ROOT, self.template_file_path))
|
---|
126 | template_content = template_file.read()
|
---|
127 | template_file.close()
|
---|
128 |
|
---|
129 | # recupero i dati da sostituire e altre info di supporto
|
---|
130 | shipment_data = ShipmentData.objects.filter(recipient_id = recipient_id, shipment_id = shipment_id)
|
---|
131 | domain = Domain.objects.filter(channel__shipment__id = shipment_id)[0].domain
|
---|
132 | links = Link.objects.filter(template_id = self.template_file.id)
|
---|
133 |
|
---|
134 | # sostituisco i placeholder e i link
|
---|
135 | parser = etree.HTMLPullParser()
|
---|
136 | parser.feed(template_content)
|
---|
137 | events = parser.read_events()
|
---|
138 |
|
---|
139 | for action, element in events:
|
---|
140 | # placeholder
|
---|
141 | if (element.tag == self.placeholder_tag and "name" in element.attrib):
|
---|
142 | key = element.attrib["name"]
|
---|
143 | value = shipment_data.filter(field = key)
|
---|
144 | if (value is not None and len(value)>0):
|
---|
145 | new_element = etree.Element("v")
|
---|
146 | new_element.text = value[0].value
|
---|
147 | element.getparent().replace(element, new_element)
|
---|
148 |
|
---|
149 | #link
|
---|
150 | if (element.tag == "a"):
|
---|
151 | href = element.attrib["href"]
|
---|
152 | if (href[:7] != "mailto:"):
|
---|
153 | # preparo i dati per lo short url
|
---|
154 | category = element.attrib["category"] if ("category" in element.attrib) else ""
|
---|
155 | name = element.attrib["name"] if ("name" in element.attrib) else ""
|
---|
156 | link = links.filter(original = href, name = name, category = category)
|
---|
157 | link_id = link[0].id if (len(link)>0) else -1
|
---|
158 | param = str(base64.b64encode(bytes(href,"utf-8")),"utf-8")
|
---|
159 | param = str(base64.b64encode(bytes(param + "|" + str(recipient_id) + "|" + str(shipment_id) + "|" + str(link_id), "utf-8")), "utf-8")
|
---|
160 |
|
---|
161 | new_href = "http://%s.%s/command/goto/%s" % (domain, settings.NL_TLD, param)
|
---|
162 | new_attrib = element.attrib
|
---|
163 | new_attrib["href"] = new_href
|
---|
164 | new_element = etree.Element("a", new_attrib)
|
---|
165 | for child in element.getchildren():
|
---|
166 | new_element.append(child)
|
---|
167 | new_element.text = element.text
|
---|
168 | element.getparent().replace(element, new_element)
|
---|
169 |
|
---|
170 |
|
---|
171 | root = parser.close()
|
---|
172 | mail = str(etree.tostring(root),"utf-8")
|
---|
173 | return mail
|
---|
174 |
|
---|
175 |
|
---|
176 |
|
---|