You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.3 KiB
47 lines
1.3 KiB
# # -*- coding: utf-8 -*- |
|
# from __future__ import unicode_literals |
|
# # Erik Stein <code@classlibrary.net>, 2016 |
|
|
|
import os |
|
import uuid |
|
from django.conf import settings |
|
from django.db import models |
|
# TODO Use improved slugify |
|
from django.utils.text import slugify |
|
|
|
from assetkit.files.storage import FILENAME_MAX_LENGTH, ORIGINAL_FILE_PREFIX |
|
|
|
|
|
class UUIDMixin(models.Model): |
|
uuid_hex = models.CharField(max_length=32, null=False, editable=False) |
|
|
|
class Meta: |
|
abstract = True |
|
|
|
# TODO Make auto-initializing UUID-field |
|
def get_uuid(self): |
|
if not self.uuid_hex: |
|
self.uuid_hex = uuid.uuid4().hex |
|
return str(uuid.UUID(self.uuid_hex)) |
|
|
|
|
|
def get_upload_path(instance, filename): |
|
""" |
|
Returns /<uuid_hex>/original/<slugified name>.<extension> |
|
where |
|
- uuid and name are fields from instance, |
|
- filename is slugified and shortened to a max length including the extension. |
|
- extension is preserved from original filename |
|
""" |
|
name, ext = os.path.splitext(filename) |
|
instance.original_file_name = filename |
|
name = slugify(instance.name or name) |
|
name = name[:(FILENAME_MAX_LENGTH - len(ext))] |
|
filename = "%s%s" % (name, ext) |
|
return os.path.join( |
|
instance.get_uuid(), |
|
ORIGINAL_FILE_PREFIX, |
|
filename |
|
) |
|
|
|
|
|
|