-
Notifications
You must be signed in to change notification settings - Fork 118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ability to add default photos from custom folder path #95
Comments
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')),
} |
@cvng Tnx, but maybe it will be better to add such functionality to main repo.... |
Yes, I can send a PR if needed |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ability to add default photos from custom folder path
The text was updated successfully, but these errors were encountered: