Skip to content

Commit 9cf13ca

Browse files
committed
Merge pull request #9 from xlusive/master
Updated LocalStorageFileField. Now it's working with mongoengine 0.8.3
2 parents ea9ef5f + 447bec8 commit 9cf13ca

File tree

1 file changed

+27
-43
lines changed

1 file changed

+27
-43
lines changed

fields/fields.py

Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from django.core.files.storage import default_storage
1212

1313
from mongoengine.connection import get_db, DEFAULT_CONNECTION_NAME
14-
from django.utils.encoding import force_text
14+
from django.utils.encoding import force_str, force_text
1515

1616

1717
class TimedeltaField(BaseField):
@@ -58,77 +58,61 @@ class LocalStorageFileField(BaseField):
5858
proxy_class = FieldFile
5959

6060
def __init__(self,
61-
db_alias=DEFAULT_CONNECTION_NAME,
61+
size=None,
6262
name=None,
6363
upload_to='',
6464
storage=None,
6565
**kwargs):
66-
67-
self.db_alias = db_alias
66+
self.size=size
6867
self.storage = storage or default_storage
6968
self.upload_to = upload_to
70-
7169
if callable(upload_to):
7270
self.generate_filename = upload_to
73-
74-
super(DJFileField, self).__init__(**kwargs)
75-
71+
super(LocalStorageFileField, self).__init__(**kwargs)
7672

7773
def __get__(self, instance, owner):
78-
# Lots of information on whats going on here can be found
79-
# on Django's FieldFile implementation, go over to GitHub to
80-
# read it.
74+
if instance is None:
75+
return self
76+
8177
file = instance._data.get(self.name)
8278

8379
if isinstance(file, str_types) or file is None:
8480
attr = self.proxy_class(instance, self, file)
8581
instance._data[self.name] = attr
8682

87-
elif isinstance(file, File) and not isinstance(file, FieldFile):
88-
file_copy = self.proxy_class(instance, self, file.name)
89-
file_copy.file = file
90-
file_copy._committed = False
91-
instance._data[self.name] = file_copy
92-
93-
elif isinstance(file, FieldFile) and not hasattr(file, 'field'):
94-
file.instance = instance
95-
file.field = self
96-
file.storage = self.storage
97-
9883
return instance._data[self.name]
9984

100-
10185
def __set__(self, instance, value):
102-
instance._data[self.name] = value
103-
86+
key = self.name
87+
if isinstance(value, File) and not isinstance(value, FieldFile):
88+
file = instance._data.get(self.name)
89+
if file:
90+
try:
91+
file.delete()
92+
except:
93+
pass
94+
# Create a new proxy object as we don't already have one
95+
file_copy = self.proxy_class(instance, self, value.name)
96+
file_copy.file = value
97+
instance._data[key] = file_copy
98+
else:
99+
instance._data[key] = value
100+
101+
instance._mark_as_changed(key)
104102

105103
def get_directory_name(self):
106-
return os.path.normpath(force_text(datetime.datetime.now().strftime(self.upload_to)))
107-
104+
return os.path.normpath(force_text(datetime.datetime.now().strftime(force_str(self.upload_to))))
108105

109106
def get_filename(self, filename):
110107
return os.path.normpath(self.storage.get_valid_name(os.path.basename(filename)))
111108

112-
113109
def generate_filename(self, instance, filename):
114110
return os.path.join(self.get_directory_name(), self.get_filename(filename))
115111

116-
117112
def to_mongo(self, value):
118-
# Store the path in MongoDB
119-
# I also used this bit to actually save the file to disk.
120-
# The value I'm getting here is a FileFiled and it all looks ok.
121-
122-
if not value._committed and value is not None:
123-
value.save(value.name, value)
124-
return value.path
125-
126-
return value.name
127-
128-
129-
def to_python(self, value):
130-
eu = self
131-
return eu.proxy_class(eu.owner_document, eu, value)
113+
if isinstance(value, self.proxy_class):
114+
return value.name
115+
return value
132116

133117

134118
class LowerStringField(StringField):

0 commit comments

Comments
 (0)