[albatross-users] Session management how-to?

Andrew McNamara andrewm at object-craft.com.au
Thu Jun 20 13:41:55 EST 2002


>Yes it did.  Thanks much.  I don't quite have the intuitions for
>the Albatross process model down yet.  Your message, notes,
>comments are helping quite a bit.  I've got my simple stuff
>working.  Tomorrow, I'm on to something a bit more complex and
>challenging.

Shout if you get stuck.

>I'm also writing up a few notes, as I learn things, on how-to do
>things in Albatross.  I've attached what I've done so far below. 
>This is preliminary, needless to say; I'm still learning Albatross. 
>But, if you you have any comments, I'm interested.  And, if you
>should feel that this would be of use to other Albatross users
>(after a bit more work, of course), let me know.

This is one of the areas in which our documentation is week - introducing
new users to the "albatross way". I've always found this one of the hardest
areas of documentation: you need to know a system thoroughly to be able to
document it, but you need to be able to think like someone who is
approaching it for the first time.

Chapter 4, "Guide to Building Applications" is probably the chapter that
comes closest to covering the information you were looking for.

I like your How-to - we may be able to put it up on our web site
(appropriately credited, of course) if you would like?

>=============================================================
>
>
>
>                       Albatross How-to for Beginners
>                                      
>   Contents:
>     * Introduction
>     * How-to Structure an Object-based Application
>     * How-to Pass a Value to the Presentation
>     * How-to Pass a Value to the Controller
>     * How-to Manage Sessions
>       
>   Back to top
>     _________________________________________________________________
>   
>Introduction
>
>   This document explains how to do a number of simple tasks with
>   Albatross. It is hoped that it will get a beginning Albatross user
>   past a number of early questions and problems so that s/he can move on
>   to more complex tasks.
>   Back to top
>     _________________________________________________________________
>   
>How-to Structure an Object-based Application
>
>   This section describes the structure of an object-based application.
>   It can be used as a "template" for creating an application.
>   
>   An object-based application is one
>    1. Create the presentation -- Create an HTML file for each page. This
>       is the Albatross template file.
>    2. Create the controller -- Create a Python script. This script will
>       contain the following items:
>    3. Import the needed Albatross classes. For an object-based
>       application, the following are appropriate:
>        from albatross import SimpleSessionApp, SessionAppContext
>    4. Define a class for each page in the application. Here is a sample:
>        class Page2:
>            name = 'page2'
>            def page_process(self, ctx):
>                if ctx.req_equals('button_1'):
>                    if ctx.get_value('next_page') == 'page3':
>                        ctx.set_page('page3')
>                o
>                o
>                o
>            def page_enter(self, ctx):
>                o
>                o
>                o
>            def page_leave(self, ctx):
>                o
>                o
>                o
>            def page_display(self, ctx):
>                o
>                o
>                o
>                ctx.run_template('page2.html')
>    5. Define a context class as a subclass of SessionAppContext. Here is
>       a sample:
>        class AppContext(SessionAppContext):
>            def __init__(self, app):
>                SessionAppContext.__init__(self, app)
>    6. Define an application class as a subclass of SimpleSessionApp. The
>       constructor should call the constructor of the superclass and
>       register each page (class) in the application. Also provide a
>       create_context method that creates and returns an instance of your
>       context class. Here is an example:
>        class App(SimpleSessionApp):
>            def __init__(self):
>                SimpleSessionApp.__init__(self,
>                    base_url = 'dispatch.py',
>                    template_path = '.',
>                    start_page = 'page1',
>                    secret = 'tomato',
>                    session_appid = 'sampleapp')
>                for page_class in (Page1, Page2, Page3):
>                    self.register_page(page_class.name, page_class())
>            def create_context(self):
>                return AppContext(self)
>    7. Create the "main" script. Here is a sample:
>        if __name__ == '__main__':
>            app = App()
>            app.run(Request())
>       
>   Back to top
>     _________________________________________________________________
>   
>How-to Pass a Value to the Presentation
>
>   This technique enables you to pass a value from the controller (the
>   Python script) to the presentation (the HTML file/template).
>    1. In the controller (i.e. in the Python script), add the value to
>       the the locals in the context. Here is a sample of the
>       page_display method in the class defined for the page:
>class Page1:
>    name = 'page1'
>    def page_display(self, ctx):
>        ctx.locals.variable_1 = 'a sample value'
>        ctx.locals.variable_2 = 'another value'
>        ctx.run_template('page1.html')
>    2. In the presentation (i.e. the HTML page), use an Albatross tag to
>       "capture" the value from the context. Here is a sample tag:
>    <p>Value #1: <al-input type="TEXT" name="variable_1"></p>
>    <p>Value #2: <al-value expr="variable_2"></p>
>       
>   Back to top
>     _________________________________________________________________
>   
>How-to Pass a Value to the Controller
>
>   This technique enables you to capture input from the presentation
>   (i.e. from the end-user at the Web browser) and pass those values back
>   to the controller (the Python script).
>    1. Add "input" items to the presentation (the HTML template):
>    <p>Enter your name: <al-input type="TEXT" name="user_name"></p>
>    <p>Enter your name: <input type="TEXT" name="user_phone"></p>
>       Note that the first item (user_name) allows you to pass a value to
       the presentation from the controller. The second item (user_phone)
