Skip to main content

A ZipFile subclass that accepts Django Templates

Project description

https://travis-ci.org/fcurella/django-zipfile.svg?branch=master

A subclass of zipfile.Zipfile that works with Django templates.

Usage:

from zipfile import ZIP_DEFLATED
from django_zipfile import TemplateZipFile

def myview(request, object_id):
    obj = get_object_or_404(MyModel, pk=object_id)
    context = {
        'object': obj
    }
    response = HttpResponse(mimetype='application/octet-stream')
    response['Content-Disposition'] = 'attachment; filename=myfile.zip'

    container = TemplateZipFile(response, mode='w', compression=ZIP_DEFLATED, template_root='myapp/myzipskeleton/')

    container.write_template('mimetype')
    container.write_template('META-INF/container.xml')
    container.write_template('chapter1.html', context=context)

    container.close()
    return response

You can also specify multiple roots for your templates:

container = TemplateZipFile(response, mode='w', compression=ZIP_DEFLATED, template_root=['myapp/myzipskeleton/override/', 'myapp/myzipskeleton/default/'])

as well as multiple templates when you add files:

container.write_template(['override.html', 'default.html'], filename="chapter1.html")

TemplateZipFile will look for templates in the specified order, first by root, then by template name. For example:

myzipfile = TemplateZipFile(response, mode='w', compression=ZIP_DEFLATED, template_root=['override/', 'default/'])
myzipfile.write_template(['two.txt', 'one.txt'], filename='myfile.txt')

# Will use the first existing template from ['override/two.txt', 'override/one.txt', 'default/two.txt', 'default/one.txt']

If none of the templates can be found, write_template will raise a TemplateDoesNotExist error.

You can specify a file as optional with optional=True

myzipfile.write_template(['two.txt', 'one.txt'], filename='myfile.txt', optional=True)

Doing so will silently swallow the TemplateDoesNotExist exception.

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-zipfile-0.4.0.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

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

django_zipfile-0.4.0-py2.py3-none-any.whl (3.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django-zipfile-0.4.0.tar.gz.

File metadata

  • Download URL: django-zipfile-0.4.0.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.4

File hashes

Hashes for django-zipfile-0.4.0.tar.gz
Algorithm Hash digest
SHA256 096dd39201d3536abf6588c5efdc6cd1876dc9a50a21540e3a2048688a1d6162
MD5 c61f2fccbddc1f638bc099bfd0413599
BLAKE2b-256 e9646553d37e670a6bea4c921d2c7950ce5d92037c47f7f3b143cebc13fb019b

See more details on using hashes here.

File details

Details for the file django_zipfile-0.4.0-py2.py3-none-any.whl.

File metadata

  • Download URL: django_zipfile-0.4.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 3.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.4

File hashes

Hashes for django_zipfile-0.4.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 24f941ff0e0f72a91f6af1895d61defabb1c5370de95835d5a460a3cfb92a3ea
MD5 da8556d60d2ed856608bff26d67bafae
BLAKE2b-256 7db069be149e9ed6e1d84b0a2a1db7f293ce1e672882c23ced571a80df4f20f9

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