Smart settings app updates
This commit is contained in:
@@ -1,14 +0,0 @@
|
|||||||
"""Configuration options for the permissions app"""
|
|
||||||
from django.utils.translation import ugettext_lazy as _
|
|
||||||
|
|
||||||
from smart_settings.api import Setting, SettingNamespace
|
|
||||||
|
|
||||||
namespace = SettingNamespace('permissions', _(u'Permissions'), module='permissions.conf.settings', sprite='key')
|
|
||||||
|
|
||||||
Setting(
|
|
||||||
namespace=namespace,
|
|
||||||
name='DEFAULT_ROLES',
|
|
||||||
global_name='ROLES_DEFAULT_ROLES',
|
|
||||||
default=[],
|
|
||||||
description=_(u'A list of existing roles that are automatically assigned to newly created users')
|
|
||||||
)
|
|
||||||
@@ -1,3 +1,8 @@
|
|||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
|
|
||||||
|
from project_setup.api import register_setup
|
||||||
|
|
||||||
from .classes import SettingsNamespace, LocalScope
|
from .classes import SettingsNamespace, LocalScope
|
||||||
|
from .links import link_settings
|
||||||
|
|
||||||
|
register_setup(link_settings)
|
||||||
|
|||||||
@@ -2,15 +2,21 @@ from __future__ import absolute_import
|
|||||||
|
|
||||||
from django.conf import settings
|
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 _
|
||||||
|
|
||||||
|
|
||||||
# Namespace
|
# Namespace
|
||||||
class SettingsNamespace(object):
|
class SettingsNamespace(object):
|
||||||
_registry = {}
|
_registry = {}
|
||||||
|
_settings = {}
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_all(cls):
|
def get_all(cls):
|
||||||
return cls._registry.values()
|
return cls._registry.values()
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get(cls, name):
|
||||||
|
return cls._registry.get(name)
|
||||||
|
|
||||||
def __init__(self, name, label, module):
|
def __init__(self, name, label, module):
|
||||||
self.name = name
|
self.name = name
|
||||||
@@ -21,15 +27,19 @@ class SettingsNamespace(object):
|
|||||||
|
|
||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
return unicode(self.label)
|
return unicode(self.label)
|
||||||
|
|
||||||
def settings(self):
|
def register(self, setting):
|
||||||
#return [setting for setting in settings_list if setting.namespace == self]
|
self.__class__._settings.setdefault(self.name, {})
|
||||||
return (setting for setting in Setting.get_all() if setting.namespace == self)
|
self.__class__._settings[self.name][setting.name] = setting
|
||||||
|
|
||||||
def add_setting(self, *args, **kwargs):
|
def add_setting(self, *args, **kwargs):
|
||||||
return Setting(namespace=self, *args, **kwargs)
|
return Setting(namespace=self, *args, **kwargs)
|
||||||
|
|
||||||
# Realms
|
def get_settings(self):
|
||||||
|
return self.__class__._settings[self.name].values()
|
||||||
|
|
||||||
|
|
||||||
|
# Scopes
|
||||||
class SettingScope(object):
|
class SettingScope(object):
|
||||||
def get_value(self):
|
def get_value(self):
|
||||||
raise NotImplemented
|
raise NotImplemented
|
||||||
@@ -39,8 +49,16 @@ class LocalScope(SettingScope):
|
|||||||
"""
|
"""
|
||||||
Return the value of a config value from the local settings.py file
|
Return the value of a config value from the local settings.py file
|
||||||
"""
|
"""
|
||||||
|
label = _(u'Local')
|
||||||
|
|
||||||
def __init__(self, global_name=None):
|
def __init__(self, global_name=None):
|
||||||
self.global_name = global_name
|
self.global_name = global_name
|
||||||
|
|
||||||
|
def __unicode__(self):
|
||||||
|
return u'%s: %s' % (self.__class__.label, self.global_name)
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return unicode(self.__unicode__())
|
||||||
|
|
||||||
def get_value(self):
|
def get_value(self):
|
||||||
if not self.global_name:
|
if not self.global_name:
|
||||||
@@ -55,12 +73,6 @@ class LocalScope(SettingScope):
|
|||||||
|
|
||||||
# Settings
|
# Settings
|
||||||
class Setting(object):
|
class Setting(object):
|
||||||
_registry = {}
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def get_all(cls):
|
|
||||||
return cls._registry.values()
|
|
||||||
|
|
||||||
def register_scope(self, scope):
|
def register_scope(self, scope):
|
||||||
"""
|
"""
|
||||||
Store this setting's instance into the scope instance and append
|
Store this setting's instance into the scope instance and append
|
||||||
@@ -73,7 +85,7 @@ class Setting(object):
|
|||||||
self.namespace = namespace
|
self.namespace = namespace
|
||||||
self.name = name
|
self.name = name
|
||||||
self.default = default
|
self.default = default
|
||||||
self.description = description or u''
|
self.description = description
|
||||||
self.hidden = hidden
|
self.hidden = hidden
|
||||||
self.exists = exists
|
self.exists = exists
|
||||||
self.scopes = []
|
self.scopes = []
|
||||||
@@ -105,11 +117,8 @@ class Setting(object):
|
|||||||
self.module = import_module(namespace.module)
|
self.module = import_module(namespace.module)
|
||||||
setattr(self.module, name, self.get_value())
|
setattr(self.module, name, self.get_value())
|
||||||
|
|
||||||
self.__class__._registry.setdefault(self.namespace.name, {})
|
# Register with the namespace
|
||||||
self.__class__._registry[self.namespace.name][self.name] = self
|
self.namespace.register(self)
|
||||||
#settings_list.append(self)
|
|
||||||
#settings.setdefault(self.namespace.name, [])
|
|
||||||
#settings[self.namespace.name].append(self)
|
|
||||||
|
|
||||||
def get_value(self):
|
def get_value(self):
|
||||||
value = self.default
|
value = self.default
|
||||||
@@ -120,3 +129,6 @@ class Setting(object):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
def get_scopes_display(self):
|
||||||
|
return u', '.join([unicode(scope) for scope in self.scopes])
|
||||||
|
|||||||
6
apps/smart_settings/icons.py
Normal file
6
apps/smart_settings/icons.py
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
from __future__ import absolute_import
|
||||||
|
|
||||||
|
from icons.literals import COG
|
||||||
|
from icons import Icon
|
||||||
|
|
||||||
|
icon_settings = Icon(COG)
|
||||||
12
apps/smart_settings/links.py
Normal file
12
apps/smart_settings/links.py
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
from __future__ import absolute_import
|
||||||
|
|
||||||
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
|
||||||
|
from navigation.api import Link
|
||||||
|
|
||||||
|
from .icons import icon_settings
|
||||||
|
|
||||||
|
def is_superuser(context):
|
||||||
|
return context['request'].user.is_staff or context['request'].user.is_superuser
|
||||||
|
|
||||||
|
link_settings = Link(text=_(u'settings'), view='setting_list', icon=icon_settings, condition=is_superuser, children_view_regex=[r'^setting_'])
|
||||||
11
apps/smart_settings/registry.py
Normal file
11
apps/smart_settings/registry.py
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
from __future__ import absolute_import
|
||||||
|
|
||||||
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
|
||||||
|
from .icons import icon_settings
|
||||||
|
|
||||||
|
name = 'smart_settings'
|
||||||
|
label = _(u'Smart settings')
|
||||||
|
description = _(u'Handles the configuration settings of all apps')
|
||||||
|
icon = icon_settings
|
||||||
|
dependencies = ['app_registry']
|
||||||
6
apps/smart_settings/urls.py
Normal file
6
apps/smart_settings/urls.py
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
from django.conf.urls.defaults import patterns, url
|
||||||
|
|
||||||
|
urlpatterns = patterns('smart_settings.views',
|
||||||
|
url(r'^list/(?P<app_name>\w+)/$', 'setting_list', (), 'setting_list'),
|
||||||
|
url(r'^list/$', 'setting_list', (), 'setting_list'),
|
||||||
|
)
|
||||||
@@ -1 +1,62 @@
|
|||||||
# Create your views here.
|
from __future__ import absolute_import
|
||||||
|
|
||||||
|
from django.shortcuts import render_to_response, get_object_or_404
|
||||||
|
from django.template import RequestContext
|
||||||
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
from django.utils.safestring import mark_safe
|
||||||
|
|
||||||
|
from common.utils import return_type, encapsulate
|
||||||
|
from common.widgets import exists_with_famfam
|
||||||
|
from navigation.api import Link
|
||||||
|
from app_registry.models import App
|
||||||
|
|
||||||
|
from .classes import SettingsNamespace
|
||||||
|
from .links import is_superuser
|
||||||
|
|
||||||
|
|
||||||
|
def setting_list(request, app_name=None, object_list=None, title=None, extra_context=None):
|
||||||
|
#TODO: check user is super user
|
||||||
|
namespace_links = []
|
||||||
|
|
||||||
|
for app in App.live.filter(name__in=[namespace.name for namespace in SettingsNamespace.get_all()]):
|
||||||
|
namespace_links.append(
|
||||||
|
Link(text=app.label, view='setting_list', args=[u'"%s"' % app.name], icon=getattr(app, 'icon') or icon_settings, condition=is_superuser, children_view_regex=[r'^setting_'])
|
||||||
|
)
|
||||||
|
|
||||||
|
if app_name:
|
||||||
|
app = get_object_or_404(App, name=app_name)
|
||||||
|
selected_namespace = SettingsNamespace.get(app_name)
|
||||||
|
app_settings = selected_namespace.get_settings()
|
||||||
|
title = _(u'settings for the app: %s') % app_name
|
||||||
|
else:
|
||||||
|
object_list = []
|
||||||
|
|
||||||
|
context = {
|
||||||
|
'title': title if title else _(u'settings'),
|
||||||
|
'object_list': object_list if not (object_list is None) else [setting for setting in selected_namespace.get_settings() if setting.hidden == False],
|
||||||
|
'hide_link': True,
|
||||||
|
'hide_object': True,
|
||||||
|
'extra_columns': [
|
||||||
|
{'name': _(u'name'), 'attribute': encapsulate(lambda x: mark_safe(u'<span style="font-weight: bold;">%s</span><br />%s' % (x.name, x.description or u'')))},
|
||||||
|
{'name': _(u'scopes'), 'attribute': 'get_scopes_display'},
|
||||||
|
{'name': _(u'default'), 'attribute': encapsulate(lambda x: return_type(x.default))},
|
||||||
|
{'name': _(u'value'), 'attribute': encapsulate(lambda x: mark_safe(u'<div class="nowrap">%s %s</div>' % (
|
||||||
|
return_type(getattr(x.module, x.name)),
|
||||||
|
exists_with_famfam(getattr(x.module, x.name)) if x.exists else ''
|
||||||
|
)))
|
||||||
|
},
|
||||||
|
],
|
||||||
|
'temporary_navigation_links': {
|
||||||
|
'form_header': {
|
||||||
|
'setting_list': {
|
||||||
|
'links': namespace_links
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if extra_context:
|
||||||
|
context.update(extra_context)
|
||||||
|
|
||||||
|
return render_to_response('generic_list.html', context,
|
||||||
|
context_instance=RequestContext(request))
|
||||||
|
|||||||
Reference in New Issue
Block a user