comparison libervia/backend/tools/common/data_objects.py @ 4267:32388d743348

tools (common/data_objects): set attachments `url` from `sources` if missing.
author Goffi <goffi@goffi.org>
date Wed, 12 Jun 2024 23:06:10 +0200
parents 83d8d8500bc2
children 0d7bb4df2343
comparison
equal deleted inserted replaced
4266:9fc3d28bc3f6 4267:32388d743348
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. 17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 18
19 """Objects handling bridge data, with jinja2 safe markup handling""" 19 """Objects handling bridge data, with jinja2 safe markup handling"""
20 20
21 from libervia.backend.core.constants import Const as C 21 from libervia.backend.core.constants import Const as C
22 from libervia.backend.core.log import getLogger
22 from libervia.backend.tools.common import data_format 23 from libervia.backend.tools.common import data_format
23 from os.path import basename 24 from os.path import basename
24 25
25 try: 26 try:
26 from markupsafe import Markup as safe 27 from markupsafe import Markup as safe
27 except ImportError: 28 except ImportError:
28 safe = str 29 safe = str
29 30
30 from libervia.backend.tools.common import uri as xmpp_uri 31 from libervia.backend.tools.common import uri as xmpp_uri
31 import urllib.request, urllib.parse, urllib.error 32 import urllib.request, urllib.parse, urllib.error
33
34
35 log = getLogger(__name__)
36
32 37
33 q = lambda value: urllib.parse.quote(value.encode("utf-8"), safe="@") 38 q = lambda value: urllib.parse.quote(value.encode("utf-8"), safe="@")
34 39
35 40
36 class Message(object): 41 class Message(object):
42 self._message_data = msg_data[4] 47 self._message_data = msg_data[4]
43 self._subject_data = msg_data[5] 48 self._subject_data = msg_data[5]
44 self._type = msg_data[6] 49 self._type = msg_data[6]
45 self.extra = data_format.deserialise(msg_data[7]) 50 self.extra = data_format.deserialise(msg_data[7])
46 self._html = dict(data_format.get_sub_dict("xhtml", self.extra)) 51 self._html = dict(data_format.get_sub_dict("xhtml", self.extra))
52 attachments = self.extra.get("attachments")
53 if attachments:
54 for attachment in attachments:
55 if "url" not in attachment:
56 try:
57 attachment["url"] = next(
58 s['url'] for s in attachment["sources"] if 'url' in s
59 )
60 except (StopIteration, KeyError):
61 log.warning(
62 f"An attachment has no URL: {attachment}"
63 )
47 64
48 @property 65 @property
49 def id(self): 66 def id(self):
50 return self._uid 67 return self._uid
51 68