factorize loader instance creation (#33563)

This commit is contained in:
Benjamin Dauvergne 2019-05-29 18:56:46 +02:00
parent f3bea0612f
commit 0a7f6f9b5d
1 changed files with 18 additions and 11 deletions

View File

@ -37,27 +37,34 @@ class TenantSettingsWrapper(object):
def clear_tenants_settings(self): def clear_tenants_settings(self):
self.__dict__['tenants_settings'] = {} self.__dict__['tenants_settings'] = {}
@property
def loaders(self):
loaders = getattr(self.default_settings, 'TENANT_SETTINGS_LOADERS', [])
for loader in loaders:
loader_class = import_class(loader)
yield loader_class()
def load_tenant_settings(self, wrapped, tenant, tenant_settings, def load_tenant_settings(self, wrapped, tenant, tenant_settings,
last_time): last_time):
'''Load tenant settings from loaders into tenant_settings object, only '''Load tenant settings from loaders into tenant_settings object, only
if any of the loaders say it is more recent than last update time''' if any of the loaders say it is more recent than last update time'''
settings = self.default_settings
update_time = time.time() update_time = time.time()
if last_time and update_time - last_time < 3: if last_time and update_time - last_time < 3:
return tenant_settings, last_time return tenant_settings, last_time
for loader in getattr(settings, 'TENANT_SETTINGS_LOADERS', []): new = False
loader_class = import_class(loader) for loader in self.loaders:
loader_instance = loader_class() new_time = loader.get_new_time(tenant)
new_time = loader_instance.get_new_time(tenant)
if (not new_time and last_time) \ if (not new_time and last_time) \
or (new_time and not last_time) \ or (new_time and not last_time) \
or (new_time and new_time > last_time): or (new_time and new_time > last_time):
# something is new, call all loaders new = True
for loader in settings.TENANT_SETTINGS_LOADERS: break
loader_class = import_class(loader)
loader_instance = loader_class() if new:
loader_instance.update_settings(tenant_settings, tenant) # something is new, call all loaders
return tenant_settings, update_time for loader in self.loaders:
loader.update_settings(tenant_settings, tenant)
return tenant_settings, update_time
return tenant_settings, last_time return tenant_settings, last_time
def get_tenant_settings(self, wrapped, tenant): def get_tenant_settings(self, wrapped, tenant):