from io import BytesIO

import factory
from django.conf import settings
from django.contrib.auth.hashers import make_password
from django.core.files import base
from django.core.files import images
from PIL import Image

from adhocracy4.test import factories


class UserFactory(factory.django.DjangoModelFactory):

    class Meta:
        model = settings.AUTH_USER_MODEL

    username = factory.Faker('name')
    password = make_password('password')
    email = factory.Faker('email')


class AdminFactory(factory.django.DjangoModelFactory):

    class Meta:
        model = settings.AUTH_USER_MODEL

    username = factory.Faker('name')
    password = make_password('password')
    is_superuser = True


class PhaseFactory(factories.PhaseFactory):
    type = 'meinberlin_ideas:010:issue'


class OrganisationFactory(factory.django.DjangoModelFactory):

    class Meta:
        model = 'meinberlin_organisations.Organisation'
        django_get_or_create = ('name',)

    name = factory.Faker('company')

    @factory.post_generation
    def initiators(self, create, extracted, **kwargs):
        if not extracted:
            user = UserFactory()
            self.initiators.add(user)
            return

        if extracted:
            for user in extracted:
                self.initiators.add(user)


class ContentTypeFactory(factory.django.DjangoModelFactory):

    class Meta:
        model = 'contenttypes.ContentType'

    app_label = factory.Faker('name')
    model = factory.Faker('name')
    name = factory.Faker('name')


class ImageFactory():
    """Create a django file object containg an image."""

    def __call__(self, resolution, image_format='JPEG', name=None):

        filename = name or 'default.{}'.format(image_format.lower())
        color = 'blue'
        image = Image.new('RGB', resolution, color)
        image_data = BytesIO()
        image.save(image_data, image_format)
        image_content = base.ContentFile(image_data.getvalue())
        return images.ImageFile(image_content.file, filename)


class CommentFactory(factory.django.DjangoModelFactory):

    class Meta:
        model = 'a4comments.Comment'

    comment = factory.Faker('text')
    creator = factory.SubFactory(UserFactory)

@@ -214,16 +214,46 @@
  Image%0A%0A
+from adhocracy4 import phases%0A
 from adh
@@ -730,16 +730,38 @@
  True%0A%0A%0A
+# FIXME: move to core%0A
 class Ph
@@ -759,24 +759,31 @@
 %0Aclass Phase
+Content
 Factory(fact
@@ -788,66 +788,779 @@
 ctor
-ies.PhaseFactory):%0A    type = 'meinberlin_ideas:010:issue'
+y.Factory):%0A    class Meta:%0A        model = phases.PhaseContent%0A%0A    app = 'phase_content_factory'%0A    phase = 'factory_phase'%0A    weight = 10%0A    view = None%0A%0A    name = 'Factory Phase'%0A    description = 'Factory Phase Description'%0A    module_name = 'factory phase module'%0A%0A    features = %7B%7D%0A%0A    @classmethod%0A    def _create(cls, model_class, *args, **kwargs):%0A        phase_content = model_class()%0A        for key, value in kwargs.items():%0A            setattr(phase_content, key, value)%0A%0A        phases.content.register(phase_content)%0A        return phase_content%0A%0A%0A# FIXME: move to core%0Aclass PhaseFactory(factories.PhaseFactory):%0A%0A    class Params:%0A        phase_content = PhaseContentFactory()%0A%0A    type = factory.LazyAttribute(lambda f: f.phase_content.identifier)
 %0A%0A%0Ac

Add PhaseContentFactory