You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
padelclub_backend/sync/registry.py

39 lines
1.3 KiB

from django.conf import settings
from django.apps import apps
from .models import BaseModel
from django.contrib.auth import get_user_model
User = get_user_model()
class SyncRegistry:
def __init__(self):
self._registry = {}
def load_sync_apps(self):
sync_apps = getattr(settings, 'SYNC_APPS', {})
for app_label, config in sync_apps.items():
app_models = apps.get_app_config(app_label).get_models()
for model in app_models:
if hasattr(model, '_meta') and not model._meta.abstract:
if issubclass(model, BaseModel) or model == User:
model_name = model.__name__
if self.should_sync_model(model_name, config):
self.register(model)
def should_sync_model(self, model_name, config):
if 'exclude' in config and model_name in config['exclude']:
return False
if 'models' in config and config['models']:
return model_name in config['models']
return True
def register(self, model):
self._registry[model.__name__] = model
def get_model(self, model_name):
if not self._registry:
self.load_sync_apps()
return self._registry.get(model_name)
# Create singleton instance
sync_registry = SyncRegistry()