# HG changeset patch
# User paulb
# Date 1169160007 0
# Node ID 973b3249ebe795a7b0f6b92458fbbc73fd3d21a0
# Parent c0ca666b446de82b9fd74946f8939021dabf70e9
[project @ 2007-01-18 22:40:07 by paulb]
Exposed page contents as attributes.
diff -r c0ca666b446d -r 973b3249ebe7 WebStack/Resources/Login.py
--- a/WebStack/Resources/Login.py Thu Jan 18 19:56:34 2007 +0000
+++ b/WebStack/Resources/Login.py Thu Jan 18 22:40:07 2007 +0000
@@ -41,6 +41,10 @@
The optional 'urlencoding' parameter allows a special encoding to be
used in producing the redirection path.
+
+ To change the pages employed by this resource, either redefine the
+ 'login_page' and 'success_page' attributes in instances of this class or
+ a subclass, or override the 'show_login' and 'show_success' methods.
"""
self.authenticator = authenticator
@@ -84,16 +88,32 @@
def show_login(self, trans, app, path, qs):
"""
- Writes a login screen using the transaction 'trans', including details of the
- 'app', 'path' and 'qs' which the client was attempting to access.
+ Writes a login screen using the transaction 'trans', including details
+ of the 'app', 'path' and 'qs' which the client was attempting to access.
"""
trans.set_content_type(WebStack.Generic.ContentType("text/html", self.encoding))
out = trans.get_response_stream()
- out.write("""
+ out.write(self.login_page % (app, path, qs))
+
+ def show_success(self, trans, app, path, qs):
+
+ """
+ Writes a success screen using the transaction 'trans', including details
+ of the 'app', 'path' and 'qs' which the client was attempting to access.
+ """
+
+ # When authentication fails or is yet to take place, show the login
+ # screen.
+
+ trans.set_content_type(WebStack.Generic.ContentType("text/html", self.encoding))
+ out = trans.get_response_stream()
+ out.write(self.success_page % (app, trans.encode_path(path, self.urlencoding), qs))
+
+ login_page = """
- Login Example
+ Login
Login
@@ -107,16 +127,9 @@
-""" % (app, path, qs))
-
- def show_success(self, trans, app, path, qs):
+"""
- # When authentication fails or is yet to take place, show the login
- # screen.
-
- trans.set_content_type(WebStack.Generic.ContentType("text/html", self.encoding))
- out = trans.get_response_stream()
- out.write("""
+ success_page = """
Login Example
@@ -126,7 +139,7 @@
Please proceed to the application.
-""" % (app, trans.encode_path(path, self.urlencoding), qs))
+"""
class LoginAuthenticator:
diff -r c0ca666b446d -r 973b3249ebe7 WebStack/Resources/LoginRedirect.py
--- a/WebStack/Resources/LoginRedirect.py Thu Jan 18 19:56:34 2007 +0000
+++ b/WebStack/Resources/LoginRedirect.py Thu Jan 18 22:40:07 2007 +0000
@@ -4,7 +4,7 @@
Login redirection resources, sending unauthenticated users to a login screen
URL.
-Copyright (C) 2004, 2005, 2006 Paul Boddie
+Copyright (C) 2004, 2005, 2006, 2007 Paul Boddie
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
@@ -33,7 +33,7 @@
def __init__(self, resource, authenticator, login_url=None, app_url=None,
anonymous_parameter_name=None, anonymous_username="anonymous",
logout_parameter_name=None, logout_url="/", use_logout_redirect=1,
- urlencoding=None):
+ urlencoding=None, path_encoding=None):
"""
Initialise the resource with a 'resource' for the application being
@@ -58,8 +58,13 @@
If the optional 'use_logout_redirect' flag is set to 0, a confirmation
screen is given instead of redirecting the user to the 'logout_url'.
- The optional 'urlencoding' parameter allows a special encoding to be
- used in producing the redirection path.
+ The optional 'path_encoding' parameter (previously 'urlencoding', which
+ is still supported) allows a special encoding to be used in producing
+ the redirection path.
+
+ To change the page used by this resource, either redefine the
+ 'logout_page' attribute in instances of this class or a subclass, or
+ override the 'show_logout' method.
"""
self.login_url = login_url
@@ -71,13 +76,13 @@
self.logout_parameter_name = logout_parameter_name
self.logout_url = logout_url
self.use_logout_redirect = use_logout_redirect
- self.urlencoding = urlencoding or self.encoding
+ self.path_encoding = path_encoding or urlencoding or self.encoding
def respond(self, trans):
"Respond using the given transaction 'trans'."
- fields_path = trans.get_fields_from_path(self.urlencoding)
+ fields_path = trans.get_fields_from_path(self.path_encoding)
# Check for the logout parameter, if appropriate.
@@ -95,7 +100,7 @@
# Show the logout confirmation anyway.
- self._show_logout(trans, self.logout_url)
+ self.show_logout(trans, self.logout_url)
# Check the authentication details with the specified authenticator.
@@ -118,24 +123,41 @@
# Redirect to the login URL.
- path = trans.get_path_without_query(self.urlencoding)
+ path = trans.get_path_without_query(self.path_encoding)
qs = trans.get_query_string()
if qs:
qs = "?" + qs
trans.redirect("%s?app=%s&path=%s&qs=%s" % (
self.get_login_url(trans),
- trans.encode_path(self.get_app_url(trans), self.urlencoding),
- trans.encode_path(path, self.urlencoding),
- trans.encode_path(qs, self.urlencoding))
+ trans.encode_path(self.get_app_url(trans), self.path_encoding),
+ trans.encode_path(path, self.path_encoding),
+ trans.encode_path(qs, self.path_encoding))
)
def get_app_url(self, trans):
+
+ """
+ Return the application URL, using 'trans' if necessary, in order to
+ provide a complete URL to redirect an authenticated user to their
+ originally requested page. If the application URL is empty, any
+ redirects will be within the same application, rather than to
+ potentially completely different applications residing at arbitrary
+ locations.
+ """
+
return self.app_url
def get_login_url(self, trans):
+
+ """
+ Return the login URL, using 'trans' if necessary, in order to
+ provide a complete URL to redirect an authenticated user to their
+ originally requested page.
+ """
+
return self.login_url
- def _show_logout(self, trans, redirect):
+ def show_logout(self, trans, redirect):
"""
Write a confirmation page to 'trans' containing the 'redirect' URL which the
@@ -146,7 +168,9 @@
trans.set_content_type(WebStack.Generic.ContentType("text/html", self.encoding))
out = trans.get_response_stream()
- out.write("""
+ out.write(self.logout_page % redirect)
+
+ logout_page = """
Logout
@@ -156,7 +180,7 @@
Please proceed to the application.
-""" % redirect)
+"""
class LoginRedirectAuthenticator: