Skip to content

Commit 7cd0a2b

Browse files
committed
1 parent 593009b commit 7cd0a2b

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

modeltrans/fields.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,9 @@ def get_localized_value(self, instance, field_name):
112112
value = instance.i18n.get(field_name)
113113

114114
if isinstance(self.original_field, fields.files.FileField):
115-
# TODO: Review this versus `descriptor_class`; need to write some additional tests to verify
116-
return self.attr_class(instance, self, value)
115+
descriptor = self.descriptor_class(self)
116+
descriptor.__set__(instance, value)
117+
return descriptor.__get__(instance)
117118

118119
return value
119120

tests/test_models.py

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from django.core.exceptions import ValidationError
22
from django.core.files.base import ContentFile
3+
from django.core.files.storage.memory import InMemoryFileNode
34
from django.db import DataError, models, transaction
45
from django.test import TestCase, override_settings
56
from django.utils.translation import override
@@ -144,12 +145,10 @@ def test_fallback_getting_FileField(self):
144145
post = Post.objects.create(title="Test Post", is_published=True)
145146
sample_file = ContentFile("sample content", name="sample-en.txt")
146147
attachment = Attachment.objects.create(post=post, file=sample_file)
147-
default_file_name = attachment.file.name
148-
assert default_file_name
149148

150149
with override("fr"):
151150
self.assertIsInstance(attachment.file_i18n, models.fields.files.FieldFile)
152-
self.assertEqual(attachment.file_i18n.name, default_file_name)
151+
self.assertIsInstance(attachment.file_i18n.file, InMemoryFileNode)
153152

154153
def test_set_FileField(self):
155154
post = Post.objects.create(title="Test Post", is_published=True)
@@ -162,14 +161,58 @@ def test_set_FileField(self):
162161
attachment = Attachment.objects.create(
163162
post=post, file=sample_file_en, file_fr=sample_file_fr
164163
)
164+
self.assertIsInstance(attachment.file_fr, models.fields.files.FieldFile)
165+
166+
self.assertIsInstance(attachment.file.file, InMemoryFileNode)
167+
self.assertIsInstance(attachment.file_fr.file, InMemoryFileNode)
165168

166169
saved_fr_content = attachment.file_fr.read().decode("utf-8")
167170
self.assertEqual(saved_fr_content, fr_content)
168-
self.assertIsInstance(attachment.file_fr, models.fields.files.FieldFile)
169171

170172
with override("fr"):
171173
self.assertEqual(attachment.file_i18n, attachment.file_fr)
172174

175+
def test_FileField_getter(self):
176+
post = Post.objects.create(title="Test Post", is_published=True)
177+
178+
fr_content = "exemple de contenu 2"
179+
sample_file_fr = ContentFile(fr_content, name="sample-fr-2.txt")
180+
181+
attachment = Attachment(post=post, file_fr=sample_file_fr)
182+
# prior to invoking save, the file_fr should be an instance of FieldFile
183+
self.assertIsInstance(attachment.file_fr, models.fields.files.FieldFile)
184+
# but the file object should be an instance of ContentFile
185+
self.assertIsInstance(attachment.file_fr.file, ContentFile)
186+
attachment.save()
187+
188+
# After saving, the file object should be the default storage class
189+
self.assertIsInstance(attachment.file_fr.file, InMemoryFileNode)
190+
191+
# Retreiving the instance from the database, file and file_fr
192+
# should return the same kind of interface (a FieldFile instance)
193+
attachment = Attachment.objects.get(pk=attachment.pk)
194+
self.assertIsInstance(attachment.file, models.fields.files.FieldFile)
195+
self.assertIsInstance(attachment.file_fr, models.fields.files.FieldFile)
196+
197+
# Test that we can overwrite those with new content
198+
new_content = "new content"
199+
new_fr_content = "new French content"
200+
attachment.file = ContentFile(new_content, name="content-new.txt")
201+
attachment.file_fr = ContentFile(new_fr_content, name="content-new-fr.txt")
202+
self.assertIsInstance(attachment.file, models.fields.files.FieldFile)
203+
self.assertIsInstance(attachment.file_fr, models.fields.files.FieldFile)
204+
205+
attachment.save()
206+
207+
self.assertIsInstance(attachment.file_fr, models.fields.files.FieldFile)
208+
self.assertIsInstance(attachment.file, models.fields.files.FieldFile)
209+
210+
with attachment.file.open() as f:
211+
self.assertEqual(f.read().decode("utf-8"), new_content)
212+
213+
with attachment.file_fr.open() as f:
214+
self.assertEqual(f.read().decode("utf-8"), new_fr_content)
215+
173216
def test_creating_using_virtual_default_language_field(self):
174217
m = Blog.objects.create(title_en="Falcon")
175218

0 commit comments

Comments
 (0)