"""
$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/dulcinea/lib/session.py $
$Id: session.py 28122 2006-04-03 12:32:41Z dbinger $
"""
from dulcinea.attachable import Attachable
from dulcinea.user import DulcineaUser
from qp.pub.session import Session


class DulcineaSession (Session, Attachable):

    effective_user_is = DulcineaUser
    owner_is = DulcineaUser

    def __init__(self):
        Session.__init__(self)
        Attachable.__init__(self)

    def clear_app_state(self):
        """Override to clear any session data
        """

# temporary
from durus.persistent import Persistent
DulcineaSessionManager = Persistent

def update_sessions():
    from datetime import datetime
    from dulcinea.common import get_publisher
    publisher = get_publisher()
    connection = get_publisher().get_connection()
    from qp.lib.spec import get_spec_problems
    sessions = connection.get_root()['session_manager'].sessions
    for session in sessions.values():
        tokens = list(session._form_tokens)[-4:]
        tokenset = session.user.get_tokens()
        tokenset.tokens = (tokenset.tokens + tokens)[-16:]
        tokenset._p_note_change()
        del session._form_tokens
        session.owner = session.last_user
        del session.last_user
        session.effective_user = session.user
        del session.user
        session.remote_address = session._remote_address
        del session._remote_address
        session.access_time = datetime.fromtimestamp(session._access_time)
        del session._access_time
        session.authentication_time = datetime.fromtimestamp(session._creation_time)
        del session._creation_time
        del session.id
        del session.actual_user
        problems = get_spec_problems(session)
        if problems:
            for problem in problems:
                print problem
            assert 0
    from durus.btree import BTree
    new_sessions = BTree()
    connection.get_root()['sessions'] = new_sessions
    for key, value in sessions.items():
        new_sessions[key] = value
    del connection.get_root()['session_manager']
    assert 'session_manager' not in connection.get_root().keys()


