Browse Source

Landing page improvements

M. Ian Graham 17 years ago
parent
commit
69e0b18738
3 changed files with 175 additions and 75 deletions
  1. 45 55
      direct/src/http/LandingPage.py
  2. 75 2
      direct/src/http/LandingPageHTML.py
  3. 55 18
      direct/src/http/WebRequest.py

+ 45 - 55
direct/src/http/LandingPage.py

@@ -5,82 +5,72 @@ class LandingPage:
         self.headerTemplate = LandingPageHTML.header
         self.headerTemplate = LandingPageHTML.header
         self.footerTemplate = LandingPageHTML.footer
         self.footerTemplate = LandingPageHTML.footer
         
         
-        self.title = LandingPageHTML.title
-        self.contactInfo = LandingPageHTML.contactInfo
+        #self.title = LandingPageHTML.title
+        #self.contactInfo = LandingPageHTML.contactInfo
 
 
         self.menu = {}
         self.menu = {}
 
 
+        self.uriToTitle = {}
+
     def addTab(self, title, uri):
     def addTab(self, title, uri):
         self.menu[title] = uri
         self.menu[title] = uri
+        self.uriToTitle[uri] = title
 
 
     def getMenu(self, activeTab):
     def getMenu(self, activeTab):
-        tabList = self.menu.keys()
-        if "Main" in tabList:
-            tabList.remove("Main")
-        
-        tabList.sort()
-        if "Main" in self.menu.keys():
-            tabList.insert(0, "Main")
-
-        s = ""
-        tabNum = 0
-
-        for tab in tabList:
-            if tabNum == 0:
-                if tab == activeTab:
-                    s += "<li id=\"active\" class=\"first\"><a href=\"%s\" id=\"current\">%s</a></li>\n" % \
-                         (self.menu[tab], tab)
-                else:
-                    s += "<li class=\"first\"><a href=\"%s\">%s</a></li>\n" % \
-                         (self.menu[tab], tab)
-            else:
-                if tab == activeTab:
-                    s += "<li id=\"active\"><a href=\"%s\" id=\"current\">%s</a></li>\n" % \
-                         (self.menu[tab], tab)
-                else:
-                    s += "<li><a href=\"%s\">%s</a></li>\n" % \
-                         (self.menu[tab], tab)
-            tabNum += 1
-
-        return s
-                
+        return LandingPageHTML.getTabs(self.menu,activeTab)
 
 
     def getHeader(self, activeTab = "Main"):
     def getHeader(self, activeTab = "Main"):
-        s = self.headerTemplate % {'titlestring' : self.title,
+        s = self.headerTemplate % {'titlestring' : LandingPageHTML.title,
                                    'menustring' : self.getMenu(activeTab)}
                                    'menustring' : self.getMenu(activeTab)}
         return s
         return s
         
         
 
 
     def getFooter(self):
     def getFooter(self):
-        return self.footerTemplate % {'contact' : self.contactInfo}
-
-    def listHandlerPage(self, uriToHandler):
-        output = self.getHeader("Services")
+        return self.footerTemplate % {'contact' : LandingPageHTML.contactInfo}
 
 
+    def getServicesPage(self, uriToHandler):
+        output = ""
+        
         uriList = uriToHandler.keys()
         uriList = uriToHandler.keys()
+
         uriList.sort()
         uriList.sort()
 
 
