Skip to main content

Django tools from WITH

Project description

Django Wools

Django tools from WITH.

That's a collection of things that we at WITH got tired of copy/pasting in every project.

Install

pip install django_wools

Included Wools

Storage

django_wools.storage.GzipManifestStaticFilesStorage

That's a sub-class of the ManifestStaticFilesStorage but that makes sure that along with all the files comes a .gz version which is easy to pick up for nginx (or other static files server).

Middlewares

django_wools.middlewares.NowMiddleware

Suppose that you have a content that is available up until a given date. When the date is passed then everything related to this content expires. However, in order to do this, you're probably going to make several request, possibly in loosely connected parts of your code. In those cases, when looking at the time, the clock will show different value as the time passes between calls. It means that you could very well end up with one half of your code considering that the object is still valid but the other half that it expired.

In order to prevent this, the simplest is to consider that the time is fixed and that the code executes instantly at the moment of the request. The goal of this middleware is to save the current time at each request and then to provide an easy way to get the current time through the request.

If the middleware is activated, you should be able to get the time like this:

from time import sleep
from django.shortcuts import render

def my_view(request):
    print(f"Now is {request.now()}")
    sleep(42)
    print(f"Now is still {request.now()}")

    return render(request, "something.html", {"now": request.now()})

django_wools.middlewares.SlowMiddleware

When developing a SPA or hybrid app, you want to make sure that the app is structurally ready to handle load times from the server, even if the connection is a bit shaky. Also, you want to make sure that not too many requests are sent.

In order for you to fully realize how slow your website is going to be on a bad connection, th e SlowMiddleware will automatically add a delay before replying to each request.

Add this to your settings.py

MIDDLEWARE = [
    # ...
    "django_wools.middlewares.SlowMiddleware",
]

SLOW_MIDDLEWARE_LATENCY = 1 if DEBUG else 0

By doing this, your requests will be added a 1s delay if the DEBUG mode is enabled.

Database

django_wools.db.require_lock

Provides a way to explicitly generate a PostgreSQL lock on a table.

By example:

from django.db.transaction import atomic
from django_wools.db import require_lock

from my_app.models import MyModel


@atomic
@require_lock(MyModel, 'ACCESS EXCLUSIVE')
def myview(request):
    # do stuff here

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_wools-0.1.4.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_wools-0.1.4-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file django_wools-0.1.4.tar.gz.

File metadata

  • Download URL: django_wools-0.1.4.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.8.2 Linux/4.19.0-9-amd64

File hashes

Hashes for django_wools-0.1.4.tar.gz
Algorithm Hash digest
SHA256 013255687e9a094e154488cd57614f03e99a2820521d3d51c5e9941fed071619
MD5 207a3165b3a81ab924f8c1264c6bb2cf
BLAKE2b-256 5e306f9c69ec7777dc17b41caab744b8ac7409fd142db7dabf120b0bc34cb957

See more details on using hashes here.

File details

Details for the file django_wools-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: django_wools-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.8.2 Linux/4.19.0-9-amd64

File hashes

Hashes for django_wools-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 56bbefb929a76f0dfbf588f4bb16c64b1ad45acdcc3a4a6d2715e01fe71967ff
MD5 5610bb9b1a2b59504f330a55c2ef7b6d
BLAKE2b-256 a69d6a00a0cf2a136d681c5d794936995defb414d7add7bcbbdab4d28b3b1315

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page