summaryrefslogtreecommitdiff
path: root/factory
diff options
context:
space:
mode:
authorRaphaël Barrois <raphael.barrois@polytechnique.org>2015-03-26 23:31:56 +0100
committerRaphaël Barrois <raphael.barrois@polytechnique.org>2015-03-26 23:31:56 +0100
commit4e0e563c1c0d823d2869d340e2fa31ca8630d854 (patch)
tree340f2947d2b1ffaa69413b183aa9da5caeca16ad /factory
parenta456a9e3f440e5f61497e97d75dd0a15efe71a8d (diff)
downloadfactory-boy-4e0e563c1c0d823d2869d340e2fa31ca8630d854.tar
factory-boy-4e0e563c1c0d823d2869d340e2fa31ca8630d854.tar.gz
Turn FileField/ImageField into normal fields (Closes #141).
Previously, they ran as post_generation hooks, meaning that they couldn't be checked in a model's ``save()`` method, for instance.
Diffstat (limited to 'factory')
-rw-r--r--factory/django.py31
1 files changed, 9 insertions, 22 deletions
diff --git a/factory/django.py b/factory/django.py
index ee5749a..9d4cde9 100644
--- a/factory/django.py
+++ b/factory/django.py
@@ -144,24 +144,23 @@ class DjangoModelFactory(base.Factory):
obj.save()
-class FileField(declarations.PostGenerationDeclaration):
+class FileField(declarations.ParameteredAttribute):
"""Helper to fill in django.db.models.FileField from a Factory."""
DEFAULT_FILENAME = 'example.dat'
def __init__(self, **defaults):
require_django()
- self.defaults = defaults
- super(FileField, self).__init__()
+ super(FileField, self).__init__(**defaults)
def _make_data(self, params):
"""Create data for the field."""
return params.get('data', b'')
- def _make_content(self, extraction_context):
+ def _make_content(self, extra):
path = ''
params = dict(self.defaults)
- params.update(extraction_context.extra)
+ params.update(extra)
if params.get('from_path') and params.get('from_file'):
raise ValueError(
@@ -169,12 +168,7 @@ class FileField(declarations.PostGenerationDeclaration):
"be non-empty when calling factory.django.FileField."
)
- if extraction_context.did_extract:
- # Should be a django.core.files.File
- content = extraction_context.value
- path = content.name
-
- elif params.get('from_path'):
+ if params.get('from_path'):
path = params['from_path']
f = open(path, 'rb')
content = django_files.File(f, name=path)
@@ -196,19 +190,12 @@ class FileField(declarations.PostGenerationDeclaration):
filename = params.get('filename', default_filename)
return filename, content
- def call(self, obj, create, extraction_context):
+ def evaluate(self, sequence, obj, create, extra=None, containers=()):
"""Fill in the field."""
- if extraction_context.did_extract and extraction_context.value is None:
- # User passed an empty value, don't fill
- return
- filename, content = self._make_content(extraction_context)
- field_file = getattr(obj, extraction_context.for_field)
- try:
- field_file.save(filename, content, save=create)
- finally:
- content.file.close()
- return field_file
+ filename, content = self._make_content(extra)
+ print("Returning file with filename=%r, contents=%r" % (filename, content))
+ return django_files.File(content.file, filename)
class ImageField(FileField):