Initial changes to support the ClusertScope

This commit is contained in:
Roberto Rosario
2012-09-11 12:58:45 -04:00
parent 66f451b23f
commit ecb237ccd7
2 changed files with 59 additions and 8 deletions

View File

@@ -4,6 +4,7 @@ from django.conf import settings
from django.utils.importlib import import_module from django.utils.importlib import import_module
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from .models import ClusterSetting
# Namespace # Namespace
class SettingsNamespace(object): class SettingsNamespace(object):
@@ -43,6 +44,15 @@ class SettingsNamespace(object):
class SettingScope(object): class SettingScope(object):
def get_value(self): def get_value(self):
raise NotImplemented raise NotImplemented
def set_value(self):
raise NotImplemented
def register_setting(self, setting):
self.setting = self
def get_full_name(self):
return '%s_%s' % (self.setting.namespace.name, self.setting.name)
class LocalScope(SettingScope): class LocalScope(SettingScope):
@@ -62,12 +72,39 @@ class LocalScope(SettingScope):
def get_value(self): def get_value(self):
if not self.global_name: if not self.global_name:
self.global_name = '%s_%s' % (self.setting.namespace.name.upper(), self.setting.name) #self.global_name = '%s_%s' % (self.setting.namespace.name.upper(), self.setting.name)
self.global_name = self.get_full_name().upper()
return getattr(settings, self.global_name)
class ClusterScope(SettingScope):
"""
Return the value of a config value from the local settings.py file
"""
label = _(u'Cluster')
#def __init__(self):
# self.global_name = global_name
def __unicode__(self):
return unicode(self.__class__.label)
def __repr__(self):
return unicode(self.__unicode__())
def get_value(self):
#if not self.global_name:
# self.global_name = '%s_%s' % (self.setting.namespace.name.upper(), self.setting.name)
#ClusterSetting.objects.
return None
return getattr(settings, self.global_name) return getattr(settings, self.global_name)
def register_setting(self, *args, **kwargs):
super(ClusterScope, self).register_setting(*args, **kwargs)
cluster_settings = ClusterSetting.objects.get_or_create(name=self.get_full_name(), defaults={'value': getattr(self.setting, 'default', None)})
# TODO: Cluster - Cluster wide setting
# TODO: Organization - Organizaition wide preferences # TODO: Organization - Organizaition wide preferences
# TODO: User - user preferences # TODO: User - user preferences
@@ -78,7 +115,7 @@ class Setting(object):
Store this setting's instance into the scope instance and append Store this setting's instance into the scope instance and append
the scope to this setting's scope list the scope to this setting's scope list
""" """
scope.setting = self scope.register_setting(self)
self.scopes.append(scope) self.scopes.append(scope)
def __init__(self, namespace, name, default, description=None, hidden=False, exists=False, scopes=None): def __init__(self, namespace, name, default, description=None, hidden=False, exists=False, scopes=None):
@@ -93,8 +130,6 @@ class Setting(object):
if scopes: if scopes:
for scope in scopes: for scope in scopes:
self.register_scope(scope) self.register_scope(scope)
#else:
# self.scopes = [] #Local('GLOBAL_%s' % self.app.name)]
# Create the local entity # Create the local entity
try: try:

View File

@@ -1,3 +1,19 @@
from django.db import models from __future__ import absolute_import
# Create your models here. from django.db import models
from django.utils.translation import ugettext_lazy as _
class ClusterSetting(models.Model):
"""
Define a setting entry common to all nodes in a cluster
"""
name = models.CharField(unique=True, max_length=64, verbose_name=_(u'name'))
value = models.TextField(blank=True, verbose_name=_(u'value'))
def __unicode__(self):
return self.name
class Meta:
verbose_name = _(u'cluster setting')
verbose_name_plural = _(u'cluster settings')