Small application that simplifies naming of uploaded files
Project description
Synopsis
Small application that simplifies naming of uploaded files. License is MIT.
Installation
You can get dj-upload-to release version from pypi with pip:
$ pip install dj-upload-to
or development one from github:
$ pip install -e git+https://github.com/marazmiki/dj-upload-to#egg=dj-upload-url
You’re not need include it into your INSTALLED_APPS
Usage
Assumes you have model:
from django.db import models
from dj_upload_to import UploadTo
upload_to = UploadTo()
class Model(models.Model):
file = models.ImageField(upload_to=upload_to)
As you see, UploadTo generates callable object (with __call__ methodfor passing into upload_to attribute of FileField (see django upload_to docs for details)
When you save model with image originally named myphoto.JPG, file will be saved with name such as:
model/ab/cd/abcdabcd-0123-4567-8901-234567890ab.jpg
where:
model is prefix automatically generated from model class. You can override it
ab is the first 2 char segment of filename
cd is the second 2 char segment of filename
abcdabcd-0123-4567-8901-234567890ab autogenerated with uuid filename
.jpg is a lower cased extension taken from original file
Customize
You can customize behavior of UploadTo with options in constructor:
prefix: prefix of filename. Default is None. If None, prefix will be generated by model class name
num_seg: number of parts of segmentation. Default is 2
seg_size: length of segment in chars. Default is 2
save_name: use original name without autogeneration. Default is False
There are some examples:
>>> model = Model()
>>> UploadTo(prefix='my_files')(model_instance, 'file.jpg')
u'my_files/d9/a4/d9a4ef25-11b0-41bb-a543-baaac6553024.jpg'
>>> UploadTo(num_seg=4)(model_instance, 'file.jpg')
u'model/36/52/99/f6/365299f6-8dc5-4ca2-848d-965f002a9b72.jpg'
>>> UploadTo(seg_size=4)(model_instance, 'file.jpg')
u'model/3142/f2ef/3142f2ef-2680-4a99-82fc-3c8d9d3179dc.jpg'
>>> UploadTo(save_name=True)(model_instance, 'file.jpg')
u'model/file.jpg'
Contributing
Ideas, bugfixes, pull requests are welcome on GitHub
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Hashes for dj_upload_to-1.0.0-py2-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1fc041822aaa82bced06fa1ead480df2957ee4962f5dd9b5d596e3bcc80309f4 |
|
MD5 | 24a04141ce7c8cf6c08c8a7c1dd9b3a9 |
|
BLAKE2b-256 | 28033dd9a832d5f8d3c2292ed5c7c8b6a878f55d53eef6fbfdb50719b2188c19 |