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.
50 lines
1.6 KiB
50 lines
1.6 KiB
9 years ago
|
# -*- coding: utf-8 -*-
|
||
|
from __future__ import unicode_literals
|
||
|
|
||
|
import os
|
||
|
import uuid
|
||
|
from django.db import models
|
||
|
from django.utils.encoding import python_2_unicode_compatible
|
||
|
from django.utils.text import slugify
|
||
|
from django.utils.translation import ugettext_lazy as _
|
||
|
|
||
|
from ..storages import ProtectedMediaAssetStorage
|
||
|
|
||
|
# TODO Define the central storage somewhere more central
|
||
|
originals_storage = ProtectedMediaAssetStorage()
|
||
|
|
||
|
|
||
|
# TODO Move this to special FileField class
|
||
|
def get_upload_path(instance, filename):
|
||
|
"""
|
||
|
Returns /<uuid_hex>/original/<slugified_filename.ext>
|
||
|
where
|
||
|
- uuid is taken from instance,
|
||
|
- filename is slugified and shortened to a max length of 255 characters including the extension.
|
||
|
"""
|
||
|
MAX_LENGTH = 255
|
||
|
name, ext = os.path.splitext(filename)
|
||
|
name = slugify(name)
|
||
|
name = name[:(MAX_LENGTH - len(ext))]
|
||
|
filename = "%s%s" % (name, ext)
|
||
|
return os.path.join(instance.get_uuid(), instance.storage.ORIGINAL_FILE_PREFIX, filename)
|
||
|
|
||
|
|
||
|
@python_2_unicode_compatible
|
||
|
class MediaAsset(models.Model):
|
||
|
uuid_hex = models.CharField(max_length=32, null=False, editable=False)
|
||
|
original_file = models.FileField(upload_to=get_upload_path,
|
||
|
storage=originals_storage
|
||
|
)
|
||
|
name = models.CharField(_('name'), max_length=50)
|
||
|
|
||
|
def __str__(self):
|
||
|
return self.name
|
||
|
|
||
|
# TODO Make auto-initialized UUID-field
|
||
|
def get_uuid(self):
|
||
|
if not self.uuid_hex:
|
||
|
self.uuid_hex = uuid.uuid4().hex
|
||
|
return uuid.UUID(self.uuid_hex)
|
||
|
|