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
Decorator
You can decorate functions and methods with block_db to block database access in them. Eg:
from sans_db.context_managers import block_db
class MyClass:
def allowed(self):
User.objects.create(...) # Works outside of block_db()
@block_db()
def not_allowed(self):
User.objects.create(...) # 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 %}
Metadata
Release files for django-sans-db 1.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django_sans_db-1.2.0.tar.gz | 4.9 kB | Without provenance | Details for django_sans_db-1.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_sans_db-1.2.0-py3-none-any.whl | Python 3 | none | any | Details for django_sans_db-1.2.0-py3-none-any.whl |
Total release size: 10.7 kB
Release files / django_sans_db-1.2.0.tar.gz
| Download URL | django_sans_db-1.2.0.tar.gz |
|---|---|
| Size | 4.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
81fa58e8aab4a48e19d8cd8a0b92ede96ae61a46bdc7b46c57eb3a089679e2ef
|
|
BLAKE2b-256 checksum How to use checksums |
500d565b09458d08128c852f815db3361fbb101437d5381eb7f0e87d536386f7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.2.2 CPython/3.10.8 Linux/5.15.0-1022-azure
|
Release files / django_sans_db-1.2.0-py3-none-any.whl
| Download URL | django_sans_db-1.2.0-py3-none-any.whl |
|---|---|
| Size | 5.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c83dc0342fc070bf7d77205db323e81c6e8ea14546cd4d3608c9bc8259f5252d
|
|
BLAKE2b-256 checksum How to use checksums |
f30900c8d4c1475a8c92936c2b18b497a122e2fa824ab3b5972577232f481107
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.2.2 CPython/3.10.8 Linux/5.15.0-1022-azure
|
Security for django-sans-db 1.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.