diff browser_side/panels.py @ 132:30d8e328559b

server & browser side: microblogging refactoring first draft - use of new getLastGroupBlogs and getMassiveLastGroupBlogs methods - microblgos browser's cache is temporarily deactivated - last 10 microblogs for everybody are requested on new meta microblog widget
author Goffi <goffi@goffi.org>
date Mon, 02 Apr 2012 00:25:38 +0200
parents ddfcc4cb6cee
children ceef355156de
line wrap: on
line diff
--- a/browser_side/panels.py	Tue Mar 06 09:09:15 2012 +0100
+++ b/browser_side/panels.py	Mon Apr 02 00:25:38 2012 +0200
@@ -97,17 +97,18 @@
             item_type = None
         DOM.eventPreventDefault(event)
         if item_type=="GROUP":
-            _new_panel = MicroblogPanel(self.host, item)
+            _new_panel = MicroblogPanel(self.host, [item])
             _new_panel.setAcceptedGroup(item)
-            self.host.FillMicroblogPanel(_new_panel)
+            self.host.FillMicroblogPanel(_new_panel) #XXX: cache is temporarly deactivated, need to be reworked
         elif item_type=="CONTACT":
             _contact = JID(item)
             self.host.contact_panel.setContactMessageWaiting(_contact.bare, False)
             _new_panel = ChatPanel(self.host, _contact)
             _new_panel.historyPrint()
         elif item_type=="CONTACT_TITLE":
-            _new_panel = MicroblogPanel(self.host, accept_all=True)
-            self.host.FillMicroblogPanel(_new_panel)
+            _new_panel = MicroblogPanel(self.host, [])
+            #self.host.FillMicroblogPanel(_new_panel) #XXX: cache is temporarly deactivated, need to be reworked
+            self.host.bridge.call('getMassiveLastMblogs', _new_panel.massiveInsert, 'ALL', [], 10)
         else:
             return False
         if isinstance(self, LiberviaWidget):
@@ -473,6 +474,15 @@
         #XXX: fixed directly on pyjamas, if the patch is accepted, no need to walk around this
         return AutoCompleteTextBox.complete(self)"""
 
+class MicroblogItem():
+    #XXX: should be moved in a separated module
+
+    def __init__(self, data):
+        self.id = data['id']
+        self.content = data['content']
+        self.author = data['author']
+        self.timestamp = float(data.get('timestamp',0)) #XXX: int doesn't work here
+        
 class MicroblogEntry(SimplePanel):
 
     def __init__(self, host, mblog_entry):
@@ -504,26 +514,44 @@
 
 class MicroblogPanel(LiberviaWidget):
 
-    def __init__(self, host, title='', accept_all=False):
+    def __init__(self, host, accepted_groups):
         """Panel used to show microblog
-        @param title: title of the panel
-        @param accept_all: if true, show every message, without filtering jids"""
-        LiberviaWidget.__init__(self, host, title)
+        @param accepted_groups: groups displayed in this panel, if empty, show all microblogs from all contacts
+        """
+        LiberviaWidget.__init__(self, host, ", ".join(accepted_groups))
         #ScrollPanelWrapper.__init__(self)
         #DropCell.__init__(self)
-        self.accept_all = accept_all
-        self.accepted_groups = []
+        self.accepted_groups = accepted_groups
         self.entries = {}
         self.vpanel = VerticalPanel()
         self.vpanel.setStyleName('microblogPanel')
         self.setWidget(self.vpanel)
 
+    def accept_all(self):
+        return not self.accepted_groups #we accept every microblog only if we are not filtering by groups
+
+    def getEntries(self):
+        """Ask all the entries for the currenly accepted groups,
+        and fill the panel"""
+       
+    def massiveInsert(self, mblogs):
+        """Insert several microblogs at once
+        @param mblogs: dictionary of microblogs, as the result of getMassiveLastGroupBlogs
+        """
+        print "Massive insertion of microblogs"
+        for publisher in mblogs:
+            print "adding blogs for [%s]" % publisher
+            for mblog in mblogs[publisher]:
+                if not mblog.has_key('content'):
+                    print ("WARNING: No content found in microblog [%s]", mblog)
+                    continue
+                mblog_entry = MicroblogItem(mblog)
+                self.addEntry(mblog_entry)
+
     def addEntry(self, mblog_entry):
         """Add an entry to the panel
-        @param text: main text of the entry
-        @param id: unique id of the entry
-        @param author: who wrote the entry
-        @param date: when the entry was written"""
+        @param mblog_entry: MicroblogItem instance
+        """
         if mblog_entry.id in self.entries:
             return
         _entry = MicroblogEntry(self.host, mblog_entry)
@@ -553,7 +581,7 @@
         """Tell if a jid is actepted and show in this panel
         @param jid: jid
         @return: True if the jid is accepted"""
-        if self.accept_all:
+        if self.accept_all():
             return True
         for group in self.accepted_groups:
             if self.host.contact_panel.isContactInGroup(group, jid):