Skip to main content

A Django app for storing and processing documents and images

Project description

The django-simple-file-handler package is an app for uploading and managing documents and images. With optional packages installed, it can process images and generate PDFs from HTML files. It only has been tested with Django 1.11 and Python 3.6.

If python-magic is installed, django-simple-file-handler will use it to check uploaded file MIME types. Otherwise, it will use Python’s built-in library. This package only has been tested with python-magic 0.4.

Please note that “private” files and PDFs would be more accurately described as “hidden.” The app generates a proxy URL that will load the file for a logged-in user and stores the file with a name consisting of many random characters. Still, it is possible to load these files without logging in if the file name is known.

Processed image generation requires that Pillow be installed. This package only has been tested with Pillow 5.0.

PDF generation requires that either WeasyPrint or xhtml2pdf be installed. This package only has been tested with WeasyPrint 0.28 and xhtml2pdf 0.2.1. Please refer to those projects’ documentation for HTML template-formatting specifics.

Quick start

  1. Run pip install django-simple-file-handler.

  2. Add django-simple-file-handler to your installed apps:

    INSTALLED_APPS = (
        ...
        'django_simple_file_handler',
    )
  3. Run python manage.py migrate.

  4. Include the django-simple-file-handler URLconf in your project urls.py like this (this step is only required if you wish to use private files or PDFs):

    urlpatterns = [
        ...
        url(
            r'^private-file-pseudo-directory-path/',
            include('django_simple_file_handler.urls')
        ),
    ]

Basics

Public and private document objects, as well as unprocessed image objects, can be created and managed via the admin site. Other types are read-only in the admin site.

All types can be created and accessed by subclassing or creating a foreign key in your own apps.

Temporary documents and temporary PDFs are intended to be used when two or more objects might need to have the same title. These add a string of random characters to the ends of file names to prevent overwriting files.

Classes

  • PublicDocument — Includes uploaded document file

  • PrivateDocument — Includes uploaded document file with a proxy URL requiring login

  • TemporaryDocument — Includes uploaded document file with a modified file name allowing title duplication

  • UnprocessedImage — Includes uploaded image file

  • ProcessedImage — Includes uploaded image file and processed image file

  • PublicPDF — Includes generated PDF file

  • PrivatePDF — Includes generated PDF file with a proxy URL requiring login

  • TemporaryPDF — Includes generated PDF file with a modified file name allowing title duplication

Attributes

  • createdDateTimeField with automatically generated value

  • updatedDateTimeField with automatically generated value

  • titleCharField with max_length of 245 characters (not available for ProcessedImage)

  • extra_textTextField (optional)

  • saved_fileFileField for the uploaded file

  • processed_fileFileField for processed image (ProcessedImage only)

  • output_widthPositiveIntegerField (ProcessedImage only)

  • output_heightPositiveIntegerField (ProcessedImage only)

  • subdirectory_path — String containing the path used by saved_file

  • image_path — String containing the path used by processed_file (ProcessedImage only)

  • output_mode — String used by Pillow (ProcessedImage only)

  • content_type — String used by Pillow (ProcessedImage only)

  • file_format — String used by Pillow (ProcessedImage only)

  • file_extension — String used by Pillow (ProcessedImage only)

Methods

  • file_url — Returns URL of the uploaded file as a string

  • file_link — Returns an HTML link to the URL of the uploaded file as a string

  • proxy_url — Returns proxy URL for the uploaded file as a string (PrivateDocument and PrivatePDF only)

  • proxy_link — Returns an HTML link to the proxy URL for the uploaded file as a string (PrivateDocument and PrivatePDF only)

  • image_url — Returns URL of the processed image file as a string (ProcessedImage only)

  • image_link — Returns an HTML link to the URL of the processed image file as a string (ProcessedImage only)

Generating PDFs

The example code below uses PublicPDF, but PrivatePDF and TemporaryPDF work the same way.

