-
Notifications
You must be signed in to change notification settings - Fork 2
/
files.py
66 lines (46 loc) · 1.81 KB
/
files.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
import stat
import tempfile
import uuid
from django.template.defaultfilters import slugify
from django.utils.deconstruct import deconstructible
from django.utils.encoding import force_text
@deconstructible
class UploadToPath(object):
def __init__(self, base_path, attr=None, uuid_filename=False):
self.base_path = base_path
self.attr = attr
self.uuid_filename = uuid_filename
def __call__(self, instance, filename):
if self.attr:
parts = self.attr.split('__')
obj_path = parts[:-1]
field_name = parts[-1]
obj = instance
for part in obj_path:
obj = getattr(obj, part)
path = self.base_path % slugify(getattr(obj, field_name, '_'))
else:
path = self.base_path
filename_parts = filename.rsplit('.', 1)
if self.uuid_filename:
filename = force_text(uuid.uuid4())
else:
filename = slugify(filename_parts[0])
extension = len(filename_parts) > 1 and u'.{0}'.format(filename_parts[-1]) or ''
return os.path.join(path, u'{0}{1}'.format(filename, extension))
def upload_to_path(base_path, attr=None, uuid_filename=False):
return UploadToPath(base_path, attr=attr, uuid_filename=uuid_filename)
class MoveableNamedTemporaryFile(object):
def __init__(self, name):
suffix = os.path.splitext(os.path.basename(name))[1]
self.file = tempfile.NamedTemporaryFile(suffix=suffix, delete=False)
perms = stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH
os.chmod(self.file.name, perms)
self.name = name
def chunks(self):
return self.file.read()
def close(self):
return self.file.close()
def temporary_file_path(self):
return self.file.name