I've written this from web.session.DBStore, it uses web.ctx.orm from
http://webpy.org/cookbook/sqlalchemy
I described Session as Base delivered class, but when i found that
cleanup needs to delete with where statement, i replaced it with just
sqlalchemy table.

Please can you check if its correct, because I'm kinda new to
SQLAlchemy.

class SQLAStore(web.session.Store):
    """Store for saving a session in database
    Needs a table as defined below:
        session_data = Table('session_data', Base.metadata,
            Column('session_id', String(128), primary_key=True),
            Column('atime', DateTime, default=func.current_timestamp
()),
            Column('data', Text)
        )
    Uses web.ctx.orm and Base from http://webpy.org/cookbook/sqlalchemy
    """
    def __init__(self, table):
        self.table = table

    def __contains__(self, key):
        return bool(web.ctx.orm.execute(self.table.select
(self.table.c.session_id==key)).fetchone())

    def __getitem__(self, key):
        s = web.ctx.orm.execute(self.table.select
(self.table.c.session_id==key)).fetchone()
        if s is None:
            raise KeyError
        else:
            web.ctx.orm.execute(self.table.update().values
(atime=datetime.datetime.now()).where(self.table.c.session_id==key))
            return self.decode(s[self.table.c.data])

    def __setitem__(self, key, value):
        pickled = self.encode(value)
        if key in self:
                web.ctx.orm.execute(self.table.update().values
(data=pickled).where(self.table.c.session_id==key))
        else:
            web.ctx.orm.execute(self.table.insert().values
(session_id=key, data=pickled))

    def __delitem__(self, key):
        web.ctx.orm.execute(self.table.delete
(self.table.c.session_id==key))

    def cleanup(self, timeout):
        timeout = datetime.timedelta(timeout/(24.0*60*60)) #timedelta
takes numdays as arg
        last_allowed_time = datetime.datetime.now() - timeout
        web.ctx.orm.execute(self.table.delete
(self.table.c.atime<last_allowed_time))

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web.py" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/webpy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to