Skip to main content

Django app that caches CDN files for use when coding offline

Project description

This app is to help in those situations where you can’t get on the network but you want to write some Django code. Surround your static CDN references (like jquery and the like) with this template tag and when you turn it on the URLs will be re-written from a local copy.

Installation

In your settings file, add ‘airplane’ to your settings.INSTALLED_APPS field:

INSTALLED_APPS = (
    ...
    'airplane',
)

Also in settings, make the following additions:

import airplane

STATICFILES_DIRS = (
    airplane.cache_path(),
)

AIRPLANE_MODE = airplane.BUILD_CACHE
#AIRPLANE_MODE = airplane.USE_CACHE
#AIRPLANE_MODE = airplane.AUTO_CACHE

Now use the airplane tag in your templates

{% load airplanetags %}

<html>
    <head>
        <link rel="stylesheet"
            href="{% airplane 'https://maxcdn.bootstrapcdn.com/bootstrap.min.css' %}">
    </head>
</html>

Change the AIRPLANE_MODE setting to airplane.USE_CACHE and subsequent calls to the {% airplane %} tag will return a reference to the locally cached version.

Settings

Airplane only does something if DEBUG=True and if you have an AIRPLANE_MODE value set to airplane.BUILD_CACHE, airplane.USE_CACHE, or airplane.AUTO_CACHE. If one of these conditions is not met, the tag simply returns the value passed in.

For example, if DEBUG=False and your template contains:

<link rel="stylesheet"
    href="{% airplane 'https://maxcdn.bootstrapcdn.com/bootstrap.min.css' %}">

Then the above snippet renders as:

<link rel="stylesheet"
    href="https://maxcdn.bootstrapcdn.com/bootstrap.min.css">

When AIRPLANE_MODE is set to airplane.BUILD_CACHE any URLs passed in are fetched and their contents added to a local cache. The default local cache is .airport_cache relative to the base directory of your project.

You can change the location of the cache by setting AIRPLANE_CACHE. The setting accepts either fully qualified paths or paths relative to the project’s base directory.

# settings.py

AIRPLANE_CACHE = /foo/bar/cache     # fully qualified

# or

AIRPLANE_CACHE = my_cache           # relative to settings.BASE_DIR

# or nothing, defaults to settings.BASEDIR + '.airplane_cache'

Once you have cached all the files you are using, switch AIRPLANE_MODE to airplane.USE_CACHE. All URLs are now re-written to point to the contents of the local cache.

Alternatively, you can set AIRPLANE_MODE to airplane.AUTO_CACHE and the first call will cache the file and subsequent calls will use the cached copy.

Commands

The following django commands come with airplane.

airinfo

$ ./manage.py airinfo
Cache mode: AUTO_CACHE
Cache directory: /Users/foo/sample_site/.airplane_cache
Cache contents:
   https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css

This command takes no arguments and displays information about the cache. The current mode, the path of the directory and any items cached inside are shown.

aircache

./manage.py aircache https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css

This command takes a single URL as an argument and caches the contents of the URL.

Schemaless URLs

As airplane is a template tag library, it doesn’t have access to the request object at execution. In order to allow schemaless URLs, the code makes the assumption that the schema is “https” if it is not given in the URL.

Supports

django-airplane has been tested with:

  • Python 3.6, 3.7 and Django 2.2

  • Python 3.6, 3.7 and Django 3.0

Docs

Docs available at: http://django-airplane.readthedocs.io/en/latest/

Source: https://github.com/cltrudeau/django-airplane

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-airplane-1.0.0.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

django_airplane-1.0.0-py2.py3-none-any.whl (9.3 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-airplane-1.0.0.tar.gz.

File metadata

  • Download URL: django-airplane-1.0.0.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.0

File hashes

Hashes for django-airplane-1.0.0.tar.gz
Algorithm Hash digest
SHA256 0f5985be127039a9c68a8e2c1083e9d86d22e4e60a5049a9bfa46da0daa1e379
MD5 061f25feb4688835f9c3570923798a2d
BLAKE2b-256 4e93ecfae7a2ddbc358b3d5289abaafc84a8fa9adb713e4b54d76c3944938a06

See more details on using hashes here.

File details

Details for the file django_airplane-1.0.0-py2.py3-none-any.whl.

File metadata

  • Download URL: django_airplane-1.0.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.0

File hashes

Hashes for django_airplane-1.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 3ea6595b5edcedd0a8b5cafc81585dae260fcc7fb14bff84744ea821392ad748
MD5 c9e8430dccb6cf41e037e7495fb1e181
BLAKE2b-256 5f4805f20efd1c370bd717565c1931f3929b006f63cca7715e6ec30a1aef57db

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