django-upthor
django-upthor provides a django application for simple ajax file uploads. We use https://github.com/blueimp/jQuery-File-Upload for the upload functionality.
Warning: This isn’t close to being a complete app, but it’s getting there.
Usage
Step 1. Install
pip install django-upthor
Now you have two options:
If you want to encrypt FQ values, install pycrypto. pip install pycrypto==2.6.1
Or you can, disable FQ encryption by adding THOR_DISABLE_FQ_ENCRYPT = True to your settings file.
Step 2. (Django 1.6+)
Add ‘upthor’ to your installed apps in settings.py:
INSTALLED_APPS = (
...
"upthor",
)
Then:
python manage.py migrate
Step 3. Use it in your app’s models.
import os
import uuid
from django.db import models
from upthor import fields as thor_fields
def random_upload_path(instance, filename):
# Split the uuid into two parts so that we won't run into subdirectory count limits. First part has 3 hex chars,
# thus 4k possible values.
uuid_hex = uuid.uuid4().hex
return os.path.join(uuid_hex[:3], uuid_hex[3:], filename)
def post_example_file_link(real_instance, temporary_instance, raw_file):
"""
A callback called after linking the temporary file with the model.
**Warning**: Don't call instances save method from here, cause it will cause an recursion error.
@:param real_instance An instance of the model the file is attached to
@:param temporary_instance An instance of TemporaryFileWrapper that the form links to.
@:param raw_file The raw file that is being uploaded.
@:return bool If True, the uploaded temporary file is removed once the linking is complete.
"""
return True
def get_file_image(file_path):
""" An optional function that returns the display image html for files after uploading is complete"""
return '<i class="fa fa-file"></i>'
class ExampleModelWithFile(models.Model):
name = models.CharField(max_length=50)
file = thor_fields.ThorFileField(upload_to=random_upload_path,
allowed_types=['*'], widget=thor_fields.ThorSingleUploadWidget,
post_link=post_product_file_link,
get_upload_image=get_file_image)
Step 4. Make sure to include form media.
Make sure you include the media files for the form in your templates:
E.g. Add the following codes where form is the context object of your modelform that uses the uploader fields.
{{ form.media.css }}
{{ form.media.js }}
Step 5. Add the upload url to your project urls.
url(r'', include('upthor.urls')),
Step 6. Optional stuff
Temporary file cleanup
If you want to clean up temporary files automatically, you’ll need to install django-cron and add upthor.cron.CleanTemporaryFiles to your cron classes in settings.
Alternatively to clean up manually you can use the management command clean_temporary_files.
Custom upload widget template
You can override ThorSingleUploadWidget.render_template to return your own widget template instead of the hardcoded one defined in widgets.py. Although the structure (including most classes) has to remain the same, there are a few data attributes on .file-upload that you can use to customize behavior:
Data Attribute Name |
Type |
Description |
|---|---|---|
upload-url |
string |
Required: URL to POST temporary files to, defaults to reverse of thor-file-upload. |
max-size |
number |
Required: Maximum allowed file size in bytes, defaults to THOR_MAX_FILE_SIZE. |
size-error |
string |
Required: Text to display if the file doesn’t meet the size requirements, defaults to "Uploaded file too large". |
use-background |
boolean |
Whether or not to use background-image instead of img elements, defaults to false. |
Backends
Currently it only supports local file backend, but we plan to add other backends when we reach a stable state.
Settings
The following settings are customizable using your django project settings file.
THOR_UPLOAD_TO
Path where the upload files will be stored. Defaults to “temp-files”.
THOR_EXPIRE_TIME
How long to keep temporary files in the database and on disk. Defaults to “60*60*24”, e.g. 24 hours.
THOR_LINKED_EXPIRE_TIME
How long to keep linked temporary files in the database and on disk. Defaults to “60*60*6”, e.g. 6 hours.
THOR_MAX_FILE_SIZE
The max file size of uploaded files. Defaults to “2*1024*1024”, e.g. 2 MB.
THOR_DISABLE_FQ_ENCRYPT
Disable the FQ Encryption, if this is False you need to install pycrypto since that is used for encryption. Defaults to “False”.
THOR_ENABLE_ADMIN
Should TemporaryFileWrapper model be shown in the admin interface. Defaults to “True”.
Metadata
Release files for django-upthor 0.9.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| django-upthor-0.9.1.tar.gz | 14.7 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_upthor-0.9.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 438.7 kB
Release files / django-upthor-0.9.1.tar.gz
| Download URL | django-upthor-0.9.1.tar.gz |
|---|---|
| Size | 14.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4e0201fada52e086377392fc6b441072370fcce830de3566c31d523793d8580a
|
|
BLAKE2b-256 checksum How to use checksums |
1e3959ab3a3374379054666c4cdc7dc7dba129395c33fff3f7a536abf49bb547
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / django_upthor-0.9.1-py3-none-any.whl
| Download URL | django_upthor-0.9.1-py3-none-any.whl |
|---|---|
| Size | 424.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4af412edf5366ac96942cbf8a3be16016c5ed637d82f1c9e3c77fa0f215d7d29
|
|
BLAKE2b-256 checksum How to use checksums |
63419f88c779b7c80a19a959b962d5670cce4392d2883c452dc3ba6a2901685c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for django-upthor 0.9.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.