Tools to limit database access in parts of your Django codebase
Project description
Django sans DB
Tools for limiting access to the database in parts of your Django code.
Installation
pip install django-sans-db
If you wish to use the {% sansdb %}
template tag,
you will need to add "sans_db"
to your INSTALLED_APPS
.
Usage
Context manager
You can block access to your database for a section of your code using block_db
:
from sans_db.context_managers import block_db
User.objects.create(...) # Works outside of block_db()
with block_db():
User.objects.get() # Raises DatabaseAccessBlocked
If you have multiple entries in your Django DATABASES
setting,
then block_db
will default to blocking all of them.
If you wish to block access to a subset of your databases,
pass a list of their aliases (the keys in the DATABASES
dictionary).
from sans_db.context_managers import block_db
with block_db(databases=["replica"]):
User.objects.using("primary").create(...) # This DB isn't blocked.
User.objects.using("replica").get() # Raises DatabaseAccessBlocked
Template backend
You can block access to the database when rendering Django templates with our custom template backend.
Note: Currently, only Django templates are supported.
You can block database access in all of your templates
by setting your templates backend to "sans_db.template_backends.django_sans_db.DjangoTemplatesSansDB"
For example:
# settings.py
TEMPLATES = [
{
"BACKEND": "sans_db.template_backends.django_sans_db.DjangoTemplatesSansDB",
"APP_DIRS": True,
"OPTIONS": {...},
},
]
Attempts to query the database will now cause a sans_db.exceptions.DatabaseAccessBlocked
to be raised.
Please refer to Django's docs on support for template engines for details on how to set this up as a secondary template renderer.
Template tag
You can block DB access in a portion of your template
by wrapping it with the {% sansdb %}
template tag.
The template tag accepts database aliases as either strings, or variables. If passed as a variable, either strings or iterables of strings are accepted. If no aliases are passed, all databases will be blocked.
Note: DatabaseAccessBlocked
is raised when an attempt is made to access the DB.
To block all databases:
{% load sansdb %}
{% sansdb %}
{# ... #}
{% endsansdb %}
To block a list of databases named in the template:
{% load sansdb %}
{% sansdb "second_db" "third_db" %}
{# ... #}
{% endsansdb %}
To block a list of databases from a context variable:
{% load sansdb %}
{% sansdb databases %}
{# ... #}
{% endsansdb %}
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
Built Distribution
File details
Details for the file django-sans-db-1.1.0.tar.gz
.
File metadata
- Download URL: django-sans-db-1.1.0.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.10.4 Linux/5.13.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 62bbd0cec6f3c549a7b575479100361b2787640bc3fbc045462862f7386f4d54 |
|
MD5 | a458df6ba9369346a286717877023d49 |
|
BLAKE2b-256 | c03e526fe3328c27136b12bf9866aded28d1cf32371ab19a9b8ee3107814ae26 |
File details
Details for the file django_sans_db-1.1.0-py3-none-any.whl
.
File metadata
- Download URL: django_sans_db-1.1.0-py3-none-any.whl
- Upload date:
- Size: 5.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.10.4 Linux/5.13.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3313cedac7595c66c12e7083b5e3d7a27e02fcacb20fa8e457f6c4a25c87faac |
|
MD5 | 54f4d5f5afb30f1274520e7cab11a28f |
|
BLAKE2b-256 | 49d11bc7d3e63c57d95d7c5600f77dc8f9d880d84dcb3188dd00e5dc55f88866 |