From 97bc2d063d40b9c9bef2c750379b62d620ae6ecc Mon Sep 17 00:00:00 2001
From: Luke Leighton <lkcl@lkcl.net>
Date: Mon, 13 Sep 2010 21:13:39 +0100
Subject: [PATCH 30/34] add patient inbox panel

---
 gnumed/gnumed/client/ProxiedWeb/gui/InboxGrid.py   |   12 ++
 .../client/ProxiedWeb/gui/PatientInboxPanel.py     |   86 ++++++++++++
 .../client/ProxiedWeb/gui/PatientsummaryPanel.py   |    2 +
 .../client/ProxiedWeb/gui/ProviderInboxPanel.py    |   12 +-
 .../client/ProxiedWeb/gui/public/gnumedweb.xml     |  140 +++++++++++++++-----
 5 files changed, 211 insertions(+), 41 deletions(-)
 create mode 100644 gnumed/gnumed/client/ProxiedWeb/gui/InboxGrid.py
 create mode 100644 gnumed/gnumed/client/ProxiedWeb/gui/PatientInboxPanel.py

diff --git a/gnumed/gnumed/client/ProxiedWeb/gui/InboxGrid.py b/gnumed/gnumed/client/ProxiedWeb/gui/InboxGrid.py
new file mode 100644
index 0000000..24da7f0
--- /dev/null
+++ b/gnumed/gnumed/client/ProxiedWeb/gui/InboxGrid.py
@@ -0,0 +1,12 @@
+def fill_grid_messages(grid, messages):
+    """ fills in inbox messages into a grid
+    """
+
+    grid = self.pp.grid
+    grid.resize(1+len(messages), 5)
+    for (row, item) in enumerate(messages):
+        grid.setHTML(row+1, 1, str(item.received_when))
+        grid.setHTML(row+1, 2, item.category)
+        grid.setHTML(row+1, 3, item.type)
+        grid.setHTML(row+1, 4, item.comment)
+
diff --git a/gnumed/gnumed/client/ProxiedWeb/gui/PatientInboxPanel.py b/gnumed/gnumed/client/ProxiedWeb/gui/PatientInboxPanel.py
new file mode 100644
index 0000000..d03b3d0
--- /dev/null
+++ b/gnumed/gnumed/client/ProxiedWeb/gui/PatientInboxPanel.py
@@ -0,0 +1,86 @@
+from pyjamas.builder.Builder import Builder, HTTPUILoader
+from pyjamas.ui.SimplePanel import SimplePanel
+from InboxGrid import fill_grid_messages
+import GMWevents
+import Remote
+
+#======================================================
+class cPatientInboxPanel(SimplePanel): 
+    """ Patient Inbox Messages.
+
+        derives from SimplePanel so that it can be added immediately to
+        cPatientsummaryPanel and then, when HTTPUILoader picks up the XML
+        file (asynchronously), the PatientInbox instance can be added then.
+    """
+    def __init__(self, **kwargs):
+        SimplePanel.__init__(self, **kwargs)
+        self.pp = None
+        self.active_patient_messages = []
+        self.active_patient_id = None
+        self.TEXT_WAITING = "Waiting for response..."
+        HTTPUILoader(self).load("gnumedweb.xml") # calls onUILoaded when done
+
+    #--------------------------------------------------
+    def onUILoaded(self, text):
+        self.b = Builder(text)
+        self.pp = self.b.createInstance("PatientInbox", self)
+        GMWevents.events.addPatientSelectedListener(self)
+        GMWevents.events.addLoginListener(self)
+        self.add(self.pp)
+
+    #--------------------------------------------------
+    def getPanel(self):
+        return self.pp
+
+    #--------------------------------------------------
+    def fill_grid(self):
+        fill_grid_messages(self.pp.grid, self.active_patient_messages)
+                
+    #--------------------------------------------------
+    def onPatientSelected(self, sender, patient_info):
+        if self.logged_in and patient_info is not None:
+            self.active_patient_id = patient_info.pk_identity
+            self.active_patient_messages = None
+            Remote.svc.get_patient_messages(self.active_patient_id, self)
+        else:
+            self.active_patient_id = None
+            self.active_patient_messages = []
+        print "patient id", self.active_patient_id
+        self.fill_grid()
+
+    #--------------------------------------------------
+    def onLogin(self, sender, username, logged_in):
+        self.logged_in = logged_in
+
+    #--------------------------------------------------
+    def onRemoteResponse(self, response, request_info):
+        method = request_info.method
+
+        if method == 'get_provider_inbox_data':
+            self.messages = response
+            self.fill_grid()
+        elif method == 'get_patient_messages':
+            self.active_patient_messages = response
+            self.fill_grid()
+
+    #--------------------------------------------------
+    def onRemoteError(self, code, errobj, request_info):
+        # onRemoteError gets the HTTP error code or 0 and
+        # errobj is an jsonrpc 2.0 error dict:
+        #     {
+        #       'code': jsonrpc-error-code (integer) ,
+        #       'message': jsonrpc-error-message (string) ,
+        #       'data' : extra-error-data
+        #     }
+        message = errobj['message']
+        if code != 0:
+            self.setStatus("HTTP error %d: %s" % 
+                                (code, message))
+        else:
+            code = errobj['code']
+            if message['message'] == 'Cannot request login parameters.':
+                self.setStatus("You need to log in first")
+            else:
+                self.setStatus("JSONRPC Error %s: %s" %
+                                (code, message))
+
diff --git a/gnumed/gnumed/client/ProxiedWeb/gui/PatientsummaryPanel.py b/gnumed/gnumed/client/ProxiedWeb/gui/PatientsummaryPanel.py
index 9fdaf7d..1baf2b0 100644
--- a/gnumed/gnumed/client/ProxiedWeb/gui/PatientsummaryPanel.py
+++ b/gnumed/gnumed/client/ProxiedWeb/gui/PatientsummaryPanel.py
@@ -2,12 +2,14 @@ from pyjamas.ui.StackPanel import StackPanel
 from pyjamas.ui.HTML import HTML
 
 from ProviderInboxPanel import cProviderInboxPanel
