Add set_many methods to kfet_config

- Fix addcost updates (from K-Psul)
This commit is contained in:
Aurélien Delobelle 2017-04-03 23:06:47 +02:00
parent 7384465cbd
commit ce2a05766d
2 changed files with 22 additions and 7 deletions

View file

@ -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()

View file

@ -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,
}
}