-        output += "<table>\n<caption>Services</caption><thead><tr><th scope=col>URI</th><th scope=col>Handler</th></tr></thead>\n\n"
-        output += "<tbody>\n"
+        autoList = []
 
 
-        rowNum = 0
-        for uri in uriList:
-            rowNum += 1
-            handlerFunc = str(uriToHandler[uri][0]).split(" ")[2]
+        if "/" in uriList:
+            uriList.remove("/")
+            autoList.append("/")
+        if "/services" in uriList:
+            uriList.remove("/services")
+            autoList.append("/services")
 
 
-            output += "<tr%s><td><a href=%s>%s</a></td><td>%s</td></tr>\n" % \
-                      (LandingPageHTML.getRowClassString(rowNum),
-                       uri,
-                       uri,
-                       handlerFunc)
-                       #handlerFunc)
-            
-        output += "</tbody></table>\n"
+        output += LandingPageHTML.getURITable(title="Application",uriList=uriList,uriToHandler=uriToHandler)
 
 
-        output = output + self.getFooter()
+        output += LandingPageHTML.getURITable(title="Admin",uriList=autoList,uriToHandler=uriToHandler)
+        
         return output
         return output
 
 
-    def main(self):
-        output = self.getHeader("Main") + "<P>Welcome!</P>\n" + self.getFooter()
-        return output
+    def populateMainPage(self, body):
+        LandingPageHTML.mainPageBody = body
+
+    def setTitle(self, title):
+        LandingPageHTML.title = title
+
+    def setDescription(self,desc):
+        LandingPageHTML.description = desc
+
+    def setContactInfo(self,info):
+        LandingPageHTML.contactInfo = info
+
+    def getDescription(self):
+        return LandingPageHTML.description
+
+    def getMainPage(self):
+        return LandingPageHTML.mainPageBody
+
+    def getQuickStatsTable(self, quickStats):
+        return LandingPageHTML.getQuickStatsTable(quickStats)
 
 
+    def skin(self, body, uri):
+        title = self.uriToTitle.get(uri,"Services")
+        return self.getHeader(title) + body + self.getFooter()

+ 75 - 2
direct/src/http/LandingPageHTML.py

@@ -1,6 +1,7 @@
 # -- Text content for the landing page.  You should change these for yours! --
 # -- Text content for the landing page.  You should change these for yours! --
 
 
 title = "Landing Page"
 title = "Landing Page"
+description = "To set this description, call WebRequestDispatcher.setDescription!<BR><BR>You can also add stats to the table below by calling WebRequestDispatcher.addQuickStat(Name,Value,PositionInTable)."
 contactInfo = "M. Ian Graham - [email protected] - 818-623-3219"
 contactInfo = "M. Ian Graham - [email protected] - 818-623-3219"
 
 
 
 
