Skip to content
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

Open
ArtemBernatskyy opened this issue Nov 29, 2016 · 3 comments
Open

Ability to add default photos from custom folder path #95

ArtemBernatskyy opened this issue Nov 29, 2016 · 3 comments

Comments

@ArtemBernatskyy
Copy link

Ability to add default photos from custom folder path

@cvng
Copy link

cvng commented Nov 30, 2016

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')),
    }

@ArtemBernatskyy
Copy link
Author

@cvng Tnx, but maybe it will be better to add such functionality to main repo....

@cvng
Copy link

cvng commented Nov 30, 2016

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants