Mercurial > libervia-backend
comparison sat_frontends/jp/cmd_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 | 51c53fc4fc4a |
comparison
equal
deleted
inserted
replaced
2806:2400cad2dace | 2807:0b7ce5daee9b |
---|---|
140 def setMbDataFromArgs(self, mb_data): | 140 def setMbDataFromArgs(self, mb_data): |
141 """set microblog metadata according to command line options | 141 """set microblog metadata according to command line options |
142 | 142 |
143 if metadata already exist, it will be overwritten | 143 if metadata already exist, it will be overwritten |
144 """ | 144 """ |
145 mb_data["allow_comments"] = C.boolConst(self.args.comments) | 145 if self.args.comments is not None: |
146 mb_data["allow_comments"] = self.args.comments | |
146 if self.args.tag: | 147 if self.args.tag: |
147 data_format.iter2dict("tag", self.args.tag, mb_data, check_conflict=False) | 148 mb_data[u'tags'] = self.args.tag |
148 if self.args.title is not None: | 149 if self.args.title is not None: |
149 mb_data["title"] = self.args.title | 150 mb_data["title"] = self.args.title |
150 | 151 |
151 | 152 |
152 class Set(base.CommandBase, BlogPublishCommon): | 153 class Set(base.CommandBase, BlogPublishCommon): |
177 self.setMbDataContent(content, mb_data) | 178 self.setMbDataContent(content, mb_data) |
178 | 179 |
179 self.host.bridge.mbSend( | 180 self.host.bridge.mbSend( |
180 self.args.service, | 181 self.args.service, |
181 self.args.node, | 182 self.args.node, |
182 mb_data, | 183 data_format.serialise(mb_data), |
183 self.profile, | 184 self.profile, |
184 callback=self.exitCb, | 185 callback=self.exitCb, |
185 errback=partial( | 186 errback=partial( |
186 self.errback, | 187 self.errback, |
187 msg=_(u"can't send item: {}"), | 188 msg=_(u"can't send item: {}"), |
237 header = C.A_HEADER + k + u": " + A.RESET | 238 header = C.A_HEADER + k + u": " + A.RESET |
238 lines.append(header + data[k]) | 239 lines.append(header + data[k]) |
239 return u"\n".join(lines) | 240 return u"\n".join(lines) |
240 | 241 |
241 def format_tags(self, item, keys): | 242 def format_tags(self, item, keys): |
242 tags = data_format.dict2iter("tag", item, pop=True) | 243 tags = item.pop(u'tags', []) |
243 return u", ".join(tags) | 244 return u", ".join(tags) |
244 | 245 |
245 def format_updated(self, item, keys): | 246 def format_updated(self, item, keys): |
246 return self.format_time(item["updated"]) | 247 return self.format_time(item["updated"]) |
247 | 248 |
316 key=k, | 317 key=k, |
317 k_fmt_e=A.RESET, | 318 k_fmt_e=A.RESET, |
318 sep=u"\n" if "content" in k else u"", | 319 sep=u"\n" if "content" in k else u"", |
319 ) | 320 ) |
320 value = k_cb[k](item, keys) if k in k_cb else item[k] | 321 value = k_cb[k](item, keys) if k in k_cb else item[k] |
322 if isinstance(value, bool): | |
323 value = unicode(value).lower() | |
321 self.disp(header + value) | 324 self.disp(header + value) |
322 # we want a separation line after each item but the last one | 325 # we want a separation line after each item but the last one |
323 if idx < len(items) - 1: | 326 if idx < len(items) - 1: |
324 print(u"") | 327 print(u"") |
325 | 328 |
351 author = item[u"author"] | 354 author = item[u"author"] |
352 published, updated = item[u"published"], item.get("updated") | 355 published, updated = item[u"published"], item.get("updated") |
353 else: | 356 else: |
354 author = published = updated = None | 357 author = published = updated = None |
355 if verbosity > 1: | 358 if verbosity > 1: |
356 tags = list(data_format.dict2iter("tag", item, pop=True)) | 359 tags = item.pop('tags', []) |
357 else: | 360 else: |
358 tags = None | 361 tags = None |
359 content = item.get(u"content") | 362 content = item.get(u"content") |
360 | 363 |
361 if title: | 364 if title: |
378 self.disp(content) | 381 self.disp(content) |
379 | 382 |
380 print(u"\n" + sep + "\n") | 383 print(u"\n" + sep + "\n") |
381 | 384 |
382 def mbGetCb(self, mb_result): | 385 def mbGetCb(self, mb_result): |
386 items, metadata = mb_result | |
387 items = [data_format.deserialise(i) for i in items] | |
388 mb_result = items, metadata | |
383 self.output(mb_result) | 389 self.output(mb_result) |
384 self.host.quit(C.EXIT_OK) | 390 self.host.quit(C.EXIT_OK) |
385 | 391 |
386 def mbGetEb(self, failure_): | 392 def mbGetEb(self, failure_): |
387 self.disp(u"can't get blog items: {reason}".format(reason=failure_), error=True) | 393 self.disp(u"can't get blog items: {reason}".format(reason=failure_), error=True) |
525 self.setMbDataContent(content, mb_data) | 531 self.setMbDataContent(content, mb_data) |
526 | 532 |
527 if self.pubsub_item is not None: | 533 if self.pubsub_item is not None: |
528 mb_data["id"] = self.pubsub_item | 534 mb_data["id"] = self.pubsub_item |
529 | 535 |
536 mb_data = data_format.serialise(mb_data) | |
537 | |
530 self.host.bridge.mbSend( | 538 self.host.bridge.mbSend( |
531 self.pubsub_service, self.pubsub_node, mb_data, self.profile | 539 self.pubsub_service, self.pubsub_node, mb_data, self.profile |
532 ) | 540 ) |
533 self.disp(u"Blog item published") | 541 self.disp(u"Blog item published") |
534 | 542 |
537 return SYNTAX_EXT.get(self.current_syntax, SYNTAX_EXT[""]) | 545 return SYNTAX_EXT.get(self.current_syntax, SYNTAX_EXT[""]) |
538 | 546 |
539 def getItemData(self, service, node, item): | 547 def getItemData(self, service, node, item): |
540 items = [item] if item is not None else [] | 548 items = [item] if item is not None else [] |
541 mb_data = self.host.bridge.mbGet(service, node, 1, items, {}, self.profile)[0][0] | 549 mb_data = self.host.bridge.mbGet(service, node, 1, items, {}, self.profile)[0][0] |
550 mb_data = data_format.deserialise(mb_data) | |
542 try: | 551 try: |
543 content = mb_data["content_xhtml"] | 552 content = mb_data["content_xhtml"] |
544 except KeyError: | 553 except KeyError: |
545 content = mb_data["content"] | 554 content = mb_data["content"] |
546 if content: | 555 if content: |