Add version attribute to namespaces
Also dump the version of all the namespaces as children of SMART_SETTINGS_NAMESPACES. Signed-off-by: Roberto Rosario <roberto.rosario@mayan-edms.com>
This commit is contained in:
@@ -33,6 +33,8 @@
|
|||||||
- Backport Docker composer makefile targets.
|
- Backport Docker composer makefile targets.
|
||||||
- Add PermissionTestCaseMixin and SmartSettingTestCaseMixin to better
|
- Add PermissionTestCaseMixin and SmartSettingTestCaseMixin to better
|
||||||
organize cache invalidation of both apps for tests.
|
organize cache invalidation of both apps for tests.
|
||||||
|
- Add a version attribute to setting namespace. These are dumped
|
||||||
|
as SMART_SETTINGS_NAMESPACES.
|
||||||
|
|
||||||
3.2.8 (2019-10-01)
|
3.2.8 (2019-10-01)
|
||||||
==================
|
==================
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ from django.utils.encoding import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
SMART_SETTINGS_NAMESPACES_NAME = 'SMART_SETTINGS_NAMESPACES'
|
||||||
|
|
||||||
|
|
||||||
@python_2_unicode_compatible
|
@python_2_unicode_compatible
|
||||||
@@ -40,26 +41,35 @@ class Namespace(object):
|
|||||||
exception
|
exception
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get(cls, name):
|
||||||
|
return cls._registry[name]
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_all(cls):
|
def get_all(cls):
|
||||||
return sorted(cls._registry.values(), key=lambda x: x.label)
|
return sorted(cls._registry.values(), key=lambda x: x.label)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get(cls, name):
|
def get_namespace_config(cls, name):
|
||||||
return cls._registry[name]
|
return cls.get_namespaces_config().get(name, {})
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_namespaces_config(cls):
|
||||||
|
return getattr(settings, SMART_SETTINGS_NAMESPACES_NAME, {})
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def invalidate_cache_all(cls):
|
def invalidate_cache_all(cls):
|
||||||
for namespace in cls.get_all():
|
for namespace in cls.get_all():
|
||||||
namespace.invalidate_cache()
|
namespace.invalidate_cache()
|
||||||
|
|
||||||
def __init__(self, name, label):
|
def __init__(self, name, label, version='0001'):
|
||||||
if name in self.__class__._registry:
|
if name in self.__class__._registry:
|
||||||
raise Exception(
|
raise Exception(
|
||||||
'Namespace names must be unique; "%s" already exists.' % name
|
'Namespace names must be unique; "%s" already exists.' % name
|
||||||
)
|
)
|
||||||
self.name = name
|
self.name = name
|
||||||
self.label = label
|
self.label = label
|
||||||
|
self.version = version
|
||||||
self.__class__._registry[name] = self
|
self.__class__._registry[name] = self
|
||||||
self._settings = []
|
self._settings = []
|
||||||
|
|
||||||
@@ -69,6 +79,9 @@ class Namespace(object):
|
|||||||
def add_setting(self, **kwargs):
|
def add_setting(self, **kwargs):
|
||||||
return Setting(namespace=self, **kwargs)
|
return Setting(namespace=self, **kwargs)
|
||||||
|
|
||||||
|
def get_config_version(self):
|
||||||
|
return Namespace.get_namespace_config(name=self.name).get('version', None)
|
||||||
|
|
||||||
def invalidate_cache(self):
|
def invalidate_cache(self):
|
||||||
for setting in self._settings:
|
for setting in self._settings:
|
||||||
setting.invalidate_cache()
|
setting.invalidate_cache()
|
||||||
@@ -123,7 +136,18 @@ class Setting(object):
|
|||||||
def dump_data(cls, filter_term=None, namespace=None):
|
def dump_data(cls, filter_term=None, namespace=None):
|
||||||
dictionary = {}
|
dictionary = {}
|
||||||
|
|
||||||
|
if not namespace:
|
||||||
|
namespace_dictionary = {}
|
||||||
|
for _namespace in Namespace.get_all():
|
||||||
|
namespace_dictionary[_namespace.name] = {
|
||||||
|
'version': _namespace.version
|
||||||
|
}
|
||||||
|
|
||||||
|
dictionary[SMART_SETTINGS_NAMESPACES_NAME] = namespace_dictionary
|
||||||
|
|
||||||
for setting in cls.get_all():
|
for setting in cls.get_all():
|
||||||
|
# If a namespace is specified, filter the list by that namespace
|
||||||
|
# otherwise return always True to include all (or not None == True)
|
||||||
if (namespace and setting.namespace.name == namespace) or not namespace:
|
if (namespace and setting.namespace.name == namespace) or not namespace:
|
||||||
if (filter_term and filter_term.lower() in setting.global_name.lower()) or not filter_term:
|
if (filter_term and filter_term.lower() in setting.global_name.lower()) or not filter_term:
|
||||||
dictionary[setting.global_name] = Setting.express_promises(setting.value)
|
dictionary[setting.global_name] = Setting.express_promises(setting.value)
|
||||||
@@ -147,9 +171,12 @@ class Setting(object):
|
|||||||
)
|
)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def save_configuration(cls, path=settings.CONFIGURATION_FILEPATH):
|
def save_configuration(cls, path=None):
|
||||||
|
if not path:
|
||||||
|
path = settings.CONFIGURATION_FILEPATH
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(path, 'w') as file_object:
|
with open(path, mode='w') as file_object:
|
||||||
file_object.write(cls.dump_data())
|
file_object.write(cls.dump_data())
|
||||||
except IOError as exception:
|
except IOError as exception:
|
||||||
if exception.errno == errno.ENOENT:
|
if exception.errno == errno.ENOENT:
|
||||||
|
|||||||
Reference in New Issue
Block a user