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.
This commit is contained in:
parent
c89a771c06
commit
512ae0b91e
3 changed files with 188 additions and 184 deletions
|
|
@ -142,9 +142,10 @@ class BlogPublishCommon(object):
|
|||
|
||||
if metadata already exist, it will be overwritten
|
||||
"""
|
||||
mb_data["allow_comments"] = C.boolConst(self.args.comments)
|
||||
if self.args.comments is not None:
|
||||
mb_data["allow_comments"] = self.args.comments
|
||||
if self.args.tag:
|
||||
data_format.iter2dict("tag", self.args.tag, mb_data, check_conflict=False)
|
||||
mb_data[u'tags'] = self.args.tag
|
||||
if self.args.title is not None:
|
||||
mb_data["title"] = self.args.title
|
||||
|
||||
|
|
@ -179,7 +180,7 @@ class Set(base.CommandBase, BlogPublishCommon):
|
|||
self.host.bridge.mbSend(
|
||||
self.args.service,
|
||||
self.args.node,
|
||||
mb_data,
|
||||
data_format.serialise(mb_data),
|
||||
self.profile,
|
||||
callback=self.exitCb,
|
||||
errback=partial(
|
||||
|
|
@ -239,7 +240,7 @@ class Get(base.CommandBase):
|
|||
return u"\n".join(lines)
|
||||
|
||||
def format_tags(self, item, keys):
|
||||
tags = data_format.dict2iter("tag", item, pop=True)
|
||||
tags = item.pop(u'tags', [])
|
||||
return u", ".join(tags)
|
||||
|
||||
def format_updated(self, item, keys):
|
||||
|
|
@ -318,6 +319,8 @@ class Get(base.CommandBase):
|
|||
sep=u"\n" if "content" in k else u"",
|
||||
)
|
||||
value = k_cb[k](item, keys) if k in k_cb else item[k]
|
||||
if isinstance(value, bool):
|
||||
value = unicode(value).lower()
|
||||
self.disp(header + value)
|
||||
# we want a separation line after each item but the last one
|
||||
if idx < len(items) - 1:
|
||||
|
|
@ -353,7 +356,7 @@ class Get(base.CommandBase):
|
|||
else:
|
||||
author = published = updated = None
|
||||
if verbosity > 1:
|
||||
tags = list(data_format.dict2iter("tag", item, pop=True))
|
||||
tags = item.pop('tags', [])
|
||||
else:
|
||||
tags = None
|
||||
content = item.get(u"content")
|
||||
|
|
@ -380,6 +383,9 @@ class Get(base.CommandBase):
|
|||
print(u"\n" + sep + "\n")
|
||||
|
||||
def mbGetCb(self, mb_result):
|
||||
items, metadata = mb_result
|
||||
items = [data_format.deserialise(i) for i in items]
|
||||
mb_result = items, metadata
|
||||
self.output(mb_result)
|
||||
self.host.quit(C.EXIT_OK)
|
||||
|
||||
|
|
@ -527,6 +533,8 @@ class Edit(base.CommandBase, BlogPublishCommon, common.BaseEdit):
|
|||
if self.pubsub_item is not None:
|
||||
mb_data["id"] = self.pubsub_item
|
||||
|
||||
mb_data = data_format.serialise(mb_data)
|
||||
|
||||
self.host.bridge.mbSend(
|
||||
self.pubsub_service, self.pubsub_node, mb_data, self.profile
|
||||
)
|
||||
|
|
@ -539,6 +547,7 @@ class Edit(base.CommandBase, BlogPublishCommon, common.BaseEdit):
|
|||
def getItemData(self, service, node, item):
|
||||
items = [item] if item is not None else []
|
||||
mb_data = self.host.bridge.mbGet(service, node, 1, items, {}, self.profile)[0][0]
|
||||
mb_data = data_format.deserialise(mb_data)
|
||||
try:
|
||||
content = mb_data["content_xhtml"]
|
||||
except KeyError:
|
||||
|
|
|
|||
|
|
@ -988,8 +988,9 @@ class QuickApp(object):
|
|||
@param service_s (unicode): pubsub service
|
||||
@param node (unicode): pubsub node
|
||||
@param event_type (unicode): event type (one of C.PUBLISH, C.RETRACT, C.DELETE)
|
||||
@param data (dict): event data
|
||||
@param data (serialised_dict): event data
|
||||
"""
|
||||
data = data_format.deserialise(data)
|
||||
service_s = jid.JID(service_s)
|
||||
|
||||
if category == C.PS_MICROBLOG and self.MB_HANDLER:
|
||||
|
|
@ -997,9 +998,9 @@ class QuickApp(object):
|
|||
if not "content" in data:
|
||||
log.warning("No content found in microblog data")
|
||||
return
|
||||
_groups = (
|
||||
set(data_format.dict2iter("group", data)) or None
|
||||
) # FIXME: check if [] make sense (instead of None)
|
||||
|
||||
# FIXME: check if [] make sense (instead of None)
|
||||
_groups = data.get("group")
|
||||
|
||||
for wid in self.widgets.getWidgets(quick_blog.QuickBlog):
|
||||
wid.addEntryIfAccepted(service_s, node, data, _groups, profile)
|
||||
|
|
|
|||
|
|
@ -53,7 +53,7 @@ class Item(object):
|
|||
self.title = data.get("title")
|
||||
self.title_rich = None
|
||||
self.title_xhtml = data.get("title_xhtml")
|
||||
self.tags = list(data_format.dict2iter("tag", data))
|
||||
self.tags = data.get('tags', [])
|
||||
self.content = data.get("content")
|
||||
self.content_rich = None
|
||||
self.content_xhtml = data.get("content_xhtml")
|
||||
|
|
@ -64,10 +64,7 @@ class Item(object):
|
|||
except KeyError:
|
||||
self.author_jid = None
|
||||
|
||||
try:
|
||||
self.author_verified = C.bool(data["author_jid_verified"])
|
||||
except KeyError:
|
||||
self.author_verified = False
|
||||
self.author_verified = data.get("author_jid_verified", False)
|
||||
|
||||
try:
|
||||
self.updated = float(
|
||||
|
|
@ -148,16 +145,8 @@ class EntriesManager(object):
|
|||
for item, comments in items:
|
||||
self.addEntry(item, comments, service=service, node=node, with_update=False)
|
||||
|
||||
def addEntry(
|
||||
self,
|
||||
item=None,
|
||||
comments=None,
|
||||
service=None,
|
||||
node=None,
|
||||
with_update=True,
|
||||
editable=False,
|
||||
edit_entry=False,
|
||||
):
|
||||
def addEntry(self, item=None, comments=None, service=None, node=None,
|
||||
with_update=True, editable=False, edit_entry=False):
|
||||
"""Add a microblog entry
|
||||
|
||||
@param editable (bool): True if the entry can be modified
|
||||
|
|
@ -286,21 +275,21 @@ class Entry(EntriesManager):
|
|||
if value is not None:
|
||||
mb_data[name] = value
|
||||
|
||||
data_format.iter2dict("tag", self.item.tags, mb_data)
|
||||
mb_data['tags'] = self.item.tags
|
||||
|
||||
if self.blog.new_message_target not in (C.PUBLIC, C.GROUP):
|
||||
raise NotImplementedError
|
||||
|
||||
if self.level == 0:
|
||||
mb_data["allow_comments"] = C.BOOL_TRUE
|
||||
mb_data["allow_comments"] = True
|
||||
|
||||
if self.blog.new_message_target == C.GROUP:
|
||||
data_format.iter2dict("group", self.blog.targets, mb_data)
|
||||
mb_data['groups'] = list(self.blog.targets)
|
||||
|
||||
self.blog.host.bridge.mbSend(
|
||||
unicode(self.service or ""),
|
||||
self.node or "",
|
||||
mb_data,
|
||||
data_format.serialise(mb_data),
|
||||
profile=self.blog.profile,
|
||||
)
|
||||
|
||||
|
|
@ -398,9 +387,14 @@ class QuickBlog(EntriesManager, quick_widgets.QuickWidget):
|
|||
)
|
||||
)
|
||||
for result in results:
|
||||
service, node, failure, items, metadata = result
|
||||
service, node, failure, items_data, metadata = result
|
||||
for item_data in items_data:
|
||||
item_data[0] = data_format.deserialise(item_data[0])
|
||||
for item_metadata in item_data[1]:
|
||||
item_metadata[3] = [data_format.deserialise(i) for i in item_metadata[3]]
|
||||
if not failure:
|
||||
self._addMBItemsWithComments((items, metadata), service=jid.JID(service))
|
||||
self._addMBItemsWithComments((items_data, metadata),
|
||||
service=jid.JID(service))
|
||||
|
||||
self.update()
|
||||
if remaining:
|
||||
|
|
|
|||
Loading…
Reference in a new issue