Skip to main content

An implementation of a zope session, using beaker.

Project description

Session

Configuration

>>> from zope.component import getUtility, queryUtility
>>> from dolmen.beaker.interfaces import ISessionConfig
>>> session_config = queryUtility(ISessionConfig)
>>> session_config.get('key')
'beaker.session.id'

Initialisation

dolmen.beaker initialize the beaker session when the traversing starts (traversing the IRootFolder object):

>>> from zope.event import notify
>>> import grokcore.component as grok
>>> from zope.publisher.browser import TestRequest
>>> from zope.publisher.interfaces.http import IHTTPRequest
>>> from zope.traversing.interfaces import BeforeTraverseEvent

First we create an instance of the Request:

>>> request = TestRequest()
>>> IHTTPRequest.providedBy(request)
True

We initalize our Session with the help of the BeforeTraverseEvent. This event will make an instance of a Session Object in the Request:

>>> from zope.component.hooks import getSite
>>> site = getSite()
>>> notify(BeforeTraverseEvent(site, request))

We can get the session object using two available adapters:

>>> from dolmen.beaker.interfaces import ISession
>>> session = ISession(request)
>>> session.__class__
<class 'beaker.session.SessionObject'>

Let’s assign an value to our session and save it:

>>> session['foo'] = 'bar'
>>> session.save()
>>> session['foo']
'bar'

A new request will not have access to the session without traversing:

>>> newrequest = TestRequest()
>>> newrequest.response._cookies
{}

>>> session = ISession(newrequest)
>>> session['foo']
Traceback (most recent call last):
...
KeyError: 'foo'

closure and invalidation

>>> cookie = request.response._cookies
>>> cookie
{}
>>> from zope.publisher.interfaces import EndRequestEvent
>>> notify(EndRequestEvent(site, request))
>>> cookie = request.response._cookies
>>> cookie
{'beaker.session.id': {'path': '/', 'samesite': 'Lax', 'value': '...'}}

Invalidating

>>> import hamcrest
>>> session = ISession(request)
>>> print(session['foo'])
bar
>>> session.invalidate() # Or destroy, to get rid of everything
>>> session['foo']
Traceback (most recent call last):
...
KeyError: 'foo'

The Zope Session adapter

>>> from zope.session.interfaces import ISession as IZopeSession
>>> request = TestRequest()
>>> notify(BeforeTraverseEvent(site, request))
>>> zsession = IZopeSession(request)
>>> print(zsession)
<dolmen.beaker.session.ZopeSession object at ...>
>>> from zope.interface.verify import verifyObject
>>> verifyObject(IZopeSession, zsession)
True
>>> data = zsession['my_package']
>>> print(data)
<dolmen.beaker.session.NamespaceSessionData object at ...>
>>> data['someitem'] = 'test'
>>> print(data['someitem'])
test
>>> data = zsession['some.other.package']
>>> data['info'] = 'Grok !'

Changelog

0.4 (2020-10-07)

  • Updated MANIFEST

0.3 (2020-04-07)

  • Corrected deprecated import and package structure.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dolmen.beaker-0.4.tar.gz (8.1 kB view details)

Uploaded Source

File details

Details for the file dolmen.beaker-0.4.tar.gz.

File metadata

  • Download URL: dolmen.beaker-0.4.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/3.6

File hashes

Hashes for dolmen.beaker-0.4.tar.gz
Algorithm Hash digest
SHA256 d071d1d68467e6fcdf8f0417bf31c7dd116b40c33a301d1d74c78ce58d0e5954
MD5 cb9a7c8986cc6cdd242054b7fda321d5
BLAKE2b-256 f23bcdd89ff75e49acad03c3f9f488f89fb4ece05922607cd4c5fb17d65c9703

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page