From c3eae1b75522e850711472df023cc1779af7c1fa Mon Sep 17 00:00:00 2001 From: sergey-misuk-valor <168101676+sergey-misuk-valor@users.noreply.github.com> Date: Mon, 10 Jun 2024 14:48:17 +0400 Subject: [PATCH] Register API models in admin (#27) --- src/hope_dedup_engine/apps/api/admin.py | 8 ++++++++ .../apps/api/models/__init__.py | 2 +- tests/extras/testutils/factories/__init__.py | 16 +++++++++++++++- tests/extras/testutils/factories/api.py | 4 +++- 4 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 src/hope_dedup_engine/apps/api/admin.py diff --git a/src/hope_dedup_engine/apps/api/admin.py b/src/hope_dedup_engine/apps/api/admin.py new file mode 100644 index 00000000..9f384e0a --- /dev/null +++ b/src/hope_dedup_engine/apps/api/admin.py @@ -0,0 +1,8 @@ +from django.contrib import admin + +from hope_dedup_engine.apps.api.models import DeduplicationSet, Duplicate, HDEToken, Image + +admin.site.register(DeduplicationSet) +admin.site.register(Duplicate) +admin.site.register(HDEToken) +admin.site.register(Image) diff --git a/src/hope_dedup_engine/apps/api/models/__init__.py b/src/hope_dedup_engine/apps/api/models/__init__.py index 7ae749fe..571a4bfd 100644 --- a/src/hope_dedup_engine/apps/api/models/__init__.py +++ b/src/hope_dedup_engine/apps/api/models/__init__.py @@ -1,2 +1,2 @@ from hope_dedup_engine.apps.api.models.auth import HDEToken # noqa: F401 -from hope_dedup_engine.apps.api.models.deduplication import DeduplicationSet # noqa: F401 +from hope_dedup_engine.apps.api.models.deduplication import DeduplicationSet, Duplicate, Image # noqa: F401 diff --git a/tests/extras/testutils/factories/__init__.py b/tests/extras/testutils/factories/__init__.py index 80ea8a38..2a687069 100644 --- a/tests/extras/testutils/factories/__init__.py +++ b/tests/extras/testutils/factories/__init__.py @@ -1,3 +1,8 @@ +import importlib +import pkgutil +from pathlib import Path + +from factory.django import DjangoModelFactory from pytest_factoryboy import register from .base import AutoRegisterModelFactory, TAutoRegisterModelFactory, factories_registry @@ -6,8 +11,14 @@ from .user import ExternalSystemFactory, GroupFactory, SuperUserFactory, User, UserFactory # noqa from .userrole import UserRole, UserRoleFactory # noqa +for _, name, _ in pkgutil.iter_modules([str(Path(__file__).parent)]): + importlib.import_module(f".{name}", __package__) + + +django_model_factories = {factory._meta.model: factory for factory in DjangoModelFactory.__subclasses__()} -def get_factory_for_model(_model) -> type[TAutoRegisterModelFactory]: + +def get_factory_for_model(_model) -> type[TAutoRegisterModelFactory] | type[DjangoModelFactory]: class Meta: model = _model @@ -15,4 +26,7 @@ class Meta: if _model in factories_registry: return factories_registry[_model] # noqa + if _model in django_model_factories: + return django_model_factories[_model] + return register(type(f"{_model._meta.model_name}AutoCreatedFactory", bases, {"Meta": Meta})) # noqa diff --git a/tests/extras/testutils/factories/api.py b/tests/extras/testutils/factories/api.py index 931a7823..a795e755 100644 --- a/tests/extras/testutils/factories/api.py +++ b/tests/extras/testutils/factories/api.py @@ -1,12 +1,14 @@ from factory import SubFactory, fuzzy from factory.django import DjangoModelFactory -from testutils.factories import ExternalSystemFactory +from testutils.factories import ExternalSystemFactory, UserFactory from hope_dedup_engine.apps.api.models import DeduplicationSet, HDEToken from hope_dedup_engine.apps.api.models.deduplication import Duplicate, Image class TokenFactory(DjangoModelFactory): + user = SubFactory(UserFactory) + class Meta: model = HDEToken