@@ -269,8 +270,9 @@ header = '''
 <center>
 <center>
 '''
 '''
 
 
-mainMenu = '''
-<p>Whee!</p>
+mainPageBody = '''
+<P>%(description)s</P>
+<P>%(quickstats)s</P>
 '''
 '''
 
 
 footer = '''
 footer = '''
@@ -289,3 +291,74 @@ def getRowClassString(rowNum):
         return ""
         return ""
     else:
     else:
         return " class=\"odd\""
         return " class=\"odd\""
+
+def getURITable(title,uriList,uriToHandler):
+    output = "<P><table>\n<caption>%s</caption><thead><tr><th scope=col>URI</th><th scope=col>Handler</th></tr></thead>\n\n" % title
+    output += "<tbody>\n"
+
+    rowNum = 0
+    for uri in uriList:
+        handlerFunc = str(uriToHandler[uri][0]).split(" ")[2]
+
+        output += "<tr%s><td><a href=%s>%s</a></td><td>%s</td></tr>\n" % \
+                  (getRowClassString(rowNum),
+                   uri,
+                   uri,
+                   handlerFunc)
+        rowNum += 1
+            
+    output += "</tbody></table></P>\n"
+
+    return output
+
+def getTabs(menu,activeTab):
+    tabList = menu.keys()
+    if "Main" in tabList:
+        tabList.remove("Main")
+    if "Services" in tabList:
+        tabList.remove("Services")
+        
+    tabList.sort()
+    
+    if "Main" in menu.keys():
+        tabList.insert(0, "Main")
+    if "Services" in menu.keys():
+        tabList.insert(1, "Services")
+
+    s = ""
+    tabNum = 0
+
+    for tab in tabList:
+        if tabNum == 0:
+            if tab == activeTab:
+                s += "<li id=\"active\" class=\"first\"><a href=\"%s\" id=\"current\">%s</a></li>\n" % \
+                     (menu[tab], tab)
+            else:
+                s += "<li class=\"first\"><a href=\"%s\">%s</a></li>\n" % \
+                     (menu[tab], tab)
+        else:
+            if tab == activeTab:
+                s += "<li id=\"active\"><a href=\"%s\" id=\"current\">%s</a></li>\n" % \
+                     (menu[tab], tab)
+            else:
+                s += "<li><a href=\"%s\">%s</a></li>\n" % \
+                     (menu[tab], tab)
+        tabNum += 1
+
+    return s    
+
+def getQuickStatsTable(quickStats):
+    output = "<P><table>\n<caption>Quick Stats</caption><thead><tr><th scope=col>Item</th><th scope=col>Value</th></tr></thead>\n\n"
+    output += "<tbody>\n"
+
+    rowNum = 0
+    for item in quickStats[0]:
+        output += "<tr%s><td>%s</td><td>%s</td></tr>\n" % \
+                  (getRowClassString(rowNum),
+                   item,
+                   quickStats[1][item])
+        rowNum += 1
+            
+    output += "</tbody></table></P>\n"
+
+    return output        

+ 55 - 18
direct/src/http/WebRequest.py

@@ -86,13 +86,16 @@ class WebRequestDispatcher(object):
     
     
     notify = notify
     notify = notify
 
 
+    quickStats = [["Pages Served"],
+                  {"Pages Served" : 0}]
+
     def __new__(self, *a, **kw):
     def __new__(self, *a, **kw):
         obj = object.__new__(self, *a, **kw)
         obj = object.__new__(self, *a, **kw)
         obj.__dict__ = self._shared_state
         obj.__dict__ = self._shared_state
         return obj
         return obj
 
 
-    def __init__(self, wantLandingPage = True, landingPageTitle = None):
-        self.enableLandingPage(wantLandingPage, landingPageTitle)
+    def __init__(self, wantLandingPage = True):
+        self.enableLandingPage(wantLandingPage)
 
 
     def listenOnPort(self,listenPort):
     def listenOnPort(self,listenPort):
         """
         """
@@ -117,17 +120,23 @@ class WebRequestDispatcher(object):
         Expects to receive a WebRequest object.
         Expects to receive a WebRequest object.
         """
         """
         assert req.getRequestType() == "GET"
         assert req.getRequestType() == "GET"
+
+        self.incrementQuickStat("Pages Served")
+        
         uri = req.getURI()
         uri = req.getURI()
         args = req.dictFromGET()
         args = req.dictFromGET()
         
         
-        callable,returnsResponse = self.uriToHandler.get(uri, [self.invalidURI,False])
+        callable,returnsResponse,autoSkin = self.uriToHandler.get(uri, [self.invalidURI,False,False])
 
 
         if callable != self.invalidURI:
         if callable != self.invalidURI:
             self.notify.info("%s - %s - %s - 200" % (req.getSourceAddress(), uri, args))
             self.notify.info("%s - %s - %s - 200" % (req.getSourceAddress(), uri, args))
         
         
         if returnsResponse:
         if returnsResponse:
             result = apply(callable,(),args)
             result = apply(callable,(),args)
-            req.respond(result)
+            if autoSkin:
+                req.respond(self.landingPage.skin(result,uri))
+            else:
+                req.respond(result)
         else:
         else:
             args["replyTo"] = req
             args["replyTo"] = req
             apply(callable,(),args)
             apply(callable,(),args)
@@ -151,7 +160,7 @@ class WebRequestDispatcher(object):
             request = HttpRequest.HttpManagerGetARequest()
             request = HttpRequest.HttpManagerGetARequest()
 
 
 
 
-    def registerGETHandler(self,uri,handler,returnsResponse=False):
+    def registerGETHandler(self,uri,handler,returnsResponse=False, autoSkin=False):
         """
         """
         Call this function to register a handler function to
         Call this function to register a handler function to
         be called in response to a query to the given URI.
         be called in response to a query to the given URI.
@@ -177,7 +186,7 @@ class WebRequestDispatcher(object):
 
 
         if self.uriToHandler.get(uri,None) is None:
         if self.uriToHandler.get(uri,None) is None:
             self.notify.info("Registered handler %s for URI %s." % (handler,uri))
             self.notify.info("Registered handler %s for URI %s." % (handler,uri))
-            self.uriToHandler[uri] = [handler,returnsResponse]
+            self.uriToHandler[uri] = [handler, returnsResponse, autoSkin]
         else:
         else:
             self.notify.warning("Attempting to register a duplicate handler for URI %s.  Ignoring." % uri)
             self.notify.warning("Attempting to register a duplicate handler for URI %s.  Ignoring." % uri)
 
 
@@ -203,25 +212,53 @@ class WebRequestDispatcher(object):
 
 
     # -- Landing page convenience functions --
     # -- Landing page convenience functions --
 
 
-    def enableLandingPage(self, enable, title):
+    def enableLandingPage(self, enable):
         if enable:
         if enable:
             if not self.__dict__.has_key("landingPage"):
             if not self.__dict__.has_key("landingPage"):
                 self.landingPage = LandingPage()
                 self.landingPage = LandingPage()
-                if title is None:
-                    title = self.__class__.__name__
-                self.landingPage.title = title
-                self.registerGETHandler("/", self.landingPage.main, returnsResponse = True)
-                #self.registerGETHandler("/main", self.landingPage.main, returnsResponse = True)
-                self.registerGETHandler("/list", self._listHandlers, returnsResponse = True)
+                self.setTitle(self.__class__.__name__)
+                self.registerGETHandler("/", self._main, returnsResponse = True, autoSkin = True)
+                self.registerGETHandler("/services", self._services, returnsResponse = True, autoSkin = True)
                 self.landingPage.addTab("Main", "/")
                 self.landingPage.addTab("Main", "/")
-                self.landingPage.addTab("Services", "/list")
+                self.landingPage.addTab("Services", "/services")
+            else:
+                self.setTitle(self.__class__.__name__)
         else:
         else:
             self.landingPage = None
             self.landingPage = None
             self.unregisterGETHandler("/")
             self.unregisterGETHandler("/")
-            #self.unregisterGETHandler("/main")
-            self.unregisterGETHandler("/list")
+            self.unregisterGETHandler("/services")
+
         
         
+    def _main(self):
+        return self.landingPage.getMainPage() % {"description" : self.landingPage.getDescription(),
+                                                 "quickstats" : self.landingPage.getQuickStatsTable(self.quickStats)}
+
+    def _services(self):
+        return self.landingPage.getServicesPage(self.uriToHandler)
 
 
-    def _listHandlers(self):
-        return self.landingPage.listHandlerPage(self.uriToHandler)
+    def setTitle(self,title):
+        self.landingPage.setTitle(title)
+
+    def setDescription(self,desc):
+        self.landingPage.setDescription(desc)
+
+    def setContactInfo(self,info):
+        self.landingPage.setContactInfo(info)
+
+    def addQuickStat(self,item,value,position):
+        if item in self.quickStats[1]:
+            self.notify.warning("Ignoring duplicate addition of quickstat %s." % item)
+            return
+                                
+        self.quickStats[0].insert(position,item)
+        self.quickStats[1][item] = value
         
         
+    def updateQuickStat(self,item,value):
+        assert item in self.quickStats[1]
+
+        self.quickStats[1][item] = value
+
+    def incrementQuickStat(self,item):
+        assert item in self.quickStats[1]
+
+        self.quickStats[1][item] += 1