import time
import persistent

import zope.location
from zope.app.container import btree
from zope.session.interfaces import IClientId
from zope.publisher.browser import BrowserView
from zope.session.interfaces import ISession

class ShowSessionInfo(BrowserView):

    def getContainer(self):
        # The container is a session data object
        return ISession(self.request)['simple']

        # The container is a simple folder
        #id = str(IClientId(self.request))
        #if id not in self.context:
        #  self.context[id] = btree.BTreeContainer()
        #return self.context[id]

    def simpleFormDataGet(self):
        # Activate and use for debugging:
        # id = str(IClientId(self.request))
        return repr(self.getContainer()['form-data'])


    def simpleFormDataSet(self):
        # Sleep for a little bit to make this a long-running transaction.
        time.sleep(1)

        container = self.getContainer()

        if 'form-data' not in container:
            fd = FormData()
            container['form-data'] = fd
            return 'New FormData created; ' + str(fd)

        return "FormData found in session.; " + str(container['form-data'])


class FormData(zope.location.Location, persistent.Persistent):

    def __init__(self):
        super(FormData, self).__init__()
        self.arr = persistent.list.PersistentList()
        self.arr.append(int(time.time()))

    def __repr__(self):
        # Returning this representation works:
        #return '<FormData %r %r>' %(self._p_oid, self.arr)

        # Returning this representation fails:
        return str(self.__dict__)

