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 project_setup.api import register_setup
|
||||
|
||||
from .classes import SettingsNamespace, LocalScope
|
||||
from .links import link_settings
|
||||
|
||||
register_setup(link_settings)
|
||||
|
||||
@@ -2,16 +2,22 @@ from __future__ import absolute_import
|
||||
|
||||
from django.conf import settings
|
||||
from django.utils.importlib import import_module
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
|
||||
# Namespace
|
||||
class SettingsNamespace(object):
|
||||
_registry = {}
|
||||
_settings = {}
|
||||
|
||||
@classmethod
|
||||
def get_all(cls):
|
||||
return cls._registry.values()
|
||||
|
||||
@classmethod
|
||||
def get(cls, name):
|
||||
return cls._registry.get(name)
|
||||
|
||||
def __init__(self, name, label, module):
|
||||
self.name = name
|
||||
self.label = label
|
||||
@@ -22,14 +28,18 @@ class SettingsNamespace(object):
|
||||
def __unicode__(self):
|
||||
return unicode(self.label)
|
||||
|
||||
def settings(self):
|
||||
#return [setting for setting in settings_list if setting.namespace == self]
|
||||
return (setting for setting in Setting.get_all() if setting.namespace == self)
|
||||
def register(self, setting):
|
||||
self.__class__._settings.setdefault(self.name, {})
|
||||
self.__class__._settings[self.name][setting.name] = setting
|
||||
|
||||
def add_setting(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):
|
||||
def get_value(self):
|
||||
raise NotImplemented
|
||||
@@ -39,9 +49,17 @@ class LocalScope(SettingScope):
|
||||
"""
|
||||
Return the value of a config value from the local settings.py file
|
||||
"""
|
||||
label = _(u'Local')
|
||||
|
||||
def __init__(self, global_name=None):
|
||||
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):
|
||||
if not self.global_name:
|
||||
self.global_name = '%s_%s' % (self.setting.namespace.name.upper(), self.setting.name)
|
||||
@@ -55,12 +73,6 @@ class LocalScope(SettingScope):
|
||||
|
||||
# Settings
|
||||
class Setting(object):
|
||||
_registry = {}
|
||||
|
||||
@classmethod
|
||||
def get_all(cls):
|
||||
return cls._registry.values()
|
||||
|
||||
def register_scope(self, scope):
|
||||
"""
|
||||
Store this setting's instance into the scope instance and append
|
||||
@@ -73,7 +85,7 @@ class Setting(object):
|
||||
self.namespace = namespace
|
||||
self.name = name
|
||||
self.default = default
|
||||
self.description = description or u''
|
||||
self.description = description
|
||||
self.hidden = hidden
|
||||
self.exists = exists
|
||||
self.scopes = []
|
||||
@@ -105,11 +117,8 @@ class Setting(object):
|
||||
self.module = import_module(namespace.module)
|
||||
setattr(self.module, name, self.get_value())
|
||||
|
||||
self.__class__._registry.setdefault(self.namespace.name, {})
|
||||
self.__class__._registry[self.namespace.name][self.name] = self
|
||||
#settings_list.append(self)
|
||||
#settings.setdefault(self.namespace.name, [])
|
||||
#settings[self.namespace.name].append(self)
|
||||
# Register with the namespace
|
||||
self.namespace.register(self)
|
||||
|
||||
def get_value(self):
|
||||
value = self.default
|
||||
@@ -120,3 +129,6 @@ class Setting(object):
|
||||
pass
|
||||
|
||||
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