If you benefit from web2py hope you feel encouraged to pay it forward by contributing back to society in whatever form you choose!

This recipe adds urls to a session variable to control if a user visited the page and checks the age of the variable to make sure the value is up to date.


    A session var timeout example for web2py apps
    Copyright (C) 2013 Alan Etkin

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    This license note applies only for the recipe below. For web2py license 
    refer to http://code.google.com/p/web2py


This is the model code:

# default timeout is 2 minutes

import datetime

VISITED_TIMEOUT = datetime.timedelta(0, 0, 0, 0, 2)

if not session.visited:
    session.visited = dict()

session.visited.update({request.url: request.now})

def recently_visited(a=request.application, c, f):
    url = URL(a=a, c=c, f=f)
    if url in session.visited:
        if request.now - session.visited[url] < VISITED_TIMEOUT
            return True
    return False


Now you can use the functions in the controller like this:

# check if a given page was recently visited and return
# a list of all the visited pages
def myaction()
    visited = recently_visited(c="default", f="otheraction")
    return dict(visited=visited, all_visited=session.visited)


Related slices

Comments (0)

Hosting graciously provided by:
Python Anywhere