from quixote.session import Session, SessionManager
from quixote.html import htmltext
import users

from storage import StorableObject
import random

class BasicSession(Session, StorableObject):
    _names = 'sessions'

    name_identifier = None
    after_url = None
    anonymous_key = None
    lasso_session_dump = None
    lasso_anonymous_identity_dump = None
    tempfiles = None
    magictokens = None
    message = None

    def has_info(self):
        return self.name_identifier or self.after_url or self.anonymous_key or \
            self.tempfiles or self.lasso_session_dump or self.message or \
            self.lasso_anonymous_identity_dump or \
            self.magictokens or Session.has_info(self)
    is_dirty = has_info

    def get_session_id(self):
        return self.id

    def set_session_id(self, session_id):
        self.id = session_id

    def get_anonymous_key(self, generate = False):
        if self.anonymous_key:
            return self.anonymous_key
        if generate:
            self.anonymous_key = random.randint(0, 1000000000)
        return self.anonymous_key

    session_id = property(get_session_id, set_session_id)

    def display_message(self):
        if not self.message:
            return ''
        s = htmltext('<div class="%snotice">%s</div>' % self.message)
        self.message = None
        return s

    def add_magictoken(self, token, data):
        if not self.magictokens:
            self.magictokens = {}
        self.magictokens[token] = data

    def get_by_magictoken(self, token, default = None):
        if not self.magictokens:
            return default
        return self.magictokens.get(token, default)

    def get_user(self):
        user_id = Session.get_user(self)
        if user_id:
            if str(user_id).startswith('anonymous-'):
                user = users.User()
                user.id = user_id
                user.anonymous = True
                user.name_identifiers = [ self.name_identifier ]
                user.lasso_dump = self.lasso_anonymous_identity_dump
                return user
            else:
                try:
                    return users.User.get(user_id)
                except KeyError:
                    pass
        return None


class StorageSessionManager(SessionManager):
    def __init__(self):
        SessionManager.__init__(self, session_class=BasicSession)

    def forget_changes(self, session):
        pass

    def __getitem__(self, session_id):
        try:
            return BasicSession.get(session_id)
        except KeyError:
            raise KeyError

    def get(self, session_id, default = None):
        try:
            return BasicSession.get(session_id)
        except KeyError:
            return default

    def commit_changes(self, session):
        if session and session.id:
            session.store()
        
    def keys(self):
        return BasicSession.keys()

    def values(self):
        return BasicSession.values()

    def items(self):
        return BasicSession.items()

    def has_key(self, session_id):
        return BasicSession.has_key(session_id)

    def __setitem__(self, session_id, session):
        session.store()

    def __delitem__(self, session_id):
        if not session_id:
            return
        try:
            BasicSession.remove_object(session_id)
        except OSError:
            raise KeyError