+from PatientInboxPanel import cPatientInboxPanel
 
 class cPatientsummaryPanel(StackPanel):
     def __init__(self):
         StackPanel.__init__(self, Width="100%", Height="300px")
 
         self.add(cProviderInboxPanel(), "Inbox")
+        self.add(cPatientInboxPanel(), "Patient Messages")
         self.add(HTML('The<br>early<br>bird<br>catches<br>the<br>worm.'),
                   "Waiting list")
         self.add(HTML('The smart money<br>is on the<br>black horse.'),
diff --git a/gnumed/gnumed/client/ProxiedWeb/gui/ProviderInboxPanel.py b/gnumed/gnumed/client/ProxiedWeb/gui/ProviderInboxPanel.py
index ed89df2..4cc9020 100644
--- a/gnumed/gnumed/client/ProxiedWeb/gui/ProviderInboxPanel.py
+++ b/gnumed/gnumed/client/ProxiedWeb/gui/ProviderInboxPanel.py
@@ -1,8 +1,10 @@
 from pyjamas.builder.Builder import Builder, HTTPUILoader
 from pyjamas.ui.SimplePanel import SimplePanel
+from InboxGrid import fill_grid_messages
 import GMWevents
 import Remote
 
+
 #======================================================
 class cProviderInboxPanel(SimplePanel): 
     """ Provider Inbox Messages.
@@ -19,6 +21,7 @@ class cProviderInboxPanel(SimplePanel):
         self.TEXT_WAITING = "Waiting for response..."
         HTTPUILoader(self).load("gnumedweb.xml") # calls onUILoaded when done
 
+    #--------------------------------------------------
     def onUILoaded(self, text):
         self.b = Builder(text)
         self.pp = self.b.createInstance("ProviderInbox", self)
@@ -26,6 +29,7 @@ class cProviderInboxPanel(SimplePanel):
         GMWevents.events.addLoginListener(self)
         self.add(self.pp)
 
+    #--------------------------------------------------
     def getPanel(self):
         return self.pp
 
@@ -40,13 +44,7 @@ class cProviderInboxPanel(SimplePanel):
         else:
             messages = self.messages
                 
-        grid = self.pp.grid
-        grid.resize(1+len(messages), 5)
-        for (row, item) in enumerate(messages):
-            grid.setHTML(row+1, 1, str(item.received_when))
-            grid.setHTML(row+1, 2, item.category)
-            grid.setHTML(row+1, 3, item.type)
-            grid.setHTML(row+1, 4, item.comment)
+        fill_grid_messages(self.pp.grid, messages)
 
     #--------------------------------------------------
     def onAllMessagesSelected(self, sender):
diff --git a/gnumed/gnumed/client/ProxiedWeb/gui/public/gnumedweb.xml b/gnumed/gnumed/client/ProxiedWeb/gui/public/gnumedweb.xml
index d4864d7..744866d 100644
--- a/gnumed/gnumed/client/ProxiedWeb/gui/public/gnumedweb.xml
+++ b/gnumed/gnumed/client/ProxiedWeb/gui/public/gnumedweb.xml
@@ -143,7 +143,7 @@
 			</component>
 		</components>
 	</component>
-	<component id="ProviderInbox" name="DockPanel" type="Panel" index="None" left="252" top="370" visible="True">
+	<component id="PatientInbox" name="DockPanel" type="Panel" index="None" left="252" top="370" visible="True">
 		<properties name="elements">
 		</properties>
 		<properties name="layout">
@@ -153,68 +153,95 @@
 			<property name="border" value="1" />
 		</properties>
 		<properties name="common">
-			<property name="name" value="ProviderInbox" />
+			<property name="name" value="PatientInbox" />
 		</properties>
 		<components>
-			<component id="None" index="(1, 'south')">
-			</component>
-			<component id="None" index="(2, 'west')">
-			</component>
-			<component id="None" index="(3, 'east')">
-			</component>
 			<component id="lbTitle" name="HTML" type="Widget" index="(0, 'north')">
 				<properties name="widget">
-					<property name="text" value="Provider Inbox" />
+					<property name="text" value="Patient Inbox" />
 				</properties>
 				<properties name="common">
 					<property name="name" value="lbTitle" />
 				</properties>
 			</component>
-			<component id="hpShow" name="HorizontalPanel" type="Panel" index="(5, 'north')">
+			<component id="None" index="(1, 'south')">
+			</component>
+			<component id="None" index="(2, 'west')">
+			</component>
+			<component id="None" index="(3, 'east')">
+			</component>
+			<component id="grid" name="Grid" type="Panel" index="(4, 'center')">
 				<properties name="elements">
 				</properties>
-				<properties name="layout">
-					<property name="spacing" value="0" />
-					<property name="padding" value="0" />
-				</properties>
 				<properties name="common">
-					<property name="name" value="hpShow" />
+					<property name="name" value="grid" />
 				</properties>
 				<components>
-					<component id="HTML2" name="HTML" type="Widget" index="0">
+					<component id="None" index="(0, 0)">
+					</component>
+					<component id="date" name="HTML" type="Widget" index="(0, 1)">
 						<properties name="widget">
-							<property name="text" value="Show messages:" />
+							<property name="text" value="date" />
 						</properties>
 						<properties name="common">
-							<property name="name" value="HTML2" />
+							<property name="name" value="date" />
 						</properties>
 					</component>
-					<component id="rdbAll" name="RadioButton" type="Widget" index="1">
+					<component id="category" name="HTML" type="Widget" index="(0, 2)">
 						<properties name="widget">
-							<property name="group" value="messages" />
-							<property name="label" value="all" />
+							<property name="text" value="category" />
 						</properties>
 						<properties name="common">
-							<property name="name" value="rdbAll" />
-						</properties>
-						<properties name="events">
-							<property name="onClick" value="onAllMessagesSelected" />
+							<property name="name" value="category" />
 						</properties>
 					</component>
-					<component id="rdbActive" name="RadioButton" type="Widget" index="2">
+					<component id="type" name="HTML" type="Widget" index="(0, 3)">
 						<properties name="widget">
-							<property name="group" value="messages" />
-							<property name="label" value="Active-patient related" />
+							<property name="text" value="type" />
 						</properties>
 						<properties name="common">
-							<property name="name" value="rdbActive" />
+							<property name="name" value="type" />
 						</properties>
-						<properties name="events">
-							<property name="onClick" value="onPatientMessagesSelected" />
+					</component>
+					<component id="message" name="HTML" type="Widget" index="(0, 4)">
+						<properties name="widget">
+							<property name="text" value="message" />
+						</properties>
+						<properties name="common">
+							<property name="name" value="message" />
 						</properties>
 					</component>
 				</components>
 			</component>
+		</components>
+	</component>
+	<component id="ProviderInbox" name="DockPanel" type="Panel" index="None" left="357" top="405" visible="True">
+		<properties name="elements">
+		</properties>
+		<properties name="layout">
+			<property name="spacing" value="0" />
+			<property name="padding" value="4" />
+			<property name="width" value="100%" />
+			<property name="border" value="1" />
+		</properties>
+		<properties name="common">
+			<property name="name" value="ProviderInbox" />
+		</properties>
+		<components>
+			<component id="lbTitle" name="HTML" type="Widget" index="(0, 'north')">
+				<properties name="widget">
+					<property name="text" value="Provider Inbox" />
+				</properties>
+				<properties name="common">
+					<property name="name" value="lbTitle" />
+				</properties>
+			</component>
+			<component id="None" index="(1, 'south')">
+			</component>
+			<component id="None" index="(2, 'west')">
+			</component>
+			<component id="None" index="(3, 'east')">
+			</component>
 			<component id="grid" name="Grid" type="Panel" index="(4, 'center')">
 				<properties name="elements">
 				</properties>
@@ -240,6 +267,14 @@
 							<property name="name" value="category" />
 						</properties>
 					</component>
+					<component id="type" name="HTML" type="Widget" index="(0, 3)">
+						<properties name="widget">
+							<property name="text" value="type" />
+						</properties>
+						<properties name="common">
+							<property name="name" value="type" />
+						</properties>
+					</component>
 					<component id="message" name="HTML" type="Widget" index="(0, 4)">
 						<properties name="widget">
 							<property name="text" value="message" />
@@ -248,12 +283,49 @@
 							<property name="name" value="message" />
 						</properties>
 					</component>
-					<component id="type" name="HTML" type="Widget" index="(0, 3)">
+				</components>
+			</component>
+			<component id="hpShow" name="HorizontalPanel" type="Panel" index="(5, 'north')">
+				<properties name="elements">
+				</properties>
+				<properties name="layout">
+					<property name="spacing" value="0" />
+					<property name="padding" value="0" />
+				</properties>
+				<properties name="common">
+					<property name="name" value="hpShow" />
+				</properties>
+				<components>
+					<component id="HTML2" name="HTML" type="Widget" index="0">
 						<properties name="widget">
-							<property name="text" value="type" />
+							<property name="text" value="Show messages:" />
 						</properties>
 						<properties name="common">
-							<property name="name" value="type" />
+							<property name="name" value="HTML2" />
+						</properties>
+					</component>
+					<component id="rdbAll" name="RadioButton" type="Widget" index="1">
+						<properties name="widget">
+							<property name="group" value="messages" />
+							<property name="label" value="all" />
+						</properties>
+						<properties name="common">
+							<property name="name" value="rdbAll" />
+						</properties>
+						<properties name="events">
+							<property name="onClick" value="onAllMessagesSelected" />
+						</properties>
+					</component>
+					<component id="rdbActive" name="RadioButton" type="Widget" index="2">
+						<properties name="widget">
+							<property name="group" value="messages" />
+							<property name="label" value="Active-patient related" />
+						</properties>
+						<properties name="common">
+							<property name="name" value="rdbActive" />
+						</properties>
+						<properties name="events">
+							<property name="onClick" value="onPatientMessagesSelected" />
 						</properties>
 					</component>
 				</components>
-- 
1.7.1

