Skip to main content

Database-backed sessions for managing user state across requests.

Project description

plain.sessions

Database-backed sessions for managing user state across requests.

Overview

The plain.sessions package provides database-backed session management for Plain applications. Sessions allow you to store and retrieve arbitrary data on a per-visitor basis, using a session key stored in a cookie.

Sessions are implemented as a dictionary-like object that automatically handles persistence to the database.

Basic usage

Sessions are automatically available on request objects when the middleware is installed. You can use request.session like a standard Python dictionary:

from plain.views import View

class MyView(View):
    def get(self):
        # Store values in the session
        self.request.session['username'] = 'jane'
        self.request.session['cart_items'] = [1, 2, 3]

        # Retrieve values from the session
        username = self.request.session.get('username')
        cart_items = self.request.session.get('cart_items', [])

        # Check if a key exists
        if 'username' in self.request.session:
            # User has a session
            pass

        # Delete values from the session
        del self.request.session['cart_items']

The session data is automatically saved when you set or delete values. Sessions are stored in the database using the Session model.

Session configuration

Sessions can be configured through various settings:

# Cookie name (default: "sessionid")
SESSION_COOKIE_NAME = "sessionid"

# Age of cookie in seconds (default: 2 weeks)
SESSION_COOKIE_AGE = 60 * 60 * 24 * 7 * 2

# Domain for session cookie (None for standard domain cookie)
SESSION_COOKIE_DOMAIN = None

# Whether the session cookie should be secure (https:// only)
SESSION_COOKIE_SECURE = True

# The path of the session cookie
SESSION_COOKIE_PATH = "/"

# Whether to use the HttpOnly flag
SESSION_COOKIE_HTTPONLY = True

# Whether to set the flag restricting cookie leaks on cross-site requests
# Can be 'Lax', 'Strict', 'None', or False
SESSION_COOKIE_SAMESITE = "Lax"

# Whether to save the session data on every request
SESSION_SAVE_EVERY_REQUEST = False

# Whether a user's session cookie expires when the browser is closed
SESSION_EXPIRE_AT_BROWSER_CLOSE = False

Session management

The SessionStore class provides additional methods for managing sessions:

Flushing sessions

To completely remove the current session data and regenerate the session key:

# Delete all session data and get a new session key
request.session.flush()

Cycling session keys

To create a new session key while retaining the current session data (useful for security purposes):

# Keep the data but change the session key
request.session.cycle_key()

Checking if session is empty

if request.session.is_empty():
    # No session data exists
    pass

Admin interface

The package includes an admin interface for viewing and managing sessions. Sessions can be viewed in the admin panel under the "Sessions" section, where you can:

  • Search sessions by session key
  • View session creation and expiration times
  • Delete expired or unwanted sessions

The SessionAdmin viewset provides the interface for managing sessions in the admin panel.

Installation

Install the plain.sessions package from PyPI:

uv add plain.sessions

Add plain.sessions to your INSTALLED_PACKAGES and include the SessionMiddleware in your middleware:

INSTALLED_PACKAGES = [
    # ...
    "plain.sessions",
]

MIDDLEWARE = [
    # ...
    "plain.sessions.middleware.SessionMiddleware",
    # ...
]

Run migrations to create the session table:

plain migrate plain.sessions

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

plain_sessions-0.31.0.tar.gz (11.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

plain_sessions-0.31.0-py3-none-any.whl (16.5 kB view details)

Uploaded Python 3

File details

Details for the file plain_sessions-0.31.0.tar.gz.

File metadata

  • Download URL: plain_sessions-0.31.0.tar.gz
  • Upload date:
  • Size: 11.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.8.22

File hashes

Hashes for plain_sessions-0.31.0.tar.gz
Algorithm Hash digest
SHA256 a8de241acad2f2495fdb1314c7e47f39e559b04f3c500311ebab04bf0d347fa7
MD5 3e40bce898aebdaf1d870b510ee97300
BLAKE2b-256 2fb7613b108fbf7fb648c8e4cea6b440c5de452644963e4c16209a205e9e3f92

See more details on using hashes here.

File details

Details for the file plain_sessions-0.31.0-py3-none-any.whl.

File metadata

File hashes

Hashes for plain_sessions-0.31.0-py3-none-any.whl
Algorithm Hash digest
SHA256 091b789c9fdcef46e9f4de9fd9d9d37f16502f2fbc74f45887ed8707e5646bee
MD5 d0d0149291bdc567d3b962d269d159c0
BLAKE2b-256 90a0fcc0a1f906754e7b1da55a78bf594999719f82c8ad8f7bc581cd36f35a32

See more details on using hashes here.

Supported by

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