From df7594a105c1f6da904f10a90de7861be69c09fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Delobelle?= Date: Thu, 12 Oct 2017 13:53:48 +0200 Subject: [PATCH] Move KFetConfigForm to kfet.config Import in `ready` method of kfet app config of `kfet.forms` may be annoying because it starts executing `__init__` methods of fields. Causing failures if these methods does DB calls, as `ready` may be called before applying migrations. --- kfet/apps.py | 8 +--- kfet/config.py | 50 +++++++++++++++++++++---- kfet/forms.py | 44 ---------------------- kfet/migrations/0054_delete_settings.py | 2 +- kfet/models.py | 3 +- kfet/utils.py | 3 +- kfet/views.py | 4 +- 7 files changed, 51 insertions(+), 63 deletions(-) diff --git a/kfet/apps.py b/kfet/apps.py index 4f114c37..157f6d02 100644 --- a/kfet/apps.py +++ b/kfet/apps.py @@ -1,11 +1,7 @@ # -*- coding: utf-8 -*- - -from __future__ import (absolute_import, division, - print_function, unicode_literals) -from builtins import * - from django.apps import AppConfig + class KFetConfig(AppConfig): name = 'kfet' verbose_name = "Application K-Fêt" @@ -15,5 +11,5 @@ class KFetConfig(AppConfig): def register_config(self): import djconfig - from kfet.forms import KFetConfigForm + from .config import KFetConfigForm djconfig.register(KFetConfigForm) diff --git a/kfet/config.py b/kfet/config.py index 76da5a79..c8470093 100644 --- a/kfet/config.py +++ b/kfet/config.py @@ -1,9 +1,15 @@ # -*- coding: utf-8 -*- +from datetime import timedelta +from decimal import Decimal +from django import forms from django.core.exceptions import ValidationError from django.db import models from djconfig import config +from djconfig.forms import ConfigForm + +from .models import Account class KFetConfig(object): @@ -19,8 +25,8 @@ class KFetConfig(object): if key == 'subvention_cof': # Allows accessing to the reduction as a subvention # Other reason: backward compatibility - reduction_mult = 1 - self.reduction_cof/100 - return (1/reduction_mult - 1) * 100 + reduction_mult = 1 - self.reduction_cof / 100 + return (1 / reduction_mult - 1) * 100 return getattr(config, self._get_dj_key(key)) def list(self): @@ -30,8 +36,6 @@ class KFetConfig(object): (key, value) for each configuration entry as list. """ - # prevent circular imports - from kfet.forms import KFetConfigForm return [(field.label, getattr(config, name), ) for name, field in KFetConfigForm.base_fields.items()] @@ -46,9 +50,6 @@ class KFetConfig(object): Config entries are updated to given values. """ - # prevent circular imports - from kfet.forms import KFetConfigForm - # get old config new_cfg = KFetConfigForm().initial # update to new config @@ -69,3 +70,38 @@ class KFetConfig(object): kfet_config = KFetConfig() + + +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), + ) diff --git a/kfet/forms.py b/kfet/forms.py index 66638e6c..8afbe85b 100644 --- a/kfet/forms.py +++ b/kfet/forms.py @@ -1,6 +1,4 @@ # -*- coding: utf-8 -*- - -from datetime import timedelta from decimal import Decimal from django import forms @@ -9,8 +7,6 @@ from django.contrib.auth.models import User from django.forms import modelformset_factory from django.utils import timezone -from djconfig.forms import ConfigForm - from kfet.models import ( Account, Checkout, Article, OperationGroup, Operation, CheckoutStatement, ArticleCategory, AccountNegative, Transfer, @@ -370,46 +366,6 @@ class AddcostForm(forms.Form): super(AddcostForm, self).clean() -# ----- -# Settings forms -# ----- - - -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), - ) - - class FilterHistoryForm(forms.Form): checkouts = forms.ModelMultipleChoiceField(queryset=Checkout.objects.all()) accounts = forms.ModelMultipleChoiceField(queryset=Account.objects.all()) diff --git a/kfet/migrations/0054_delete_settings.py b/kfet/migrations/0054_delete_settings.py index 80ee1d24..aa826637 100644 --- a/kfet/migrations/0054_delete_settings.py +++ b/kfet/migrations/0054_delete_settings.py @@ -4,7 +4,7 @@ from __future__ import unicode_literals from django.db import migrations, models import django.db.models.deletion -from kfet.forms import KFetConfigForm +from kfet.config import KFetConfigForm def adapt_settings(apps, schema_editor): diff --git a/kfet/models.py b/kfet/models.py index b255a2e2..62fd3ed6 100644 --- a/kfet/models.py +++ b/kfet/models.py @@ -16,7 +16,6 @@ import re from .auth import KFET_GENERIC_TRIGRAMME from .auth.models import GenericTeamToken, Group, Permission # noqa -from .config import kfet_config from .utils import to_ukf def choices_length(choices): @@ -165,6 +164,7 @@ class Account(models.Model): return data def perms_to_perform_operation(self, amount): + from .config import kfet_config overdraft_duration_max = kfet_config.overdraft_duration overdraft_amount_max = kfet_config.overdraft_amount perms = set() @@ -335,6 +335,7 @@ class AccountNegative(models.Model): @property def until_default(self): + from .config import kfet_config return self.start + kfet_config.overdraft_duration diff --git a/kfet/utils.py b/kfet/utils.py index 3d06bb0b..9ee0ad46 100644 --- a/kfet/utils.py +++ b/kfet/utils.py @@ -7,11 +7,10 @@ from django.core.serializers.json import DjangoJSONEncoder from channels.channel import Group from channels.generic.websockets import JsonWebsocketConsumer -from .config import kfet_config - def to_ukf(balance, is_cof=False): """Convert euro to UKF.""" + from .config import kfet_config subvention = kfet_config.subvention_cof grant = (1 + subvention / 100) if is_cof else 1 return math.floor(balance * 10 * grant) diff --git a/kfet/views.py b/kfet/views.py index 79fe184d..bfba8220 100644 --- a/kfet/views.py +++ b/kfet/views.py @@ -24,7 +24,7 @@ from django.utils.decorators import method_decorator from gestioncof.models import CofProfile -from kfet.config import kfet_config +from kfet.config import KFetConfigForm, kfet_config from kfet.decorators import teamkfet_required from kfet.models import ( Account, Checkout, Article, AccountNegative, @@ -40,7 +40,7 @@ from kfet.forms import ( KPsulOperationGroupForm, KPsulAccountForm, KPsulCheckoutForm, KPsulOperationFormSet, AddcostForm, FilterHistoryForm, TransferFormSet, InventoryArticleForm, OrderArticleForm, - OrderArticleToInventoryForm, CategoryForm, KFetConfigForm + OrderArticleToInventoryForm, CategoryForm, ) from collections import defaultdict from kfet import consumers