from django.conf import settings

import factory

from ..models import Document


class DocumentFactory(factory.django.DjangoModelFactory):
    title = factory.Sequence(lambda n: "Test document {0}".format(n))
    summary = "This is a test summary"
    lang = settings.LANGUAGE_CODE
    original = factory.django.FileField()
    credits = "Document credits"
    package_id = ""

    @factory.post_generation
    def tags(self, create, extracted, **kwargs):
        if extracted:
            self.tags.add(*extracted)

    class Meta:
        model = Document

from django.conf import settings

import factory

from ..models import Document

class EmptyFileField(factory.django.FileField):
    DEFAULT_FILENAME = None

class DocumentFactory(factory.django.DjangoModelFactory):
    title = factory.Sequence(lambda n: "Test document {0}".format(n))
    summary = "This is a test summary"
    lang = settings.LANGUAGE_CODE
    original = factory.django.FileField()
    preview = EmptyFileField()
    credits = "Document credits"
    package_id = ""

    @factory.post_generation
    def tags(self, create, extracted, **kwargs):
        if extracted:
            self.tags.add(*extracted)

    class Meta:
        model = Document

Allow DocumentFactory to handle preview field.
Allow DocumentFactory to handle preview field.

The factory.django.FileField.DEFAULT_FILENAME == 'example.dat'.
It means that by default a FileField created by factoryboy is considered as a
True value.
Before this commit, we were not defining a Document.preview field in the
factory so factoryboy created a empty FileField.
To not break the API for other tests, we need to create a "False" FileField by
default.
To do so, we need to change the DEFAULT_FILENAME to None.

  from django.conf import settings
  
  import factory
  
  from ..models import Document
  
+ class EmptyFileField(factory.django.FileField):
+     DEFAULT_FILENAME = None
  
  class DocumentFactory(factory.django.DjangoModelFactory):
      title = factory.Sequence(lambda n: "Test document {0}".format(n))
      summary = "This is a test summary"
      lang = settings.LANGUAGE_CODE
      original = factory.django.FileField()
+     preview = EmptyFileField()
      credits = "Document credits"
      package_id = ""
  
      @factory.post_generation
      def tags(self, create, extracted, **kwargs):
          if extracted:
              self.tags.add(*extracted)
  
      class Meta:
          model = Document
  
Allow DocumentFactory to handle preview field.
## Code Before:
from django.conf import settings

import factory

from ..models import Document


class DocumentFactory(factory.django.DjangoModelFactory):
    title = factory.Sequence(lambda n: "Test document {0}".format(n))
    summary = "This is a test summary"
    lang = settings.LANGUAGE_CODE
    original = factory.django.FileField()
    credits = "Document credits"
    package_id = ""

    @factory.post_generation
    def tags(self, create, extracted, **kwargs):
        if extracted:
            self.tags.add(*extracted)

    class Meta:
        model = Document

## Instruction:
Allow DocumentFactory to handle preview field.
## Code After:
from django.conf import settings

import factory

from ..models import Document

class EmptyFileField(factory.django.FileField):
    DEFAULT_FILENAME = None

class DocumentFactory(factory.django.DjangoModelFactory):
    title = factory.Sequence(lambda n: "Test document {0}".format(n))
    summary = "This is a test summary"
    lang = settings.LANGUAGE_CODE
    original = factory.django.FileField()
    preview = EmptyFileField()
    credits = "Document credits"
    package_id = ""

    @factory.post_generation
    def tags(self, create, extracted, **kwargs):
        if extracted:
            self.tags.add(*extracted)

    class Meta:
        model = Document
