Mercurial > libervia-backend
diff sat_frontends/quick_frontend/quick_blog.py @ 2624:56f94936df1e
code style reformatting using black
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 27 Jun 2018 20:14:46 +0200 |
parents | 26edcf3a30eb |
children | 003b8b4b56a7 |
line wrap: on
line diff
--- a/sat_frontends/quick_frontend/quick_blog.py Wed Jun 27 07:51:29 2018 +0200 +++ b/sat_frontends/quick_frontend/quick_blog.py Wed Jun 27 20:14:46 2018 +0200 @@ -19,6 +19,7 @@ # from sat.core.i18n import _, D_ from sat.core.log import getLogger + log = getLogger(__name__) @@ -29,8 +30,11 @@ try: # FIXME: to be removed when an acceptable solution is here - unicode('') # XXX: unicode doesn't exist in pyjamas -except (TypeError, AttributeError): # Error raised is not the same depending on pyjsbuild options + unicode("") # XXX: unicode doesn't exist in pyjamas +except ( + TypeError, + AttributeError, +): # Error raised is not the same depending on pyjsbuild options unicode = str ENTRY_CLS = None @@ -45,42 +49,46 @@ @param data(dict): microblog data as return by bridge methods if data values are not defined, set default values """ - self.id = data['id'] - self.title = data.get('title') + self.id = data["id"] + 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.content = data.get('content') + self.title_xhtml = data.get("title_xhtml") + self.tags = list(data_format.dict2iter("tag", data)) + self.content = data.get("content") self.content_rich = None - self.content_xhtml = data.get('content_xhtml') - self.author = data['author'] + self.content_xhtml = data.get("content_xhtml") + self.author = data["author"] try: - author_jid = data['author_jid'] + author_jid = data["author_jid"] self.author_jid = jid.JID(author_jid) if author_jid else None except KeyError: self.author_jid = None try: - self.author_verified = C.bool(data['author_jid_verified']) + self.author_verified = C.bool(data["author_jid_verified"]) except KeyError: self.author_verified = False try: - self.updated = float(data['updated']) # XXX: int doesn't work here (pyjamas bug) + self.updated = float( + data["updated"] + ) # XXX: int doesn't work here (pyjamas bug) except KeyError: self.updated = None try: - self.published = float(data['published']) # XXX: int doesn't work here (pyjamas bug) + self.published = float( + data["published"] + ) # XXX: int doesn't work here (pyjamas bug) except KeyError: self.published = None - self.comments = data.get('comments') + self.comments = data.get("comments") try: - self.comments_service = jid.JID(data['comments_service']) + self.comments_service = jid.JID(data["comments_service"]) except KeyError: self.comments_service = None - self.comments_node = data.get('comments_node') + self.comments_node = data.get("comments_node") # def loadComments(self): # """Load all the comments""" @@ -140,7 +148,16 @@ 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 @@ -178,7 +195,9 @@ """Graphical representation of an Item This class must be overriden by frontends""" - def __init__(self, manager, item_data=None, comments_data=None, service=None, node=None): + def __init__( + self, manager, item_data=None, comments_data=None, service=None, node=None + ): """ @param blog(QuickBlog): the parent QuickBlog @param manager(EntriesManager): the parent EntriesManager @@ -194,7 +213,7 @@ self.blog.id2entries[self.item.id] = self if self.item.comments: node_tuple = (self.item.comments_service, self.item.comments_node) - self.blog.node2entries.setdefault(node_tuple,[]).append(self) + self.blog.node2entries.setdefault(node_tuple, []).append(self) def reset(self, item_data): """Reset the entry with given data @@ -205,17 +224,20 @@ """ if item_data is None: self.new = True - item_data = {'id': None, - # TODO: find a best author value - 'author': self.blog.host.whoami.node - } + item_data = { + "id": None, + # TODO: find a best author value + "author": self.blog.host.whoami.node, + } else: self.new = False self.item = Item(item_data) - self.author_jid = self.blog.host.whoami.bare if self.new else self.item.author_jid + self.author_jid = self.blog.host.whoami.bare if self.new else self.item.author_jid if self.author_jid is None and self.service and self.service.node: self.author_jid = self.service - self.mode = C.ENTRY_MODE_TEXT if self.item.content_xhtml is None else C.ENTRY_MODE_XHTML + self.mode = ( + C.ENTRY_MODE_TEXT if self.item.content_xhtml is None else C.ENTRY_MODE_XHTML + ) def refresh(self): """Refresh the display when data have been modified""" @@ -226,7 +248,7 @@ @param editable(bool): True if the entry can be edited """ - #XXX: we don't use @property as property setter doesn't play well with pyjamas + # XXX: we don't use @property as property setter doesn't play well with pyjamas raise NotImplementedError def addComments(self, comments_data): @@ -249,7 +271,7 @@ # keys to keep other than content*, title* and tag* # FIXME: see how to avoid comments node hijacking (someone could bind his post to another post's comments node) - keys_to_keep = ('id', 'comments', 'author', 'author_jid', 'published') + keys_to_keep = ("id", "comments", "author", "author_jid", "published") mb_data = {} for key in keys_to_keep: @@ -257,14 +279,14 @@ if value is not None: mb_data[key] = unicode(value) - for prefix in ('content', 'title'): - for suffix in ('', '_rich', '_xhtml'): - name = '{}{}'.format(prefix, suffix) + for prefix in ("content", "title"): + for suffix in ("", "_rich", "_xhtml"): + name = "{}{}".format(prefix, suffix) value = getattr(self.item, name) if value is not None: mb_data[name] = value - data_format.iter2dict('tag', self.item.tags, mb_data) + data_format.iter2dict("tag", self.item.tags, mb_data) if self.blog.new_message_target not in (C.PUBLIC, C.GROUP): raise NotImplementedError @@ -273,13 +295,14 @@ mb_data["allow_comments"] = C.BOOL_TRUE if self.blog.new_message_target == C.GROUP: - data_format.iter2dict('group', self.blog.targets, mb_data) + data_format.iter2dict("group", self.blog.targets, mb_data) self.blog.host.bridge.mbSend( - unicode(self.service or ''), - self.node or '', + unicode(self.service or ""), + self.node or "", mb_data, - profile=self.blog.profile) + profile=self.blog.profile, + ) def delete(self): """Remove this Entry from parent manager @@ -288,7 +311,7 @@ all children entries will be recursively removed too """ # XXX: named delete and not remove to avoid conflict with pyjamas - log.debug(u"deleting entry {}".format('EDIT ENTRY' if self.new else self.item.id)) + log.debug(u"deleting entry {}".format("EDIT ENTRY" if self.new else self.item.id)) for child in self.entries: child.delete() try: @@ -303,8 +326,7 @@ # in QuickBlog's dictionary del self.blog.id2entries[self.item.id] if self.item.comments: - comments_tuple = (self.item.comments_service, - self.item.comments_node) + comments_tuple = (self.item.comments_service, self.item.comments_node) other_entries = self.blog.node2entries[comments_tuple].remove(self) if not other_entries: del self.blog.node2entries[comments_tuple] @@ -316,12 +338,20 @@ """ # TODO: manage several comments nodes case. if self.item.comments: - self.blog.host.bridge.psNodeDelete(unicode(self.item.comments_service) or "", self.item.comments_node, profile=self.blog.profile) - self.blog.host.bridge.mbRetract(unicode(self.service or ""), self.node or "", self.item.id, profile=self.blog.profile) + self.blog.host.bridge.psNodeDelete( + unicode(self.item.comments_service) or "", + self.item.comments_node, + profile=self.blog.profile, + ) + self.blog.host.bridge.mbRetract( + unicode(self.service or ""), + self.node or "", + self.item.id, + profile=self.blog.profile, + ) class QuickBlog(EntriesManager, quick_widgets.QuickWidget): - def __init__(self, host, targets, profiles=None): """Panel used to show microblog @@ -330,12 +360,12 @@ to know where to send new messages. """ EntriesManager.__init__(self, None) - self.id2entries = {} # used to find an entry with it's item id - # must be kept up-to-date by Entry - self.node2entries = {} # same as above, values are lists in case of - # two entries link to the same comments node + self.id2entries = {} # used to find an entry with it's item id + # must be kept up-to-date by Entry + self.node2entries = {} # same as above, values are lists in case of + # two entries link to the same comments node if not targets: - targets = () # XXX: we use empty tuple instead of None to workaround a pyjamas bug + targets = () # XXX: we use empty tuple instead of None to workaround a pyjamas bug quick_widgets.QuickWidget.__init__(self, host, targets, C.PROF_KEY_NONE) self._targets_type = C.ALL else: @@ -356,11 +386,17 @@ raise ValueError("Unkown targets type") def __str__(self): - return u"Blog Widget [target: {}, profile: {}]".format(', '.join(self.targets), self.profile) + return u"Blog Widget [target: {}, profile: {}]".format( + ", ".join(self.targets), self.profile + ) def _getResultsCb(self, data, rt_session): remaining, results = data - log.debug("Got {got_len} results, {rem_len} remaining".format(got_len=len(results), rem_len=remaining)) + log.debug( + "Got {got_len} results, {rem_len} remaining".format( + got_len=len(results), rem_len=remaining + ) + ) for result in results: service, node, failure, items, metadata = result if not failure: @@ -378,22 +414,46 @@ @param rt_session(str): session id as returned by mbGetFromMany """ - self.host.bridge.mbGetFromManyWithCommentsRTResult(rt_session, profile=self.profile, - callback=lambda data:self._getResultsCb(data, rt_session), - errback=self._getResultsEb) + self.host.bridge.mbGetFromManyWithCommentsRTResult( + rt_session, + profile=self.profile, + callback=lambda data: self._getResultsCb(data, rt_session), + errback=self._getResultsEb, + ) def getAll(self): """Get all (micro)blogs from self.targets""" + def gotSession(rt_session): self._getResults(rt_session) if self._targets_type in (C.ALL, C.GROUP): targets = tuple(self.targets) if self._targets_type is C.GROUP else () - self.host.bridge.mbGetFromManyWithComments(self._targets_type, targets, 10, 10, {}, {"subscribe":C.BOOL_TRUE}, profile=self.profile, callback=gotSession) + self.host.bridge.mbGetFromManyWithComments( + self._targets_type, + targets, + 10, + 10, + {}, + {"subscribe": C.BOOL_TRUE}, + profile=self.profile, + callback=gotSession, + ) own_pep = self.host.whoami.bare - self.host.bridge.mbGetFromManyWithComments(C.JID, (unicode(own_pep),), 10, 10, {}, {}, profile=self.profile, callback=gotSession) + self.host.bridge.mbGetFromManyWithComments( + C.JID, + (unicode(own_pep),), + 10, + 10, + {}, + {}, + profile=self.profile, + callback=gotSession, + ) else: - raise NotImplementedError(u'{} target type is not managed'.format(self._targets_type)) + raise NotImplementedError( + u"{} target type is not managed".format(self._targets_type) + ) def isJidAccepted(self, jid_): """Tell if a jid is actepted and must be shown in this panel @@ -403,7 +463,7 @@ """ if self._targets_type == C.ALL: return True - assert self._targets_type is C.GROUP # we don't manage other types for now + assert self._targets_type is C.GROUP # we don't manage other types for now for group in self.targets: if self.host.contact_lists[self.profile].isEntityInGroup(jid_, group): return True @@ -422,7 +482,7 @@ @param profile: %(doc_profile)s """ try: - entry = self.id2entries[mb_data['id']] + entry = self.id2entries[mb_data["id"]] except KeyError: # The entry is new try: @@ -430,9 +490,14 @@ except: # The node is unknown, # we need to check that we can accept the entry - if (self.isJidAccepted(service) - or (groups is None and service == self.host.profiles[self.profile].whoami.bare) - or (groups and groups.intersection(self.targets))): + if ( + self.isJidAccepted(service) + or ( + groups is None + and service == self.host.profiles[self.profile].whoami.bare + ) + or (groups and groups.intersection(self.targets)) + ): self.addEntry(mb_data, service=service, node=node) else: # the entry is a comment in a known node