|
11 | 11 | from django.core.files.storage import default_storage
|
12 | 12 |
|
13 | 13 | 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 |
15 | 15 |
|
16 | 16 |
|
17 | 17 | class TimedeltaField(BaseField):
|
@@ -58,77 +58,61 @@ class LocalStorageFileField(BaseField):
|
58 | 58 | proxy_class = FieldFile
|
59 | 59 |
|
60 | 60 | def __init__(self,
|
61 |
| - db_alias=DEFAULT_CONNECTION_NAME, |
| 61 | + size=None, |
62 | 62 | name=None,
|
63 | 63 | upload_to='',
|
64 | 64 | storage=None,
|
65 | 65 | **kwargs):
|
66 |
| - |
67 |
| - self.db_alias = db_alias |
| 66 | + self.size=size |
68 | 67 | self.storage = storage or default_storage
|
69 | 68 | self.upload_to = upload_to
|
70 |
| - |
71 | 69 | if callable(upload_to):
|
72 | 70 | self.generate_filename = upload_to
|
73 |
| - |
74 |
| - super(DJFileField, self).__init__(**kwargs) |
75 |
| - |
| 71 | + super(LocalStorageFileField, self).__init__(**kwargs) |
76 | 72 |
|
77 | 73 | 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 | + |
81 | 77 | file = instance._data.get(self.name)
|
82 | 78 |
|
83 | 79 | if isinstance(file, str_types) or file is None:
|
84 | 80 | attr = self.proxy_class(instance, self, file)
|
85 | 81 | instance._data[self.name] = attr
|
86 | 82 |
|
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 |
| - |
98 | 83 | return instance._data[self.name]
|
99 | 84 |
|
100 |
| - |
101 | 85 | 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) |
104 | 102 |
|
105 | 103 | 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)))) |
108 | 105 |
|
109 | 106 | def get_filename(self, filename):
|
110 | 107 | return os.path.normpath(self.storage.get_valid_name(os.path.basename(filename)))
|
111 | 108 |
|
112 |
| - |
113 | 109 | def generate_filename(self, instance, filename):
|
114 | 110 | return os.path.join(self.get_directory_name(), self.get_filename(filename))
|
115 | 111 |
|
116 |
| - |
117 | 112 | 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 |
132 | 116 |
|
133 | 117 |
|
134 | 118 | class LowerStringField(StringField):
|
|
0 commit comments