generated_pdf = PublicPDF(
    title = 'title of the generated PDF document',
    extra_text = 'any additional text needed with the object',
    template_location = 'path/to/your/html/template.html',
    template_data = {
        'value_one': value_to_be_inserted_in_template,
        'value_two': value_to_be_inserted_in_template,
        'value_three': value_to_be_inserted_in_template,
    },
)
generated_pdf.save()

Database object attributes can then be changed without rewriting the PDF file. The file is only written when the template_data dictionary is given and the object resaved.

File types

Supported document formats include PDF, ZIP, Word, Excel and PowerPoint. Supported image formats include PNG, JPEG, GIF and TIFF (processed images only).

To support different file types, subclass the relevant model and initialize your model with your own dictionary of allowed attributes.

from django_simple_file_handler.models import PublicDocument

...
class MyPublicDocument(PublicDocument):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._meta.get_field('saved_file').validators = [
            CheckExtMIME(allowed_attributes=MY_DICTIONARY),
        ]

Note that this validator will not appear in database migrations, which is intentional.

The dictionary’s format can optionally include arguments for file extensions, MIME types and verbose names for file formats (these will appear in error messages if given). Here is an example:

MY_DICTIONARY = {
    'allowed_extensions' : [
        'abc',
        'def',
        'ghi',
    ],
    'allowed_mimetypes' : [
        'application/example1',
        'application/example2',
        'image/example3',
    ],
    'allowed_verbose' : [
        'Format1',
        'Format2',
        'Format3',
    ],
}

File locations

If you wish to change the locations where files are stored, subclass the relevant model and change the relevant attributes as in this example:

from django_simple_file_handler import ProcessedImage

...
class MyProcessedImage(ProcessedImage):
    subdirectory_path = 'path/to/save/location/'
    image_path = 'path/to/save/location/'

Note that the image_path attribute only applies to ProcessedImage.

Image attributes

By default, images are processed into PNG format with RGB data. To use something else, subclass the ProcessedImage model and change the attributes as in this example:

from django_simple_file_handler.models import ProcessedImage

...
class MyProcessedImage(ProcessedImage):
    output_mode = 'P'
    content_type = 'image/gif'
    file_format = 'GIF'
    file_extension = 'gif'

Advanced use

The django-simple-file-handler models make use of modular, reusable mixins and functions that can, of course, be imported for use with your own code.

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_simple_file_handler-0.1.6.tar.gz (18.9 kB view details)

Uploaded Source

Built Distribution

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

django_simple_file_handler-0.1.6-py36-none-any.whl (24.1 kB view details)

Uploaded Python 3.6

File details

Details for the file django_simple_file_handler-0.1.6.tar.gz.

File metadata

  • Download URL: django_simple_file_handler-0.1.6.tar.gz
  • Upload date:
  • Size: 18.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.1.0 requests-toolbelt/0.8.0 tqdm/4.23.3 CPython/3.6.5

File hashes

Hashes for django_simple_file_handler-0.1.6.tar.gz
Algorithm Hash digest
SHA256 1577450f169a68ec8a8465dee055ed1dca17a9159f5b8ebb6076296f8a1f2ac0
MD5 b41df85d01cdb08cbdf36b4b414fe967
BLAKE2b-256 465ac9fa78f86aca219d5fb5d09a394892b0b6c5646f1e3bd52b4baa71262521

See more details on using hashes here.

File details

Details for the file django_simple_file_handler-0.1.6-py36-none-any.whl.

File metadata

  • Download URL: django_simple_file_handler-0.1.6-py36-none-any.whl
  • Upload date:
  • Size: 24.1 kB
  • Tags: Python 3.6
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.1.0 requests-toolbelt/0.8.0 tqdm/4.23.3 CPython/3.6.5

File hashes

Hashes for django_simple_file_handler-0.1.6-py36-none-any.whl
Algorithm Hash digest
SHA256 7706d9528586e2b2fb1837122f27ba8f40d6d2cfdf17469e1290b3a66838a0e5
MD5 6960307d05afb3add3cf68591af3ff4a
BLAKE2b-256 43ce3b5ecccf3458f130caeffbfe6c141bca3e34af6f302b250b8ff4f3c61267

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