diff --git a/apps/permissions/settings.py b/apps/permissions/settings.py deleted file mode 100644 index 6eabd085d4..0000000000 --- a/apps/permissions/settings.py +++ /dev/null @@ -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') -) diff --git a/apps/smart_settings/__init__.py b/apps/smart_settings/__init__.py index 9070697de8..82f4c8deee 100644 --- a/apps/smart_settings/__init__.py +++ b/apps/smart_settings/__init__.py @@ -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) diff --git a/apps/smart_settings/classes.py b/apps/smart_settings/classes.py index 8d0aa5b692..a5cf8a330e 100644 --- a/apps/smart_settings/classes.py +++ b/apps/smart_settings/classes.py @@ -2,15 +2,21 @@ 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 @@ -21,15 +27,19 @@ 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,8 +49,16 @@ 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: @@ -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]) diff --git a/apps/smart_settings/icons.py b/apps/smart_settings/icons.py new file mode 100644 index 0000000000..6c18f216e4 --- /dev/null +++ b/apps/smart_settings/icons.py @@ -0,0 +1,6 @@ +from __future__ import absolute_import + +from icons.literals import COG +from icons import Icon + +icon_settings = Icon(COG) diff --git a/apps/smart_settings/links.py b/apps/smart_settings/links.py new file mode 100644 index 0000000000..5fcaaf7ab1 --- /dev/null +++ b/apps/smart_settings/links.py @@ -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_']) diff --git a/apps/smart_settings/registry.py b/apps/smart_settings/registry.py new file mode 100644 index 0000000000..1bdf1ad6f5 --- /dev/null +++ b/apps/smart_settings/registry.py @@ -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'] diff --git a/apps/smart_settings/urls.py b/apps/smart_settings/urls.py new file mode 100644 index 0000000000..ecc6ec07ff --- /dev/null +++ b/apps/smart_settings/urls.py @@ -0,0 +1,6 @@ +from django.conf.urls.defaults import patterns, url + +urlpatterns = patterns('smart_settings.views', + url(r'^list/(?P\w+)/$', 'setting_list', (), 'setting_list'), + url(r'^list/$', 'setting_list', (), 'setting_list'), +) diff --git a/apps/smart_settings/views.py b/apps/smart_settings/views.py index 60f00ef0ef..394b2bfee4 100644 --- a/apps/smart_settings/views.py +++ b/apps/smart_settings/views.py @@ -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'%s
%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'
%s %s
' % ( + 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))