Comments (3)
I have a forked version that do this already, i can send a PR if wanted.
class ImageGenerator(generators.ImageGenerator):
"""
Extends base class to expose ``get_placeholder_image`` as a method,
so child classes can easily override default implementation.
"""
@staticmethod
def get_placeholder_image(*args, **kwargs):
from autofixture.placeholder import get_placeholder_image
return get_placeholder_image(*args, **kwargs)
def generate(self):
# from .placeholder import get_placeholder_image
width, height = random.choice(self.sizes)
# Ensure that _autofixture folder exists.
i = 0
path = self.generate_file_path(width, height)
while self.storage.exists(path):
i += 1
path = self.generate_file_path(width, height, '_{0}'.format(i))
return self.storage.save(
path,
ContentFile(self.get_placeholder_image(width, height))
)
class RandomImageGenerator(ImageGenerator):
"""
Randomly picks an image from a given directory and saves it to the ``settings.MEDIA_ROOT``.
"""
def __init__(self, source_dir=None, *args, **kwargs):
if source_dir:
assert os.path.exists(source_dir)
self.source_dir = source_dir
self.available_files = os.listdir(self.source_dir)
super(RandomImageGenerator, self).__init__(*args, **kwargs)
def get_placeholder_image(self, *args, **kwargs):
if self.available_files:
file_path = random.choice(self.available_files)
with open(os.path.join(self.source_dir, file_path), 'rb') as f:
return f.read() # Must returns bytes
else:
return super(RandomImageGenerator, self).get_placeholder_image(*args, **kwargs)
Usage:
class MyModelAutoFixture(AutoFixture):
current_dir = os.path.dirname(os.path.realpath(__file__))
field_values = {
'picture_1': RandomImageGenerator(source_dir=os.path.join(current_dir, 'autofixtures/pictures')),
}
from django-autofixture.
@cvng Tnx, but maybe it will be better to add such functionality to main repo....
from django-autofixture.
Yes, I can send a PR if needed
from django-autofixture.
Related Issues (20)
- Better support for choices, null=True and blank=True
- Integration with Faker
- Support for Django 2.0 HOT 9
- Error with Django 2: TypeError: Direct assignment to the forward side of a many-to-many set is prohibited. HOT 3
- How to populate a reverse relationship?
- Use of bulk_create
- Allow muting signals in a way similar to factoryboy HOT 1
- Raise an error in case when field doesn't exist in a model.
- django.utils.six -> six in latest version of django HOT 2
- python setup.py egg_info did not run successfully HOT 6
- Unknown command: 'loadtestdata' Type 'manage.py help' for usage. HOT 3
- docs could be a bit easier to follow for newer developer HOT 1
- Frequently got ``TransactionManagementError`` HOT 6
- could we add Point fields into django-autofixture ? HOT 4
- errro with mysql Row size HOT 1
- Recursive generate_fx HOT 1
- Add max length value
- How to generate random value from given list? HOT 1
- Support for Django 1.10 and higher? HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-autofixture.