Skip to main content

Allows ORM constructs to be sealed to prevent them from executing queries on attribute accesses.

Project description

Seal
Build Status Coverage status

Django application providing queryset sealing capability to force appropriate usage of only()/defer() and select_related()/prefetch_related().

Installation

pip install django-seal

Usage

# models.py
from django.db import models
from seal.models import SealableModel

class Location(SealableModel):
    latitude = models.FloatField()
    longitude = models.FloatField()

class SeaLion(SealableModel):
    height = models.PositiveIntegerField()
    weight = models.PositiveIntegerField()
    location = models.ForeignKey(Location, models.CASCADE, null=True)
    previous_locations = models.ManyToManyField(Location, related_name='previous_visitors')

By default UnsealedAttributeAccess warnings will be raised on sealed objects attributes accesses

>>> location = Location.objects.create(latitude=51.585474, longitude=156.634331)
>>> sealion = SeaLion.objects.create(height=1, weight=100, location=location)
>>> sealion.previous_locations.add(location)
>>> SeaLion.objects.only('height').seal().get().weight
UnsealedAttributeAccess:: Attempt to fetch deferred field "weight" on sealed <SeaLion instance>.
>>> SeaLion.objects.seal().get().location
UnsealedAttributeAccess: Attempt to fetch related field "location" on sealed <SeaLion instance>.
>>> SeaLion.objects.seal().get().previous_locations.all()
UnsealedAttributeAccess: Attempt to fetch many-to-many field "previous_locations" on sealed <SeaLion instance>.

You can elevate the warnings to exceptions by filtering them. This is useful to assert no unsealed attribute accesses are performed when running your test suite for example.

>>> import warnings
>>> from seal.exceptions import UnsealedAttributeAccess
>>> warnings.filterwarnings('error', category=UnsealedAttributeAccess)
>>> SeaLion.objects.only('height').seal().get().weight
Traceback (most recent call last)
...
UnsealedAttributeAccess:: Attempt to fetch deferred field "weight" on sealed <SeaLion instance>.
>>> SeaLion.objects.seal().get().location
Traceback (most recent call last)
...
UnsealedAttributeAccess: Attempt to fetch related field "location" on sealed <SeaLion instance>.
>>> SeaLion.objects.seal().get().previous_locations.all()
Traceback (most recent call last)
...
UnsealedAttributeAccess: Attempt to fetch many-to-many field "previous_locations" on sealed <SeaLion instance>.

Or you can configure logging to capture warnings to log unsealed attribute accesses to the py.warnings logger which is a nice way to identify and address unsealed attributes accesses from production logs without taking your application down if some instances happen to slip through your battery of tests.

>>> import logging
>>> logging.captureWarnings()

Development

Make your changes, and then run tests via tox:

tox

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-seal-1.4.3.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

django_seal-1.4.3-py2.py3-none-any.whl (8.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-seal-1.4.3.tar.gz.

File metadata

  • Download URL: django-seal-1.4.3.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for django-seal-1.4.3.tar.gz
Algorithm Hash digest
SHA256 756f7fee2c2ff192f12ad14ea001d81cb8f6f96a4c8443940937a72eb57e65da
MD5 eaf9ab1585cbf8bc1d95c65179ab5c70
BLAKE2b-256 07ef1cfa9af87e28176b79262b11faae7e8b8ebabc5e4b204886faad990728ce

See more details on using hashes here.

File details

Details for the file django_seal-1.4.3-py2.py3-none-any.whl.

File metadata

  • Download URL: django_seal-1.4.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 8.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for django_seal-1.4.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 21b3f1d2dd9c4e6e984cfb25e06fcf084e5fbb4e028d92d2e166c5e0dbf194ea
MD5 de5fd9a1a5d522a43d5f810e55539f15
BLAKE2b-256 b35ffaaef6952f1e21083b64ed293ae30289000475788cc04fd423c480e1507a

See more details on using hashes here.

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