diff --git a/kfet/config.py b/kfet/config.py index deb12504..8e6e0901 100644 --- a/kfet/config.py +++ b/kfet/config.py @@ -1,25 +1,40 @@ # -*- coding: utf-8 -*- +from django.core.exceptions import ValidationError + from djconfig import config class KFetConfig(object): """kfet app configuration. - Enhance dependency with backend used to store config. + Enhance isolation with backend used to store config. Usable after DjConfig middleware was called. """ prefix = 'kfet_' def __getattr__(self, key): - dj_key = '{}{}'.format(self.prefix, key) - return getattr(config, dj_key) + return getattr(config, self.get_dj_key(key)) def list(self): from kfet.forms import KFetConfigForm return [(field.label, getattr(config, name), ) for name, field in KFetConfigForm.base_fields.items()] + def get_dj_key(self, key): + return '{}{}'.format(self.prefix, key) + + def set_many(self, **kwargs): + from kfet.forms import KFetConfigForm + new_cfg = KFetConfigForm().initial + new_cfg.update({self.get_dj_key(key): value + for key, value in kwargs.items()}) + cfg_form = KFetConfigForm(new_cfg) + if cfg_form.is_valid(): + cfg_form.save() + else: + raise ValidationError() + kfet_config = KFetConfig() diff --git a/kfet/views.py b/kfet/views.py index 96d2a183..34e640b3 100644 --- a/kfet/views.py +++ b/kfet/views.py @@ -944,12 +944,12 @@ def kpsul_update_addcost(request): account = trigramme and Account.objects.get(trigramme=trigramme) or None amount = addcost_form.cleaned_data['amount'] - cfg_form = KFetConfigForm(kfet_addcost_for=account, - kfet_addcost_amount=amount) - cfg_form.save() + kfet_config.set_many(addcost_for=account and account.pk or None, + addcost_amount=amount) + data = { 'addcost': { - 'for': trigramme and account.trigramme or None, + 'for': account and account.trigramme or None, 'amount': amount, } }