from __future__ import with_statement import os from django core files

 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
from __future__ import with_statement
import os
from django.core.files import locks
from django.core.files.storage import FileSystemStorage, file_move_safe
class AvatarStorage(FileSystemStorage):
"""
Storage for avatars, which replace existing file,
if names are clashing.
"""
def _save(self, name, content):
full_path = self.path(name)
directory = os.path.dirname(full_path)
if not os.path.exists(directory):
os.makedirs(directory)
elif not os.path.isdir(directory):
raise IOError("%s exists and is not a directory." % directory)
# This file has a file path that we can move.
if hasattr(content, 'temporary_file_path'):
file_move_safe(content.temporary_file_path(), full_path,
allow_overwrite=True)
content.close()
# This is a normal uploadedfile that we can stream.
else:
with file(full_path, 'w') as f:
for chunk in content.chunks():
f.write(chunk)
return name
def path(self, name):
return os.path.normpath(os.path.join(self.location, name))
def get_available_name(self, name):
return name