changeset 365:a74a2dfbe4f5

browser_side: groupblog: handle main items with no associated comments node + do not mix up attributes names between the item own service/node and the comments service/node
author souliane <souliane@mailoo.org>
date Thu, 20 Feb 2014 19:33:31 +0100
parents 4cf735b40304
children 39ae5bf92786
files browser_side/panels.py
diffstat 1 files changed, 29 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/browser_side/panels.py	Wed Feb 19 17:52:39 2014 +0100
+++ b/browser_side/panels.py	Thu Feb 20 19:33:31 2014 +0100
@@ -366,22 +366,12 @@
         self.content_xhtml = data.get('content_xhtml', '')
         self.author = data['author']
         self.updated = float(data.get('updated', 0))  # XXX: int doesn't work here
-        try:
-            self.published = float(data['published'])  # XXX: int doesn't work here
-        except KeyError:
-            self.published = self.updated
+        self.published = float(data.get('published', self.updated))  # XXX: int doesn't work here
+        self.service = data.get('service', '')
+        self.node = data.get('node', '')
         self.comments = data.get('comments', False)
-        if self.empty and self.type == 'main_item':
-            self.service = self.node = None
-            self.hash = (self.service, self.node)
-        else:
-            try:
-                self.service = data['comments_service'] if self.comments else data['service']
-                self.node = data['comments_node'] if self.comments else data['node']
-                self.hash = (self.service, self.node)
-            except KeyError:
-                print "Warning: can't manage item [%s] some keys are missing in microblog data (%s)" % (self.id, str(data))
-                self.comments = False
+        self.comments_service = data.get('comments_service', '')
+        self.comments_node = data.get('comments_node', '')
 
 
 class MicroblogEntry(SimplePanel, ClickHandler, FocusHandler, KeyboardHandler):
@@ -393,7 +383,8 @@
         """
         self._base_item = data if isinstance(data, MicroblogItem) else MicroblogItem(data)
         for attr in ['id', 'type', 'empty', 'title', 'title_xhtml', 'content', 'content_xhtml',
-                     'author', 'updated', 'published', 'comments', 'service', 'node', 'hash']:
+                     'author', 'updated', 'published', 'comments', 'service', 'node',
+                     'comments_service', 'comments_node']:
             getter = lambda attr: lambda inst: getattr(inst._base_item, attr)
             setattr(MicroblogEntry, attr, property(getter(attr)))
 
@@ -424,7 +415,7 @@
         ClickHandler.__init__(self)
         self.addClickListener(self)
 
-        self.pub_data = (self.hash[0], self.hash[1], self.id)
+        self.__pub_data = (self.service, self.node, self.id)
         self.__setContent()
 
     def __setContent(self):
@@ -501,7 +492,7 @@
             else:
                 self._blog_panel.host.bridge.call('sendMblogComment', None, self._parent_entry.comments, content['text'], extra)
         else:
-            self._blog_panel.host.bridge.call('updateMblog', None, self.pub_data, self.comments, content['text'], extra)
+            self._blog_panel.host.bridge.call('updateMblog', None, self.__pub_data, self.comments, content['text'], extra)
         return True
 
     def __afterEditCb(self, content):
@@ -543,7 +534,7 @@
         @return: False if the deletion has been cancelled."""
         def confirm_cb(answer):
             if answer:
-                self._blog_panel.host.bridge.call('deleteMblog', None, self.pub_data, self.comments)
+                self._blog_panel.host.bridge.call('deleteMblog', None, self.__pub_data, self.comments)
             else:  # restore the text if it has been emptied during the edition
                 self.bubble.setContent(self.bubble._original_content)
 
@@ -567,10 +558,13 @@
                 'new': True,
                 'type': 'comment',
                 'author': self._blog_panel.host.whoami.bare,
-                'service': self.service,
-                'node': self.node
+                'service': self.comments_service,
+                'node': self.comments_node
                 }
         entry = self._blog_panel.addEntry(data)
+        if entry is None:
+            print "The entry of id %s can not be commented" % self.id
+            return
         entry._parent_entry = self
         self._current_comment = entry
         entry.bubble.edit(True)
@@ -678,10 +672,10 @@
     def onTextEntered(self, text):
         if self.selected_entry:
             # we are entering a comment
-            comments_node = self.selected_entry.comments
-            if not comments_node:
-                raise Exception("ERROR: comments node is empty")
-            target = ("COMMENT", comments_node)
+            comments_url = self.selected_entry.comments
+            if not comments_url:
+                raise Exception("ERROR: the comments URL is empty")
+            target = ("COMMENT", comments_url)
         elif not self._accepted_groups:
             # we are entering a public microblog
             target = ("PUBLIC", None)
@@ -754,10 +748,11 @@
         """
         _entry = MicroblogEntry(self, data)
         if _entry.type == "comment":
-            if not _entry.hash in self.comments:
+            comments_hash = (_entry.service, _entry.node)
+            if not comments_hash in self.comments:
                 # The comments node is not known in this panel
                 return None
-            parent = self.comments[_entry.hash]
+            parent = self.comments[comments_hash]
             parent_idx = self.vpanel.getWidgetIndex(parent)
             # we find or create the panel where the comment must be inserted
             try:
@@ -771,7 +766,7 @@
                 self.vpanel.insert(sub_panel, parent_idx + 1)
             for idx in xrange(0, len(sub_panel.getChildren())):
                 comment = sub_panel.getIndexedChild(idx)
-                if comment.pub_data[2] == _entry.id:
+                if comment.id == _entry.id:
                     # update an existing comment
                     sub_panel.remove(comment)
                     sub_panel.insert(_entry, idx)
@@ -789,9 +784,10 @@
         self.entries[_entry.id] = _entry
 
         if _entry.comments:
-            # entry has comments, we keep the comment node as a reference
-            self.comments[_entry.hash] = _entry
-            self.host.bridge.call('getMblogComments', self.mblogsInsert, _entry.service, _entry.node)
+            # entry has comments, we keep the comments service/node as a reference
+            comments_hash = (_entry.comments_service, _entry.comments_node)
+            self.comments[comments_hash] = _entry
+            self.host.bridge.call('getMblogComments', self.mblogsInsert, _entry.comments_service, _entry.comments_node)
 
         return _entry
 
@@ -802,7 +798,7 @@
         """
         for child in self.vpanel.getChildren():
             if isinstance(child, MicroblogEntry) and type_ == 'main_item':
-                if child.pub_data[2] == id_:
+                if child.id == id_:
                     main_idx = self.vpanel.getWidgetIndex(child)
                     try:
                         sub_panel = self.vpanel.getWidget(main_idx + 1)
@@ -815,7 +811,7 @@
                     break
             elif isinstance(child, VerticalPanel) and type_ == 'comment':
                 for comment in child.getChildren():
-                    if comment.pub_data[2] == id_:
+                    if comment.id == id_:
                         comment.removeFromParent()
                         self.selected_entry = None
                         break