Skip to content

Commit

Permalink
chore: add a metaclass resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
edelclaux authored and jacquesfize committed May 16, 2024
1 parent 20a8aab commit 0114d1e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
7 changes: 2 additions & 5 deletions backend/geonature/core/gn_synthese/module.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
from geonature.core.gn_commons.models import TModules

from geonature.core.gn_synthese.imports import SyntheseImportMixin
from geonature.utils.metaclass_utils import metaclass_resolver


class SyntheseModuleMetaclass(type(SyntheseImportMixin), type(TModules)):
pass


class SyntheseModule(TModules, SyntheseImportMixin, metaclass=SyntheseModuleMetaclass):
class SyntheseModule(metaclass_resolver(TModules, SyntheseImportMixin)):
__mapper_args__ = {"polymorphic_identity": "synthese"}

def generate_input_url_for_dataset(self, dataset):
Expand Down
8 changes: 8 additions & 0 deletions backend/geonature/utils/metaclass_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def metaclass_resolver(*classes):
metaclass = tuple(set(type(cls) for cls in classes))
metaclass = (
metaclass[0]
if len(metaclass) == 1
else type("_".join(mcls.__name__ for mcls in metaclass), metaclass, {})
)
return metaclass("_".join(cls.__name__ for cls in classes), classes, {})
7 changes: 2 additions & 5 deletions contrib/gn_module_occhab/backend/gn_module_occhab/module.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
from geonature.core.gn_commons.models import TModules
from geonature.utils.metaclass_utils import metaclass_resolver

from .imports import OcchabImportMixin


class OcchabModuleMetaclass(type(OcchabImportMixin), type(TModules)):
pass


class OcchabModule(TModules, OcchabImportMixin, metaclass=OcchabModuleMetaclass):
class OcchabModule(metaclass_resolver(TModules, OcchabImportMixin)):
__mapper_args__ = {"polymorphic_identity": "occhab"}

def generate_input_url_for_dataset(self, dataset):
Expand Down

0 comments on commit 0114d1e

Please sign in to comment.