Leandro Lucarella, el lunes  9 de enero a las 13:25 me escribiste:
> > > And we certainly are! =)
> > > Because he is doing such a great work is why I don't want him wasting his
> > > precious time duplicating the work already done in CP =)
> > 
> > On this you're absolutely right, if something developed inside TG fits
> > better on CP or can lead to improve/fix CP (sessions in this case) it
> > will be a big plus for anyone a not only TG users.
> 
> I'm taking a deeper look at it, and trying to implement a SQLObjectStorage
> for CP sessions (but since I never used CP this could take some time ;)

Well, I'll post this even when it's not even tested (I mean, I didn't
'compiled' it, so it could even have syntax errors :S). It's only 57 lines
of code, so the only point taken with this post is it shouldn't be very
hard to make a CP Session Storage that uses SQLObject.

If is there any interest on it, I could dive into CP to test it and make
it actually work...

-- 
Leandro Lucarella (luca) | Blog colectivo: http://www.mazziblog.com.ar/blog/
 .------------------------------------------------------------------------,
  \  GPG: 5F5A8D05 // F8CD F9A7 BF00 5431 4145  104C 949E BFB6 5F5A 8D05 /
   '--------------------------------------------------------------------'
People love to judge homeless guys.
Like, your giving him money he's just gonna waste it.
He's just gonna waste the money
Well, he lives in a box, what do you want him to do?
Save up and buy a wall unit?
Take a little run to the store for a throw rug and a CD rack?
He's homeless. 
import SQLObject
import StringIO

class TG_Session(InheritableSQLObject):
    class sqlmeta:
        idType = str

    data = StringCol(notNull=True, default='')
    expirationTime = DateTimeCol()

class SQLObectStorage:
    "Implementation of the SQLObject backend for sessions."

    def __init__(self):
        # XXX configure the hub? And what about transactions?
        TG_Session.createTable(ifNotExists=True)
    
    def __del__(self):
        # XXX nothing to see here, move on (unless we use transactions)
        pass
    
    def load(self, id):
        # Select session data from table
        session = TG_Session.get(id)
        # Unpickle data
        f = StringIO.StringIO(session.data)
        data = pickle.load(f)
        return (data, session.expirationTime)
    
    def save(self, id, data, expirationTime):
        # Try to delete session if it was already there
        try:
            session = TG_Session.get(id)
            session.expirationTime = expirationTime
        except SQLObjectNotFound:
            session = TG_Session(id=id, expirationTime=expirationTime)
        # Pickle data
        f = StringIO.StringIO()
        pickle.dump(data, f)
        # Update session data
        session.data = f.getvalue()
    
    def acquireLock(self):
        # XXX SQLObject is thread-safe, right?
        pass
    
    def releaseLock(self):
        # XXX SQLObject is thread-safe, right?
        pass
    
    def cleanUp(self):
        sess = cherrypy.request._session
        now = datetime.datetime.now()
        for s in TG_Session.select(TG_Session.q.expiration_time < now):
            sess.onDeleteSession(s.id)
            s.destroySelf()

Reply via email to