Author: ianb
Date: 2004-04-21 20:58:59 -0600 (Wed, 21 Apr 2004)
New Revision: 94
Modified:
Component/__init__.py
Log:
Various linking methods
Modified: Component/__init__.py
===================================================================
--- Component/__init__.py 2004-04-22 02:58:46 UTC (rev 93)
+++ Component/__init__.py 2004-04-22 02:58:59 UTC (rev 94)
@@ -1,5 +1,6 @@
from WebKit.Page import Page
from WebKit.HTTPExceptions import *
+from WebUtils import Funcs
class CPage(Page):
@@ -47,6 +48,9 @@
for componentFactory in self.components:
self.addComponent(componentFactory)
+ def actions(self):
+ return []
+
def addComponent(self, componentFactory):
component = componentFactory.componentFor(self)
if not component:
@@ -77,10 +81,14 @@
def awake(self, trans):
Page.awake(self, trans)
+ self._title = self.__class__.__name__
+ self.setView('writeContent')
self.callEvent('awake', trans)
def sleep(self, trans):
self.callEvent('sleep', trans)
+ self._title = None
+ self._view = None
Page.sleep(self, trans)
def runTransaction(self, trans):
@@ -100,7 +108,125 @@
def handleException(self, exc):
pass
+
+ def servletLink(self, name, absolute=False, extraURLPath=None,
+ args=None):
+ """
+ Links to the named servlet; or use 'path/to/servlet'; do not
+ include a leading /
+ """
+ assert not name.startswith('/'), 'The link name must not start with a / (%r)' % name
+ url = ''
+ req = self.request()
+ env = req.environ()
+ if absolute:
+ # @@: Should detect HTTPS
+ url = 'http://%s' % env.get('HTTP_HOST', env['SERVER_NAME'])
+ port = env.get('SERVER_PORT', '80')
+ if port != '80':
+ url = url + ':' + port
+ url = url + req.adapterName() + '/' + name
+ if extraURLPath:
+ if not extraURLPath.startswith('/'):
+ extraURLPath = '/' + extraURLPath
+ url = url + extraURLPath
+ if args:
+ fields = []
+ if isinstance(args, dict):
+ args = args.items()
+ for name, value in args:
+ fields.append('%s=%s' % (self.urlEncode(str(name)),
+ self.urlEncode(str(value))))
+ url = url + '?' + '&'.join(fields)
+ return url
+
+ def linkToSelf(self, absolute=False, extraURLPath=None, args=None):
+ # @@: should test for default context
+ return self.servletLink(
+ self.request().contextName() + '/' + self.__class__.__name__,
+ absolute=absolute,
+ extraURLPath=extraURLPath, args=args)
+
+ ############################################################
+ ## Reorganized action system:
+ ############################################################
+
+ def writeHeader(self):
+ pass
+
+ def writeFooter(self):
+ pass
+
+ def title(self):
+ return self._title
+
+ def viewMethod(self):
+ if type(self._view) in (type(""), type(u"")):
+ return getattr(self, self._view)
+ else:
+ return self._view
+
+ def setView(self, view):
+ self._view = view
+
+ def writeBodyParts(self):
+ self.writeHeader()
+ self.viewMethod()()
+ self.writeFooter()
+ def _respond(self, transaction):
+ """
+ Handles actions if an ``_action_`` or ``_action_XXX``
+ field is defined, otherwise invokes `writeHTML`.
+ Invoked by both `respondToGet` and `respondToPost`.
+
+ Copied with slight modification from Page._respond
+ """
+ req = transaction.request()
+
+ # Support old style actions from 0.5.x and below.
+ # Use setting OldStyleActions in Application.config
+ # to use this.
+ if self.transaction().application().setting('OldStyleActions', ) \
+ and req.hasField('_action_'):
+ action = self.methodNameForAction(req.field('_action_'))
+ actions = self._actionSet()
+ if actions.has_key(action):
+ self.preAction(action)
+ apply(getattr(self, action), (transaction,))
+ self.postAction(action)
+ return
+ else:
+ raise PageError, "Action '%s' is not in the public list of actions, %s, for %s." % (action, actions.keys(), self)
+
+ # Check for actions
+ # @@: This is the only change for CPage:
+ for action in self._actionSet().keys():
+ if req.hasField('_action_%s' % action) or \
+ req.field('_action_', None) == action or \
+ (req.hasField('_action_%s.x' % action) and \
+ req.hasField('_action_%s.y' % action)):
+ self.handleAction(action)
+ return
+
+ self.defaultAction()
+
+ def _actionSet(self):
+ if not hasattr(self, '_actionDict'):
+ self._actionDict = {}
+ for action in self.actions():
+ self._actionDict[action] = 1
+ for component in self._instanceComponents:
+ for action in component.actions():
+ self._actionDict[action] = 1
+ return self._actionDict
+
+ def preAction(self, actionName):
+ pass
+
+ def postAction(self, actionName):
+ pass
+
components = []
class Component:
@@ -174,7 +300,7 @@
def exceptionHTTPNotFoundEvent(self, exc):
self.servlet().write('...')
- # returning 'break' means the exception has been
+ # returning 'break' means the exception has been
# handled and needn't be propagated up:
return 'break'
@@ -214,6 +340,9 @@
if not getattr(servlet, method, None):
setattr(servlet, method, getattr(self, method))
+ def actions(self):
+ return []
+
def servlet(self):
return self._servlet
@@ -242,6 +371,12 @@
return 0
return 1
+ def htmlEncode(self, v):
+ return Funcs.htmlEncode(v)
+
+ def urlEncode(self, v):
+ return Funcs.urlEncode(v)
+
# Some null, standard event handlers:
awakeEvent = None
sleepEvent = None
|