diff --git a/factory/__init__.py b/factory/__init__.py index 62042a2a..9808e87a 100644 --- a/factory/__init__.py +++ b/factory/__init__.py @@ -2,6 +2,7 @@ import importlib.metadata +from . import alchemy, django, mogo, mongoengine from .base import ( BaseDictFactory, BaseListFactory, @@ -54,22 +55,5 @@ stub_batch, ) -try: - from . import alchemy -except ImportError: - pass -try: - from . import django -except ImportError: - pass -try: - from . import mogo -except ImportError: - pass -try: - from . import mongoengine -except ImportError: - pass - __author__ = 'Raphaƫl Barrois ' __version__ = importlib.metadata.version("factory_boy") diff --git a/factory/alchemy.py b/factory/alchemy.py index f934ce5d..ae4fc94c 100644 --- a/factory/alchemy.py +++ b/factory/alchemy.py @@ -1,8 +1,5 @@ # Copyright: See the LICENSE file. -from sqlalchemy.exc import IntegrityError -from sqlalchemy.orm.exc import NoResultFound - from . import base, errors SESSION_PERSISTENCE_COMMIT = 'commit' @@ -64,6 +61,9 @@ def _generate(cls, strategy, params): @classmethod def _get_or_create(cls, model_class, session, args, kwargs): + from sqlalchemy.exc import IntegrityError + from sqlalchemy.orm.exc import NoResultFound + key_fields = {} for field in cls._meta.sqlalchemy_get_or_create: if field not in kwargs: diff --git a/factory/django.py b/factory/django.py index b53fd5b5..8af13622 100644 --- a/factory/django.py +++ b/factory/django.py @@ -11,10 +11,6 @@ import warnings from typing import Dict, TypeVar -from django.contrib.auth.hashers import make_password -from django.core import files as django_files -from django.db import IntegrityError - from . import base, declarations, errors logger = logging.getLogger('factory.generate') @@ -124,6 +120,7 @@ def _generate(cls, strategy, params): @classmethod def _get_or_create(cls, model_class, *args, **kwargs): """Create an instance of the model through objects.get_or_create.""" + from django.db import IntegrityError manager = cls._get_manager(model_class) assert 'defaults' not in cls._meta.django_get_or_create, ( @@ -193,7 +190,10 @@ def _after_postgeneration(cls, instance, create, results=None): class Password(declarations.Transformer): - def __init__(self, password, transform=make_password, **kwargs): + def __init__(self, password, transform=None, **kwargs): + if transform is None: + from django.contrib.auth.hashers import make_password + transform = make_password super().__init__(password, transform=transform, **kwargs) @@ -207,6 +207,7 @@ def _make_data(self, params): return params.get('data', b'') def _make_content(self, params): + from django.core.files.base import ContentFile, File path = '' from_path = params.get('from_path') @@ -222,21 +223,21 @@ def _make_content(self, params): if from_path: path = from_path with open(path, 'rb') as f: - content = django_files.base.ContentFile(f.read()) + content = ContentFile(f.read()) elif from_file: f = from_file - content = django_files.File(f) + content = File(f) path = content.name elif from_func: func = from_func - content = django_files.File(func()) + content = File(func()) path = content.name else: data = self._make_data(params) - content = django_files.base.ContentFile(data) + content = ContentFile(data) if path: default_filename = os.path.basename(path) @@ -248,8 +249,9 @@ def _make_content(self, params): def evaluate(self, instance, step, extra): """Fill in the field.""" + from django.core.files.base import File filename, content = self._make_content(extra) - return django_files.File(content.file, filename) + return File(content.file, filename) class ImageField(FileField):