Skip to content

Commit

Permalink
Register API models in admin (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-misuk-valor authored Jun 10, 2024
1 parent 21d1de6 commit c3eae1b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/hope_dedup_engine/apps/api/admin.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion src/hope_dedup_engine/apps/api/models/__init__.py
Original file line number Diff line number Diff line change
@@ -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
16 changes: 15 additions & 1 deletion tests/extras/testutils/factories/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -6,13 +11,22 @@
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

bases = (AutoRegisterModelFactory,)
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
4 changes: 3 additions & 1 deletion tests/extras/testutils/factories/api.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit c3eae1b

Please sign in to comment.