2022-10-19 17:10:30 +03:00
|
|
|
class Registry:
|
2016-09-18 02:29:00 +03:00
|
|
|
def __init__(self):
|
|
|
|
self._registry = {}
|
2017-04-21 20:25:30 +03:00
|
|
|
self._field_registry = {}
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
def register(self, cls):
|
2024-11-09 12:41:25 +03:00
|
|
|
from .types import DjangoObjectType, DjangoUnionType
|
2018-07-20 02:51:33 +03:00
|
|
|
|
2016-09-18 03:09:56 +03:00
|
|
|
assert issubclass(
|
2024-11-09 12:41:25 +03:00
|
|
|
cls, (DjangoObjectType, DjangoUnionType)
|
|
|
|
), f'Only DjangoObjectTypes or DjangoUnionType can be registered, received "{cls.__name__}"'
|
2018-07-20 02:51:33 +03:00
|
|
|
assert cls._meta.registry == self, "Registry for a Model have to match."
|
2016-09-18 03:09:56 +03:00
|
|
|
# assert self.get_type_for_model(cls._meta.model) == cls, (
|
|
|
|
# 'Multiple DjangoObjectTypes registered for "{}"'.format(cls._meta.model)
|
|
|
|
# )
|
2018-07-20 02:51:33 +03:00
|
|
|
if not getattr(cls._meta, "skip_registry", False):
|
2017-02-02 22:12:51 +03:00
|
|
|
self._registry[cls._meta.model] = cls
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
def get_type_for_model(self, model):
|
|
|
|
return self._registry.get(model)
|
|
|
|
|
2017-04-21 20:25:30 +03:00
|
|
|
def register_converted_field(self, field, converted):
|
|
|
|
self._field_registry[field] = converted
|
|
|
|
|
|
|
|
def get_converted_field(self, field):
|
|
|
|
return self._field_registry.get(field)
|
|
|
|
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
registry = None
|
|
|
|
|
|
|
|
|
|
|
|
def get_global_registry():
|
|
|
|
global registry
|
|
|
|
if not registry:
|
|
|
|
registry = Registry()
|
|
|
|
return registry
|
|
|
|
|
|
|
|
|
|
|
|
def reset_global_registry():
|
|
|
|
global registry
|
|
|
|
registry = None
|