Skip to content

Commit

Permalink
Merge pull request #48 from Yupeek/develop
Browse files Browse the repository at this point in the history
release 1.8.2
  • Loading branch information
darius BERNARD authored Jan 16, 2019
2 parents 7a76adf + 2b2a5c0 commit fd05f23
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
2 changes: 1 addition & 1 deletion rest_models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__VERSION__ = '1.8.1'
__VERSION__ = '1.8.2'

try:
from rest_models.checks import register_checks
Expand Down
14 changes: 10 additions & 4 deletions rest_models/backend/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import re
from collections import defaultdict, namedtuple

import six
from django.core.exceptions import ImproperlyConfigured
from django.db.models import FileField, Transform
from django.db.models.aggregates import Count
Expand Down Expand Up @@ -1289,12 +1290,17 @@ def resolve_data_n_files(self):
if isinstance(field, FileField):
fieldfile = field.pre_save(val.instance, False)
# file.name is the return of our storage.save => we get the content file instead of his name
# to retreive it there.
# file.name can be a string, in this case, we did got the file at all, he is unmodified
file = fieldfile.name
if file is not None: # field value can be None....
files[field.column] = (file.name, file, file.content_type)
else:
if file is None:
# field can be set to None
data[field.column] = None
elif isinstance(file, six.string_types):
# str => we don't change it since it's not comming from our custom storage
pass
else:
files[field.column] = (file.name, file, file.content_type)

else:
fieldname = field.concrete and field.db_column or field.name
data[fieldname] = field.get_db_prep_save(val, connection=self.connection)
Expand Down
20 changes: 20 additions & 0 deletions rest_models/tests/tests_upload_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,23 @@ def test_null_value_update_from_client(self):
self.assertEqual(review_client.photo.url, 'http://testserver/media/%s' % self.img_name2)
self.assertEqual(review_client.photo.file.read(), image_test)
self.assertEqual(review_client.comment, 'comment')

def test_save_unchanged_file(self):

review_api = apimodels.Review.objects.create(
comment="coucou",
photo=SimpleUploadedFile(self.img_name, image_test, 'image/png'),
)

review_client = clientmodels.Review.objects.get(pk=review_api.pk)
self.assertEqual(review_client.photo.url, 'http://testserver/media/%s' % self.img_name)
self.assertEqual(review_client.photo.name, self.img_name)

review_client.save()
self.assertEqual(review_client.photo.url, 'http://testserver/media/%s' % self.img_name)
self.assertEqual(review_client.photo.name, self.img_name)
review_client.comment = 'eratum'
review_client.save()
self.assertEqual(review_client.photo.url, 'http://testserver/media/%s' % self.img_name)
self.assertEqual(review_client.photo.name, self.img_name)
self.assertEqual(review_client.photo.file.read(), image_test)

0 comments on commit fd05f23

Please sign in to comment.