changeset 4:7325e787c22b

personalEvent management signal first draft, microblogs are now put in a central grid
author Goffi <goffi@goffi.org>
date Sun, 13 Feb 2011 16:20:22 +0100
parents 154d4caa57f4
children c8d3821efc36
files libervia.py
diffstat 1 files changed, 47 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/libervia.py	Sun Feb 13 16:16:40 2011 +0100
+++ b/libervia.py	Sun Feb 13 16:20:22 2011 +0100
@@ -24,6 +24,8 @@
 from pyjamas.ui.RootPanel import RootPanel
 from pyjamas.ui.VerticalPanel import VerticalPanel
 from pyjamas.ui.HorizontalPanel import HorizontalPanel
+from pyjamas.ui.HTMLPanel import HTMLPanel
+from pyjamas.ui.Grid import Grid
 from pyjamas.ui.Label import Label
 from pyjamas.ui import HasAlignment
 from pyjamas.ui.MenuBar import MenuBar
@@ -34,6 +36,7 @@
 from register import RegisterPanel, RegisterBox
 from pyjamas import DOM
 from contact import ContactPanel
+from datetime import datetime
 
 
 class LiberviaJsonProxy(JSONProxy):
@@ -157,14 +160,18 @@
 
 class MicroblogEntry(SimplePanel):
 
-    def __init__(self, text):
+    def __init__(self, body, author, timestamp):
         SimplePanel.__init__(self)
-        self._vPanel = VerticalPanel()
-        self._vPanel.setStyleName('microblogEntry')
-        _label = Label(text)
-        self._vPanel.add(_label)
-        self.add(self._vPanel)
+
+        _datetime = datetime.fromtimestamp(timestamp)
 
+        panel = HTMLPanel("<div class='mb_entry_author'>%(author)s on <span class='mb_entry_timestamp'>%(timestamp)s</span></div><p class='mb_entry_body'>%(body)s</p>" %
+            {"author": author,
+            "timestamp": _datetime,
+            "body": body}
+            )
+        panel.setStyleName('microblogEntry')
+        self.add(panel)
 
 
 class MicroblogPanel(VerticalPanel):
@@ -173,31 +180,33 @@
         VerticalPanel.__init__(self)
         self.setStyleName('microblogPanel')
 
-    def addEntry(self, text, author=None, date=None):
+    def addEntry(self, text, author=None, timestamp=None):
         """Add an entry to the panel
         @param text: main text of the entry
         @param author: who wrote the entry
         @param date: when the entry was written"""
-        _entry = MicroblogEntry(text)
+        _entry = MicroblogEntry(text, author, timestamp)
         self.add(_entry)
 
 class MiddlePannel(HorizontalPanel):
     
-    def __init__(self,host):
+    def __init__(self, host):
         self.host=host
         HorizontalPanel.__init__(self)
         self._left = self.host.contactPanel
-        self._right = HorizontalPanel()
+        self._right = Grid(1,3)
         self._right.setWidth('100%')
         self._right.setHeight('100%')
-        test = MicroblogPanel()
-        self._right.add(test)
-        test.addEntry("test entry")
         self.add(self._left)
         self.setCellWidth(self._left, "15%")
         self.add(self._right)
         self.setCellWidth(self._right, "85%")
 
+    def changePanel(self, idx, panel):
+        print "panel:",panel
+        print "idx:",idx
+        self._right.setWidget(0,idx,panel)
+
 class MainPanel(VerticalPanel):
 
     def __init__(self, host):
@@ -209,19 +218,19 @@
 
         menu = Menu()
         magic_box = host.magicBox
-        middle_panel = MiddlePannel(self.host)
-        middle_panel.setWidth('100%')
+        self.middle_panel = MiddlePannel(self.host)
+        self.middle_panel.setWidth('100%')
 
         self.add(menu)
         self.add(magic_box)
-        self.add(middle_panel)
+        self.add(self.middle_panel)
         
         self.setCellHeight(menu, "5%")
         self.setCellHeight(magic_box, "5%")
         self.setCellVerticalAlignment(magic_box, HasAlignment.ALIGN_CENTER)
         self.setCellHorizontalAlignment(magic_box, HasAlignment.ALIGN_CENTER)
-        self.setCellHeight(middle_panel, "90%")
-        self.setCellWidth(middle_panel, "100%")
+        self.setCellHeight(self.middle_panel, "90%")
+        self.setCellWidth(self.middle_panel, "100%")
 
         self.setWidth("100%")
         self.setHeight("100%")
@@ -231,6 +240,9 @@
         self.magicBox = MagicBox()
         self.contactPanel = ContactPanel(self)
         self.panel = MainPanel(self)
+        self.middle_panel = self.panel.middle_panel
+        self.mpanels = [MicroblogPanel()]
+        self.middle_panel.changePanel(1,self.mpanels[0])
         self._dialog = None
         RootPanel().add(self.panel)
         self._register = RegisterCall()
@@ -271,7 +283,24 @@
         bridge_signals = BridgeSignals()
         bridge_signals.call('getSignals', self._getSignalsCB)
         print('Got signal ==> name: %s, params: %s' % (signal_data[0],signal_data[1]))
+        name,args = signal_data
+        if name == 'personalEvent':
+            self._personalEventCb(*args)
 
+    ## Signals callbacks ##
+
+    def _personalEventCb(self, sender, event_type, data, profile):
+        if event_type == "MICROBLOG":
+            if not data.has_key('content'):
+                print ("WARNING: No content found in microblog data")
+                return
+            for panel in self.mpanels:
+                if isinstance(panel,MicroblogPanel):
+                    content = data['content']
+                    author = data.get('author')
+                    print "timestamp: %s" % data.get('timestamp')
+                    timestamp = float(data.get('timestamp'),0) #XXX: int doesn't work here
+                    panel.addEntry(content, author, timestamp)
 
 if __name__ == '__main__':
     pyjd.setup("http://localhost:8080/libervia.html")