Skip to content

Commit

Permalink
Merge pull request coagulant#58 from sprymak/master
Browse files Browse the repository at this point in the history
Add support for Django 1.8 UUIDField. Fix FileField issues
  • Loading branch information
GeyseR committed Feb 24, 2017
2 parents 7d159ea + ea4781a commit c9ba5f2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions django_any/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@


ipaddress_field_defined = django.VERSION < (1, 9)
uuid_field_defined = django.VERSION >= (1, 8)


def get_model_onetoone_fields(model):
Expand Down
20 changes: 18 additions & 2 deletions django_any/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,11 @@ def get_some_file(path):

if files:
result_file = random.choice(files)
instance = field.storage.open("%s/%s" % (path, result_file)).file
instance = field.storage.open(os.path.join(path, result_file)).file
return FieldFile(instance, field, result_file)

for subdir in subdirs:
result = get_some_file("%s/%s" % (path, subdir))
result = get_some_file(os.path.join(path, subdir))
if result:
return result

Expand All @@ -265,6 +265,9 @@ def get_some_file(path):
upload_to = os.path.dirname(generated_filepath)
else:
upload_to = field.upload_to
if not field.storage.exists(upload_to):
# make a directory, assuming it's on local file system
os.makedirs(field.storage.path(upload_to))
result = get_some_file(upload_to)

if result is None and not field.blank:
Expand Down Expand Up @@ -303,6 +306,19 @@ def get_some_file(path):
return result


if compat.uuid_field_defined:
@any_field.register(models.UUIDField)
def any_uuid_field(field, **kwargs):
"""
Return random value for UUIDField
>>> result = any_field(models.UUIDField())
>>> type(result)
<class 'uuid.UUID'>
"""
import uuid
return uuid.uuid4()


if compat.ipaddress_field_defined:
@any_field.register(models.IPAddressField)
def any_ipaddress_field(field, **kwargs):
Expand Down

0 comments on commit c9ba5f2

Please sign in to comment.