Giter Site home page Giter Site logo

Comments (3)

cvng avatar cvng commented on August 15, 2024

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.

ArtemBernatskyy avatar ArtemBernatskyy commented on August 15, 2024

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

from django-autofixture.

cvng avatar cvng commented on August 15, 2024

Yes, I can send a PR if needed

from django-autofixture.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.