From 69e0b1873831fc8031131842615db10acb1c448c Mon Sep 17 00:00:00 2001
From: "M. Ian Graham"
Date: Sat, 5 Apr 2008 01:42:10 +0000
Subject: [PATCH] Landing page improvements
---
direct/src/http/LandingPage.py | 100 +++++++++++++----------------
direct/src/http/LandingPageHTML.py | 77 +++++++++++++++++++++-
direct/src/http/WebRequest.py | 75 ++++++++++++++++------
3 files changed, 176 insertions(+), 76 deletions(-)
diff --git a/direct/src/http/LandingPage.py b/direct/src/http/LandingPage.py
index 68c9056bed..e24d2f8408 100755
--- a/direct/src/http/LandingPage.py
+++ b/direct/src/http/LandingPage.py
@@ -5,82 +5,72 @@ class LandingPage:
self.headerTemplate = LandingPageHTML.header
self.footerTemplate = LandingPageHTML.footer
- self.title = LandingPageHTML.title
- self.contactInfo = LandingPageHTML.contactInfo
+ #self.title = LandingPageHTML.title
+ #self.contactInfo = LandingPageHTML.contactInfo
self.menu = {}
+ self.uriToTitle = {}
+
def addTab(self, title, uri):
self.menu[title] = uri
+ self.uriToTitle[uri] = title
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 += "%s\n" % \
- (self.menu[tab], tab)
- else:
- s += "%s\n" % \
- (self.menu[tab], tab)
- else:
- if tab == activeTab:
- s += "%s\n" % \
- (self.menu[tab], tab)
- else:
- s += "%s\n" % \
- (self.menu[tab], tab)
- tabNum += 1
-
- return s
-
+ return LandingPageHTML.getTabs(self.menu,activeTab)
def getHeader(self, activeTab = "Main"):
- s = self.headerTemplate % {'titlestring' : self.title,
+ s = self.headerTemplate % {'titlestring' : LandingPageHTML.title,
'menustring' : self.getMenu(activeTab)}
return s
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.sort()
- output += "\nServicesURI | Handler |
\n\n"
- output += "\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 += "%s | %s |
\n" % \
- (LandingPageHTML.getRowClassString(rowNum),
- uri,
- uri,
- handlerFunc)
- #handlerFunc)
-
- output += "
\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
- def main(self):
- output = self.getHeader("Main") + "Welcome!
\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()
diff --git a/direct/src/http/LandingPageHTML.py b/direct/src/http/LandingPageHTML.py
index 8db29f203d..517f9f944d 100755
--- a/direct/src/http/LandingPageHTML.py
+++ b/direct/src/http/LandingPageHTML.py
@@ -1,6 +1,7 @@
# -- Text content for the landing page. You should change these for yours! --
title = "Landing Page"
+description = "To set this description, call WebRequestDispatcher.setDescription!
You can also add stats to the table below by calling WebRequestDispatcher.addQuickStat(Name,Value,PositionInTable)."
contactInfo = "M. Ian Graham - ian.graham@dig.com - 818-623-3219"
@@ -269,8 +270,9 @@ header = '''
'''
-mainMenu = '''
-Whee!
+mainPageBody = '''
+%(description)s
+%(quickstats)s
'''
footer = '''
@@ -289,3 +291,74 @@ def getRowClassString(rowNum):
return ""
else:
return " class=\"odd\""
+
+def getURITable(title,uriList,uriToHandler):
+ output = "\n%sURI | Handler |
\n\n" % title
+ output += "\n"
+
+ rowNum = 0
+ for uri in uriList:
+ handlerFunc = str(uriToHandler[uri][0]).split(" ")[2]
+
+ output += "%s | %s |
\n" % \
+ (getRowClassString(rowNum),
+ uri,
+ uri,
+ handlerFunc)
+ rowNum += 1
+
+ output += "
\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 += "%s\n" % \
+ (menu[tab], tab)
+ else:
+ s += "%s\n" % \
+ (menu[tab], tab)
+ else:
+ if tab == activeTab:
+ s += "%s\n" % \
+ (menu[tab], tab)
+ else:
+ s += "%s\n" % \
+ (menu[tab], tab)
+ tabNum += 1
+
+ return s
+
+def getQuickStatsTable(quickStats):
+ output = "\nQuick StatsItem | Value |
\n\n"
+ output += "\n"
+
+ rowNum = 0
+ for item in quickStats[0]:
+ output += "%s | %s |
\n" % \
+ (getRowClassString(rowNum),
+ item,
+ quickStats[1][item])
+ rowNum += 1
+
+ output += "
\n"
+
+ return output
diff --git a/direct/src/http/WebRequest.py b/direct/src/http/WebRequest.py
index 9f1370fdfd..0299d70bdd 100755
--- a/direct/src/http/WebRequest.py
+++ b/direct/src/http/WebRequest.py
@@ -86,13 +86,16 @@ class WebRequestDispatcher(object):
notify = notify
+ quickStats = [["Pages Served"],
+ {"Pages Served" : 0}]
+
def __new__(self, *a, **kw):
obj = object.__new__(self, *a, **kw)
obj.__dict__ = self._shared_state
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):
"""
@@ -117,17 +120,23 @@ class WebRequestDispatcher(object):
Expects to receive a WebRequest object.
"""
assert req.getRequestType() == "GET"
+
+ self.incrementQuickStat("Pages Served")
+
uri = req.getURI()
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:
self.notify.info("%s - %s - %s - 200" % (req.getSourceAddress(), uri, args))
if returnsResponse:
result = apply(callable,(),args)
- req.respond(result)
+ if autoSkin:
+ req.respond(self.landingPage.skin(result,uri))
+ else:
+ req.respond(result)
else:
args["replyTo"] = req
apply(callable,(),args)
@@ -151,7 +160,7 @@ class WebRequestDispatcher(object):
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
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:
self.notify.info("Registered handler %s for URI %s." % (handler,uri))
- self.uriToHandler[uri] = [handler,returnsResponse]
+ self.uriToHandler[uri] = [handler, returnsResponse, autoSkin]
else:
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 --
- def enableLandingPage(self, enable, title):
+ def enableLandingPage(self, enable):
if enable:
if not self.__dict__.has_key("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("Services", "/list")
+ self.landingPage.addTab("Services", "/services")
+ else:
+ self.setTitle(self.__class__.__name__)
else:
self.landingPage = None
self.unregisterGETHandler("/")
- #self.unregisterGETHandler("/main")
- self.unregisterGETHandler("/list")
-
+ self.unregisterGETHandler("/services")
- def _listHandlers(self):
- return self.landingPage.listHandlerPage(self.uriToHandler)
+ 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 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