Skip to main content

Inspect the current Python call stack to determine if the Django app is just starting up or already running.

Project description

Django Stack Inspector

The Django Stack Inspector package is useful to inspect the current call stack of a Django application and determine if it is starting up or already running.

It is likely that the 99% of applications do not have a need for this, but in specific niche scenarios it can be very useful. For example, when implementing a model manager that limits queries to items owned by a specific tenant by overriding the get_queryset() method filtered using data stored in a ContextVar or thread-local set by a request middleware. It may be desired that if queries are executed without the ContextVar being appropriately set (e.g. queries via asynchronous background workers or via admin commands) then the get_queryset() method fails-closed by throwing an exception warning the user that the required context is not set. However, this fail-closed behaviour breaks the management commands for migrations by throwing the exception.

Installation

pip install django_stack_inspector

Usage

An example of how to use when creating a custom model manager to restrict queries to specific tenants.

from django_stack_inspector import StackInspector

def get_queryset(self):
    """
    Override the default queryset behaviour to enforce a filter dependent on the tenant context
    The queryset will fail-closed if a query is ran without a context, either by returning no results in the
    queryset or by raising an exception.
    """
    try:
        ctx = get_request_context()
    except RequestContextNotSet as exc:
        # Context not set so fail-closed
        if StackInspector().is_app_startup():
            # Silence exception and return empty queryset as app starting (i.e. is a management command)
            return super().get_queryset().none()
        # Raise exception to alert developer
        raise exc

    # Perform filtering
    return super().get_queryset().filter(tenant=ctx.tenant)

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

django_stack_inspector-1.0.0.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

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

django_stack_inspector-1.0.0-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file django_stack_inspector-1.0.0.tar.gz.

File metadata

  • Download URL: django_stack_inspector-1.0.0.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.11.1 Linux/5.15.0-1031-azure

File hashes

Hashes for django_stack_inspector-1.0.0.tar.gz
Algorithm Hash digest
SHA256 1f0c04752f2630ed55eab4c6500b53ae97725e3540a04777135b37e724a9f9ce
MD5 c35e09b4261857967fffa408a3cf629e
BLAKE2b-256 d2351ec3297f61f11e9f86719eabb4409ce0e9c1234ffc6c99d06d288123abca

See more details on using hashes here.

File details

Details for the file django_stack_inspector-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_stack_inspector-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2c02365d2078889ce3ac5ba1e4b54a1046ce2aac90b3c4ee6ad62bfcaf9f03e5
MD5 66067ae9339c8ffba42694787170b120
BLAKE2b-256 67ebf2738c3767273c935d7a11fe40bb049ed73f6244efbd894d33b99e16a878

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