comparison frontends/src/quick_frontend/quick_blog.py @ 1638:9e17690fb187

quick_frontend (quick_blog): retrieve and set the tags from/to microblog data
author souliane <souliane@mailoo.org>
date Sat, 21 Nov 2015 13:58:30 +0100
parents 955221487a3e
children b08b828a87c9
comparison
equal deleted inserted replaced
1637:7751b5a51586 1638:9e17690fb187
39 class Item(object): 39 class Item(object):
40 """Manage all (meta)data of an item""" 40 """Manage all (meta)data of an item"""
41 41
42 def __init__(self, data): 42 def __init__(self, data):
43 """ 43 """
44 @param data(dict, None): microblog data as return by bridge methods 44 @param data(dict): microblog data as return by bridge methods
45 if data is None, set a default values 45 if data values are not defined, set default values
46 """ 46 """
47 self.id = data['id'] 47 self.id = data['id']
48 self.title = data.get('title') 48 self.title = data.get('title')
49 self.title_rich = None 49 self.title_rich = None
50 self.title_xhtml = data.get('title_xhtml') 50 self.title_xhtml = data.get('title_xhtml')
51 self.tags = mbdata2tags(data)
51 self.content = data.get('content') 52 self.content = data.get('content')
52 self.content_rich = None 53 self.content_rich = None
53 self.content_xhtml = data.get('content_xhtml') 54 self.content_xhtml = data.get('content_xhtml')
54 self.author = data['author'] 55 self.author = data['author']
55 try: 56 try:
195 def reset(self, item_data): 196 def reset(self, item_data):
196 """Reset the entry with given data 197 """Reset the entry with given data
197 198
198 used during init (it's a set and not a reset then) 199 used during init (it's a set and not a reset then)
199 or later (e.g. message sent, or cancellation of an edition 200 or later (e.g. message sent, or cancellation of an edition
200 @param idem_data(dict): data as in __init__ 201 @param idem_data(dict, None): data as in __init__
201 """ 202 """
202 if item_data is None: 203 if item_data is None:
203 self.new = True 204 self.new = True
204 item_data = {'id': None, 205 item_data = {'id': None,
205 # TODO: find a best author value 206 # TODO: find a best author value
211 self.author_jid = self.blog.host.whoami.bare if self.new else self.item.author_jid 212 self.author_jid = self.blog.host.whoami.bare if self.new else self.item.author_jid
212 if self.author_jid is None and self.service and self.service.node: 213 if self.author_jid is None and self.service and self.service.node:
213 self.author_jid = self.service 214 self.author_jid = self.service
214 self.mode = C.ENTRY_MODE_TEXT if self.item.content_xhtml is None else C.ENTRY_MODE_XHTML 215 self.mode = C.ENTRY_MODE_TEXT if self.item.content_xhtml is None else C.ENTRY_MODE_XHTML
215 216
216 def refresh(semf): 217 def refresh(self):
217 """Refresh the display when data have been modified""" 218 """Refresh the display when data have been modified"""
218 pass 219 pass
219 220
220 def setEditable(self, editable=True): 221 def setEditable(self, editable=True):
221 """tell if the entry can be edited or not 222 """tell if the entry can be edited or not
241 self.update() 242 self.update()
242 243
243 def send(self): 244 def send(self):
244 """Send entry according to parent QuickBlog configuration and current level""" 245 """Send entry according to parent QuickBlog configuration and current level"""
245 246
246 # keys other to keep other than content* and title* 247 # keys to keep other than content*, title* and tag*
247 keys_to_keep = ('id', 'comments', 'author', 'author_jid', 'published') 248 keys_to_keep = ('id', 'comments', 'author', 'author_jid', 'published')
248 249
249 mb_data = {} 250 mb_data = {}
250 for key in keys_to_keep: 251 for key in keys_to_keep:
251 value = getattr(self.item, key) 252 value = getattr(self.item, key)
256 for suffix in ('', '_rich', '_xhtml'): 257 for suffix in ('', '_rich', '_xhtml'):
257 name = '{}{}'.format(prefix, suffix) 258 name = '{}{}'.format(prefix, suffix)
258 value = getattr(self.item, name) 259 value = getattr(self.item, name)
259 if value is not None: 260 if value is not None:
260 mb_data[name] = value 261 mb_data[name] = value
262
263 mb_data.update(tags2mbdata(self.item.tags))
261 264
262 if self.level == 0: 265 if self.level == 0:
263 if self.blog.new_message_target == C.PUBLIC: 266 if self.blog.new_message_target == C.PUBLIC:
264 if self.new: 267 if self.new:
265 mb_data["allow_comments"] = C.BOOL_TRUE 268 mb_data["allow_comments"] = C.BOOL_TRUE
449 COMMENTS_CLS = cls 452 COMMENTS_CLS = cls
450 else: 453 else:
451 raise ValueError("type_ should be ENTRY or COMMENT") 454 raise ValueError("type_ should be ENTRY or COMMENT")
452 if COMMENTS_CLS is None: 455 if COMMENTS_CLS is None:
453 COMMENTS_CLS = ENTRY_CLS 456 COMMENTS_CLS = ENTRY_CLS
457
458
459 def mbdata2tags(mb_data):
460 """Parse the tags in microblog data.
461
462 @param mb_data (dict): microblog data as return by bridge methods
463 @return list[unicode]
464 """
465 return [tag for key, tag in mb_data.iteritems() if (key == "tag" or key.startswith("tag#")) and tag]
466
467 def tags2mbdata(tags):
468 """Build from the tags a dict using the microblog data format.
469
470 @param tags (list[unicode]): list of tags
471 @return dict
472 """
473 data = {}
474 count = 0
475 for tag in tags:
476 data[('tag#%i' % count) if count else 'tag'] = tag
477 count += 1
478 return data