Skip to main content

No project description provided

Project description

Sometimes you need to be able to restrict how many of a certain model a site can have. For example, the number of active users, the number of posts per user, or the number of active pages.

This django module allows you to easily setup predefined, hardcoded limits to enforce these kinds of restrictions.

  1. Install django-limits from PyPI:

    pip install django-limits
  2. Add django_limits to your INSTALLED_APPS:

    INSTALLED APPS = [
        'django_limits',
        # All your other apps
    ]
  3. Create and set your limiter class (see below)

    MODEL_LIMIT_CLASS = ‘limiter.MyLimiter’

  4. Add the limiter middleware to properly render templates:

    MIDDLEWARE_CLASSES = [
        'django_limits.middleware.LimitExceededMiddleware',
        # Your other middleware
    ]

Defining a Limiter class

To properly limit your models, you need a limiter class. This isn’t auto loaded, so it can be anywhere, should probably go in a limiter.py file or in your models.py.

All limiters inherit from the django_limits.limiter.Limiter, but you can override most of this class as needed.

The simplest limiter, is just an inheritance of this class, with a class attribute defining the limiting rules. A set of rules is a dictionary, with the key being a model class, and the associated values being a dictionary described below, or list of dictionaries, like so:

from django_limits.limiter import Limiter
from django.contrib.auth.models import User

class MyLimiter(Limiter):
    rules = {
        User: [
            {
                'limit': 20,
                'message': "Only 20 active users allowed",
                'filterset': Q(is_active=True)
            },
            {
                'limit': 10,
                'message': "Only 10 staff members allowed",
                'filterset': Q(is_staff=True)
            }
        ]
    }

The above Limiter will only allow a maximum of 20 active users, and 10 staff members.

Defining a rules dictionary

A rules dictionary can contain the following keys:

  • limit (required - The total number of the given model allowed, or if there is a queryset, the total allowed for that queryset

  • message - The message shown when a user tries to exceed this number

  • filterset - A django Q filters that defines the types of models to restrict on, if this is not set the total number of the model in the database is used (e.g Model.objects.all())

  • template - The template used to render the page explaining the limit restriction, defaults to django_limits/limit_exceeded.html

An example rule is below for a widget is below:

Widget: [
    {
        'limit': 10,
        'message': "Only 10 staff members allowed",
        'filterset': Q(color="Blue")
    },
    {
        'limit': 10,
        'message': "Only 10 staff members allowed",
        'filterset': Q(color="Red")
    },
    {
        'limit': 30,
        'message': "Only 30 widgets allowed",
        'template': "widgets/totla_widgets_exceeded.html'
    }
]

In this example, at most 10 blue, 10 red and a total 30 widgets are allowed.

How it works

Django limtis installs a pre_save signal for every model, and checks against the rules, and to prevent the save from finishing throws a LimitExceeded exception which is caught by the middleware, which in turn renders a page with a HTTP 403 error.

Apologies to Tina Turner.

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-limits-0.0.6.tar.gz (19.6 kB view details)

Uploaded Source

Built Distribution

django_limits-0.0.6-py3-none-any.whl (21.5 kB view details)

Uploaded Python 3

File details

Details for the file django-limits-0.0.6.tar.gz.

File metadata

  • Download URL: django-limits-0.0.6.tar.gz
  • Upload date:
  • Size: 19.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.4.2 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.8

File hashes

Hashes for django-limits-0.0.6.tar.gz
Algorithm Hash digest
SHA256 207bf77886a05b1d051a485b7fc58b411d77977d71dc43c137fff3f5b450a107
MD5 9c748840619750e19495b93284445bb1
BLAKE2b-256 ad6ddeb1c8fdd6282a7cc0cc2eba805c9504961352b0fbc07806915a295c573b

See more details on using hashes here.

File details

Details for the file django_limits-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: django_limits-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 21.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.4.2 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.8

File hashes

Hashes for django_limits-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 633ad74700d3978b2285dacd622e457cd328db9bf8906f26d985f76c837d6250
MD5 8e88a2c6c77b2fd2fa9ba6aa6ea3a4ce
BLAKE2b-256 d65c8a20a5a40c84053ed9b3214429406ef1cea34af193de223fffba8943e149

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