changeset 350:f1b9ec412769

browser_side: MicroblogEntry defines properties to access MicroblogItem attributes (instead of overwriting __getattr__)
author souliane <souliane@mailoo.org>
date Wed, 12 Feb 2014 15:01:33 +0100
parents f488692c4903
children c943fd54c90e
files browser_side/panels.py
diffstat 1 files changed, 7 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/browser_side/panels.py	Wed Feb 12 14:58:11 2014 +0100
+++ b/browser_side/panels.py	Wed Feb 12 15:01:33 2014 +0100
@@ -360,15 +360,17 @@
 
 class MicroblogEntry(SimplePanel, ClickHandler, FocusHandler, KeyboardHandler):
 
-    def __init__(self, blog_panel, data={}):
+    def __init__(self, blog_panel, data):
         """
         @param blog_panel: the parent panel
         @param data: dict containing the blog item data, or a MicroblogItem instance.
         """
-        if isinstance(data, MicroblogItem):
-            self._base_item = data
-        else:
-            self._base_item = MicroblogItem(data)
+        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']:
+            getter = lambda attr: lambda inst: getattr(inst._base_item, attr)
+            setattr(MicroblogEntry, attr, property(getter(attr)))
+
         SimplePanel.__init__(self)
         self._blog_panel = blog_panel
 
@@ -399,13 +401,6 @@
         self.pub_data = (self.hash[0], self.hash[1], self.id)
         self._setContent()
 
-    def __getattr__(self, name):
-        """This allows to directly use the attributes of MicroblogItem"""
-        if hasattr(self, name):
-            return self.__dict__[name]
-        else:
-            return getattr(self._base_item, name)
-
     def _setContent(self):
         """Actually set the entry content (header, icons, bubble...)"""
         self.delete_label = self.update_label = self.comment_label = None