comparison sat_frontends/quick_frontend/quick_blog.py @ 2807:0b7ce5daee9b

plugin XEP-0277: blog items data are now entirely serialised before going to bridge: So far, and for historical reasons, blog items data where serialised using a unicode: unicode dict, which was causing trouble for many types of values (timestamps, booleans, lists). This patch changes it by serialising the whole items before going to bridge, and deserialising it when going back. This way, complex data can be used easily in items. This impact psEvent and serItemsData* methods which are renamed transItemsData* because there are not always serialising anymore (a new argument "serialise" allows to specify it). When editing a blog post in jp, metadata are now more easy to manipulate, specially lists like tags.
author Goffi <goffi@goffi.org>
date Sat, 23 Feb 2019 18:59:00 +0100
parents 003b8b4b56a7
children ab2696e34d29
comparison
equal deleted inserted replaced
2806:2400cad2dace 2807:0b7ce5daee9b
51 """ 51 """
52 self.id = data["id"] 52 self.id = data["id"]
53 self.title = data.get("title") 53 self.title = data.get("title")
54 self.title_rich = None 54 self.title_rich = None
55 self.title_xhtml = data.get("title_xhtml") 55 self.title_xhtml = data.get("title_xhtml")
56 self.tags = list(data_format.dict2iter("tag", data)) 56 self.tags = data.get('tags', [])
57 self.content = data.get("content") 57 self.content = data.get("content")
58 self.content_rich = None 58 self.content_rich = None
59 self.content_xhtml = data.get("content_xhtml") 59 self.content_xhtml = data.get("content_xhtml")
60 self.author = data["author"] 60 self.author = data["author"]
61 try: 61 try:
62 author_jid = data["author_jid"] 62 author_jid = data["author_jid"]
63 self.author_jid = jid.JID(author_jid) if author_jid else None 63 self.author_jid = jid.JID(author_jid) if author_jid else None
64 except KeyError: 64 except KeyError:
65 self.author_jid = None 65 self.author_jid = None
66 66
67 try: 67 self.author_verified = data.get("author_jid_verified", False)
68 self.author_verified = C.bool(data["author_jid_verified"])
69 except KeyError:
70 self.author_verified = False
71 68
72 try: 69 try:
73 self.updated = float( 70 self.updated = float(
74 data["updated"] 71 data["updated"]
75 ) # XXX: int doesn't work here (pyjamas bug) 72 ) # XXX: int doesn't work here (pyjamas bug)
146 """ 143 """
147 items, metadata = items_tuple 144 items, metadata = items_tuple
148 for item, comments in items: 145 for item, comments in items:
149 self.addEntry(item, comments, service=service, node=node, with_update=False) 146 self.addEntry(item, comments, service=service, node=node, with_update=False)
150 147
151 def addEntry( 148 def addEntry(self, item=None, comments=None, service=None, node=None,
152 self, 149 with_update=True, editable=False, edit_entry=False):
153 item=None,
154 comments=None,
155 service=None,
156 node=None,
157 with_update=True,
158 editable=False,
159 edit_entry=False,
160 ):
161 """Add a microblog entry 150 """Add a microblog entry
162 151
163 @param editable (bool): True if the entry can be modified 152 @param editable (bool): True if the entry can be modified
164 @param item (dict, None): blog item data, or None for an empty entry 153 @param item (dict, None): blog item data, or None for an empty entry
165 @param comments (list, None): list of comments data if available 154 @param comments (list, None): list of comments data if available
284 name = "{}{}".format(prefix, suffix) 273 name = "{}{}".format(prefix, suffix)
285 value = getattr(self.item, name) 274 value = getattr(self.item, name)
286 if value is not None: 275 if value is not None:
287 mb_data[name] = value 276 mb_data[name] = value
288 277
289 data_format.iter2dict("tag", self.item.tags, mb_data) 278 mb_data['tags'] = self.item.tags
290 279
291 if self.blog.new_message_target not in (C.PUBLIC, C.GROUP): 280 if self.blog.new_message_target not in (C.PUBLIC, C.GROUP):
292 raise NotImplementedError 281 raise NotImplementedError
293 282
294 if self.level == 0: 283 if self.level == 0:
295 mb_data["allow_comments"] = C.BOOL_TRUE 284 mb_data["allow_comments"] = True
296 285
297 if self.blog.new_message_target == C.GROUP: 286 if self.blog.new_message_target == C.GROUP:
298 data_format.iter2dict("group", self.blog.targets, mb_data) 287 mb_data['groups'] = list(self.blog.targets)
299 288
300 self.blog.host.bridge.mbSend( 289 self.blog.host.bridge.mbSend(
301 unicode(self.service or ""), 290 unicode(self.service or ""),
302 self.node or "", 291 self.node or "",
303 mb_data, 292 data_format.serialise(mb_data),
304 profile=self.blog.profile, 293 profile=self.blog.profile,
305 ) 294 )
306 295
307 def delete(self): 296 def delete(self):
308 """Remove this Entry from parent manager 297 """Remove this Entry from parent manager
396 "Got {got_len} results, {rem_len} remaining".format( 385 "Got {got_len} results, {rem_len} remaining".format(
397 got_len=len(results), rem_len=remaining 386 got_len=len(results), rem_len=remaining
398 ) 387 )
399 ) 388 )
400 for result in results: 389 for result in results:
401 service, node, failure, items, metadata = result 390 service, node, failure, items_data, metadata = result
391 for item_data in items_data:
392 item_data[0] = data_format.deserialise(item_data[0])
393 for item_metadata in item_data[1]:
394 item_metadata[3] = [data_format.deserialise(i) for i in item_metadata[3]]
402 if not failure: 395 if not failure:
403 self._addMBItemsWithComments((items, metadata), service=jid.JID(service)) 396 self._addMBItemsWithComments((items_data, metadata),
397 service=jid.JID(service))
404 398
405 self.update() 399 self.update()
406 if remaining: 400 if remaining:
407 self._getResults(rt_session) 401 self._getResults(rt_session)
408 402