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 hashes)

Uploaded Source

Built Distribution

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

Uploaded Python 3

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