A small refactoring for Albatross 1.10 allows an Albatross application to provide its own scheme for mapping a URL to a page module. By overriding the get_page_from_uri() method (pulled in from the RandomPageModuleMixin) in your application class you can decode the URL in any way you choose. This mechanism can potentially be used for all sorts of magical mapping schemes but the most obvious use is to process everything below a certain URL path with the same page module.
The following example is a brief demonstration of how you might write an Albatross application whose only purpose is to publish content based on the requested URL.
Note that the application is not restricted to sending HTML to the browser, the requested URL could just as easily be for an image, CSS stylesheet, etc. See Sending_non-HTML_content for an example.
app.py is the module containing the application class. It also deploys the application as a CGI.
from albatross import RandomModularApp from albatross import cgiapp class App(RandomModularApp): def __init__(self): RandomModularApp.__init__( self, base_url = 'app.py', page_path = '.', start_page = None, secret = '-=secret=-') def get_page_from_uri(self, ctx, uri): ''' Clean up the requested URI a little and then use the 'content' page module to process all requests. ''' # Let the mixin do all the hard work content_path = RandomModularApp.get_page_from_uri(self, ctx, uri) if content_path is None: content_path = '' # Removing slashes from start and end content_path = content_path.strip('/') # Record in the locals ctx.locals.content_path = content_path # Always process with the content 'page' return 'content' app = App() app.run(cgiapp.Request())
Points of interest:
- start_page = None. For this application the start_page is completely irrelevant as all URLs are mapped to the same page module.
- get_page_from_uri(). I think the comments explain it all but all the method is doing is cleaning up the URI path to make sure it looks nice and passing it through to content.py in ctx.locals.content_path.
This is the where any database or file system access would be performed to turn content_path into real data for display. Purely so the function does something interesting, it builds a breadcrumb trail and uses the last part of the path as the content or 'root'.
1 def page_display(ctx): 2 ''' 3 Decides what to display based on ctx.locals.content_path and renders 4 the content using the content.html template. 5 ''' 6 7 # Break the path into parts 8 parts = ctx.locals.content_path.split('/') 9 10 # Build a breadcrumb trail, you would probably turn the path's parts 11 # into something a bit more meaningful. 12 breadcrumbs =  13 for i in range(len(parts)): 14 breadcrumbs.append((parts[i].capitalize(), '/'.join(parts[:i+1]))) 15 ctx.locals.breadcrumbs = breadcrumbs 16 17 # Load the content 18 ctx.locals.content = 'Content for %s' % (parts[-1] or 'root') 19 20 ctx.run_template('content.html')
<html> <head> <title> Albatross Demo: RandomPageModuleMixin.get_page_from_uri() </title> </head> <body> <p> You are here: <al-a href="">Home</al-a> <al-for iter="crumb" expr="breadcrumbs"> :: <al-a expr="crumb.value()"> <al-value expr="crumb.value()" /> </al-a> </al-for> </p> <p> <strong>requested content path:</strong> <al-value expr="content_path" /> </p> <p> <strong>content:</strong> <al-value expr="content" /> </p> </body> </html>