2017-04-03 20:32:16 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2017-10-12 13:53:48 +02:00
|
|
|
from datetime import timedelta
|
|
|
|
from decimal import Decimal
|
2017-04-03 20:32:16 +02:00
|
|
|
|
2017-10-12 13:53:48 +02:00
|
|
|
from django import forms
|
2017-04-03 23:06:47 +02:00
|
|
|
from django.core.exceptions import ValidationError
|
2017-04-04 17:05:55 +02:00
|
|
|
from django.db import models
|
2017-04-03 23:06:47 +02:00
|
|
|
|
2017-04-03 20:32:16 +02:00
|
|
|
from djconfig import config
|
2017-10-12 13:53:48 +02:00
|
|
|
from djconfig.forms import ConfigForm
|
|
|
|
|
|
|
|
from .models import Account
|
2017-04-03 20:32:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
class KFetConfig(object):
|
|
|
|
"""kfet app configuration.
|
|
|
|
|
2017-04-03 23:06:47 +02:00
|
|
|
Enhance isolation with backend used to store config.
|
2017-04-03 20:32:16 +02:00
|
|
|
Usable after DjConfig middleware was called.
|
|
|
|
|
|
|
|
"""
|
|
|
|
prefix = 'kfet_'
|
|
|
|
|
|
|
|
def __getattr__(self, key):
|
2017-04-10 11:36:06 +02:00
|
|
|
if key == 'subvention_cof':
|
|
|
|
# Allows accessing to the reduction as a subvention
|
|
|
|
# Other reason: backward compatibility
|
2017-10-12 13:53:48 +02:00
|
|
|
reduction_mult = 1 - self.reduction_cof / 100
|
|
|
|
return (1 / reduction_mult - 1) * 100
|
2017-04-04 17:05:55 +02:00
|
|
|
return getattr(config, self._get_dj_key(key))
|
2017-04-03 20:32:16 +02:00
|
|
|
|
|
|
|
def list(self):
|
2017-04-04 17:05:55 +02:00
|
|
|
"""Get list of kfet app configuration.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
(key, value) for each configuration entry as list.
|
|
|
|
|
|
|
|
"""
|
2017-04-03 20:32:16 +02:00
|
|
|
return [(field.label, getattr(config, name), )
|
|
|
|
for name, field in KFetConfigForm.base_fields.items()]
|
|
|
|
|
2017-04-04 17:05:55 +02:00
|
|
|
def _get_dj_key(self, key):
|
2017-04-03 23:06:47 +02:00
|
|
|
return '{}{}'.format(self.prefix, key)
|
|
|
|
|
2017-04-04 17:05:55 +02:00
|
|
|
def set(self, **kwargs):
|
|
|
|
"""Update configuration value(s).
|
|
|
|
|
|
|
|
Args:
|
|
|
|
**kwargs: Keyword arguments. Keys must be in kfet config.
|
|
|
|
Config entries are updated to given values.
|
|
|
|
|
|
|
|
"""
|
|
|
|
# get old config
|
2017-04-03 23:06:47 +02:00
|
|
|
new_cfg = KFetConfigForm().initial
|
2017-04-04 17:05:55 +02:00
|
|
|
# update to new config
|
|
|
|
for key, value in kwargs.items():
|
|
|
|
dj_key = self._get_dj_key(key)
|
|
|
|
if isinstance(value, models.Model):
|
|
|
|
new_cfg[dj_key] = value.pk
|
|
|
|
else:
|
|
|
|
new_cfg[dj_key] = value
|
|
|
|
# save new config
|
2017-04-03 23:06:47 +02:00
|
|
|
cfg_form = KFetConfigForm(new_cfg)
|
|
|
|
if cfg_form.is_valid():
|
|
|
|
cfg_form.save()
|
|
|
|
else:
|
2017-04-04 17:05:55 +02:00
|
|
|
raise ValidationError(
|
|
|
|
'Invalid values in kfet_config.set: %(fields)s',
|
|
|
|
params={'fields': list(cfg_form.errors)})
|
2017-04-03 23:06:47 +02:00
|
|
|
|
2017-04-03 20:32:16 +02:00
|
|
|
|
|
|
|
kfet_config = KFetConfig()
|
2017-10-12 13:53:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
class KFetConfigForm(ConfigForm):
|
|
|
|
|
|
|
|
kfet_reduction_cof = forms.DecimalField(
|
|
|
|
label='Réduction COF', initial=Decimal('20'),
|
|
|
|
max_digits=6, decimal_places=2,
|
|
|
|
help_text="Réduction, à donner en pourcentage, appliquée lors d'un "
|
|
|
|
"achat par un-e membre du COF sur le montant en euros.",
|
|
|
|
)
|
|
|
|
kfet_addcost_amount = forms.DecimalField(
|
|
|
|
label='Montant de la majoration (en €)', initial=Decimal('0'),
|
|
|
|
required=False,
|
|
|
|
max_digits=6, decimal_places=2,
|
|
|
|
)
|
|
|
|
kfet_addcost_for = forms.ModelChoiceField(
|
|
|
|
label='Destinataire de la majoration', initial=None, required=False,
|
|
|
|
help_text='Laissez vide pour désactiver la majoration.',
|
|
|
|
queryset=(Account.objects
|
|
|
|
.select_related('cofprofile', 'cofprofile__user')
|
|
|
|
.all()),
|
|
|
|
)
|
|
|
|
kfet_overdraft_duration = forms.DurationField(
|
|
|
|
label='Durée du découvert autorisé par défaut',
|
|
|
|
initial=timedelta(days=1),
|
|
|
|
)
|
|
|
|
kfet_overdraft_amount = forms.DecimalField(
|
|
|
|
label='Montant du découvert autorisé par défaut (en €)',
|
|
|
|
initial=Decimal('20'),
|
|
|
|
max_digits=6, decimal_places=2,
|
|
|
|
)
|
|
|
|
kfet_cancel_duration = forms.DurationField(
|
|
|
|
label='Durée pour annuler une commande sans mot de passe',
|
|
|
|
initial=timedelta(minutes=5),
|
|
|
|
)
|