<filename>django_lindau/base.py<gh_stars>0
from decimal import Decimal

from django.core.exceptions import ImproperlyConfigured
from django import forms

from django_lindau.models import Settings

DEFAULT_FIELDS = {
    str: forms.CharField,
    int: forms.IntegerField,
    float: forms.FloatField,
    Decimal: forms.DecimalField,
}

class Config(object):

    def __init__(self):
        self._registry = {}

    def __getattr__(self, key):
        setting = self.get_setting(key)
        return setting.value

    def save_setting(self, key, value):
        Settings.objects.filter(key=key).update(value=value)

    def register(self, key, default=None, verbose_name=None, field_class=None):
        '''
        Register a setting
        '''
        if key in dir(self):
            raise ImproperlyConfigured(
                "Setting with key '%s' not allowed, since it's a method or attribute of '%s'."
                % (key, self.__class__.__name__)
            )
        if not field_class:
            _type = type(default)
            field_class = DEFAULT_FIELDS[_type]
        defaults = dict(value=default)
        self._registry[key] = dict(defaults=defaults, verbose_name=verbose_name, field_class=field_class)

    def get_setting(self, key):
        if key in self._registry:
            defaults = self._registry[key]['defaults']
            setting, created = Settings.objects.get_or_create(key=key, defaults=defaults)
            return setting
        raise ImproperlyConfigured(
            "Setting '%s' has not been registered." % (key,)
        )
<filename>django_lindau/base.py<gh_stars>0
from decimal import Decimal

from django.core.exceptions import ImproperlyConfigured
from django import forms

from django_lindau.models import Settings

DEFAULT_FIELDS = {
    str: forms.CharField,
    int: forms.IntegerField,
    float: forms.FloatField,
    Decimal: forms.DecimalField,
}

class Config(object):

    def __init__(self):
        self._registry = {}

    def __getattr__(self, key):
        setting = self.get_setting(key)
        return setting.value

    def save_setting(self, key, value):
        Settings.objects.filter(key=key).update(value=value)

    def register(self, key, default=None, verbose_name=None, field_class=None):
        '''
        Register a setting
        '''
        if key in dir(self):
            raise ImproperlyConfigured(
                "Setting with key '%s' not allowed, since it's a method or attribute of '%s'."
                % (key, self.__class__.__name__)
            )
        if not field_class:
            _type = type(default)
            field_class = DEFAULT_FIELDS[_type]
        defaults = dict(value=default)
        self._registry[key] = dict(defaults=defaults, verbose_name=verbose_name, field_class=field_class)

    def get_setting(self, key):
        if key in self._registry:
            defaults = self._registry[key]['defaults']
            setting, created = Settings.objects.get_or_create(key=key, defaults=defaults)
            return setting
        raise ImproperlyConfigured(
            "Setting '%s' has not been registered." % (key,)
        )
Register a setting