>       does not.
>    2. Capture the entered values in the controller. Here is a sample
>       page_display method:
>class Page2:
>    name = 'page1'
>    def page_display(self, ctx):
>        ctx.run_template('page1.html')
>        user_name = ctx.locals.user_name
>        user_phone = ctx.locals.user_phone
>        # Do something with user_name and user_phone.
>        o
>        o
>        o
>       
>   Back to top
>     _________________________________________________________________
>   
>How-to Manage Sessions
>
>    1. How-to create a session -- Albatross does this for you
>       automatically. If the request from the browser contains
>       information about an existing session, that session is used,
>       otherwise Albatross creates a new session.
>    2. How-to remove a session -- You will need to explicitly do this.
>       (Albatross does not do this automatically.) If you have a page
>       from which the application can go no further, you could remove the
>       session on entering the page (the user will never leave the page -
>       but reloading the page will take them back to the beginning). Here
>       is an example:
>        class FinalPage:
>            def page_enter(self, ctx):
>                ctx.remove_session()
>       Or, if the final page of your application contains a "Logout" or
>       "Exit" button, you can remove the session in the page_process
>       method of the class for that page like this:
>        class FinalPage:
>            def page_process(self, ctx):
>                # Check for the logout button.
>                if ctx.req_equals('logout'):
>                    ctx.remove_session()
>                    # Send the user back to the login page.
>                    ctx.set_page('login')
>    3. How-to save session state across pages within the application --
>       In order to direct Albatross to do this, you will "register" the
>       variables that you want saved. Do this by calling
>       add_session_vars, passing the names of the variables to be saved.
>       Here is an example:
>        class Page1:
>            def page_enter(self, ctx):
>                ctx.add_session_vars('user_name', 'user_phone')
>       
>   Additional notes on session management:
>     * In order to run a session based application, you must start-up an
>       Albatross session server. The session-server sub-directory in the
>       Albatross distribution, contains an implementation of a session
>       server (al-session-daemon).
>     * If your application inherits it context and application classes
>       from SimpleSessionApp and SessionAppContext then Albatross stores
>       and saves session information in files in the local file system.
>     * In contrast, if your application inherits it context and
>       application classes from SimpleSessionFileApp and
>       SessionFileAppContext then Albatross stores and saves session
>       information in the session server, i.e. in memory in the session
>       server daemon. Note that if you change from memory-based session
>       storage to file-based storage, you will need to modify the call to
>       the application super class (). Add the session_dir keyword
>       argument, with the directory (a string) where you want Albatross
>       to store session state.
>       
>     _________________________________________________________________
>   
>-- 
>Dave Kuhlman
>dkuhlman at rexx.com
>http://www.rexx.com/~dkuhlman
>_______________________________________________
>Albatross-users mailing list
>Albatross-users at object-craft.com.au
>https://www.object-craft.com.au/cgi-bin/mailman/listinfo/albatross-users
-- 
Andrew McNamara, Senior Developer, Object Craft
http://www.object-craft.com.au/



More information about the Albatross-users mailing list