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

Wagtail Images

Several Wagtail-specific tags are provided to deal with images and more specifically responsive images. To use it:

  1. Add django_wools to INSTALLED_APPS
  2. Import wools_for_wt_images from your template
{% load wools_for_wt_images %}

Some specific settings can be set:

  • WOOLS_MAX_PIXEL_RATIO (default: 3) — Highest device pixel ratio to support.
  • WOOLS_INCREMENT_STEP_PERCENT (default: (sqrt(2) - 1) * 100) — The percentage of increase from the base density to the next one. The default values will generate x1, x2 and x4 with intermediate values that are x1.4142 and x2.8284.

All the tags will be default generate WebP images with a PNG fallback. The fallback can be changed to JPEG and the main format has to be WebP.

image_fixed_size

This tag will generate a <picture> tag for an image whose size in pixels you known in advance. That picture size will be enforced in the HTML code with inline properties.

Usage:

{% image_fixed_size max-500x500 %}

The arguments to this tag, in order, are:

  • image — The Wagtail-compatible image itself
  • spec — A spec like you would give to Wagtail (fill-500x500, max-500x500, etc)
  • fallback_format — The format to fallback in case the browser doesn't support WebP. Can either be "jpeg" or "png". Defaults to "png".
  • lossless — A boolean to enable losslessness of WebP. This does not affect the fallback format, so if you want a lossless fallback as well you'll need to use PNG.

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.2.0a2.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

django_wools-0.2.0a2-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file django_wools-0.2.0a2.tar.gz.

File metadata

  • Download URL: django_wools-0.2.0a2.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.8.2 Linux/5.10.0-3-amd64

File hashes

Hashes for django_wools-0.2.0a2.tar.gz
Algorithm Hash digest
SHA256 893d31b5849bedfb990ed4e047333976318c83fda035ea45c5e4520427e59c15
MD5 dae73c5fddfe16e3cc43e3bf11a49305
BLAKE2b-256 b80515a485c57312c2cbd6bdd889538044a58828be10acd00fb672e74ae167c6

See more details on using hashes here.

File details

Details for the file django_wools-0.2.0a2-py3-none-any.whl.

File metadata

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

File hashes

Hashes for django_wools-0.2.0a2-py3-none-any.whl
Algorithm Hash digest
SHA256 64ff4a89fcf84ae439c2b2cca744dd4a70d8de68679c04fc775be88d5efd7308
MD5 08ab867b8e190acc35e8c5db364cfbe7
BLAKE2b-256 e231999a1794531738d4628b399e6e2e097f1b68f2918209232999d75e4c1914

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