diff --git a/kfet/forms.py b/kfet/forms.py index 7f9ff30a..9d164746 100644 --- a/kfet/forms.py +++ b/kfet/forms.py @@ -7,7 +7,7 @@ from django.contrib.admin.widgets import FilteredSelectMultiple from django.forms import modelformset_factory from django.utils import timezone from kfet.models import (Account, Checkout, Article, OperationGroup, Operation, - CheckoutStatement, ArticleCategory, Settings) + CheckoutStatement, ArticleCategory, Settings, AccountNegative) from gestioncof.models import CofProfile # ----- @@ -104,6 +104,15 @@ class GroupForm(forms.ModelForm): model = Group fields = ['name', 'permissions'] +class AccountNegativeForm(forms.ModelForm): + class Meta: + model = AccountNegative + fields = ['authz_overdraft_amount', 'authz_overdraft_until', + 'balance_offset', 'comment'] + widgets = { + 'authz_overdraft_until': DateTimeWidget(), + } + # ----- # Checkout forms # ----- diff --git a/kfet/templates/kfet/account_update.html b/kfet/templates/kfet/account_update.html index dedbcb24..0c63d6a3 100644 --- a/kfet/templates/kfet/account_update.html +++ b/kfet/templates/kfet/account_update.html @@ -1,4 +1,8 @@ - {% extends "kfet/base.html" %} +{% extends "kfet/base.html" %} + +{% block extra_head %} +{{ negative_form.media }} +{% endblock %} {% block title %} {% if account.user == request.user %} @@ -34,7 +38,16 @@ {{ cof_form.as_p }} {{ account_form.as_p }} {{ group_form.as_p }} - {% if perms.kfet.is_team and not perms.kfet.change_account %} + {{ negative_form.non_field_errors }} + {% for field in negative_form %} + {{ field.errors }} + {{ field.label_tag }} +
{{ field }}
+ {% if field.help_text %} +

{{ field.help_text|safe }}

+ {% endif %} + {% endfor %} + {% if perms.kfet.is_team %} {% endif %} @@ -44,4 +57,16 @@ + + {% endblock %} diff --git a/kfet/views.py b/kfet/views.py index ac8afcb8..87784e6c 100644 --- a/kfet/views.py +++ b/kfet/views.py @@ -230,11 +230,16 @@ def account_update(request, trigramme): group_form = UserGroupForm(instance=account.user) account_form = AccountForm(instance=account) cof_form = CofRestrictForm(instance=account.cofprofile) + if hasattr(account, 'negative'): + negative_form = AccountNegativeForm(instance=account.negative) + else: + negative_form = None else: user_form = UserRestrictForm(instance=account.user) account_form = None cof_form = None group_form = None + negative_form = None if request.method == "POST": # Update attempt @@ -246,6 +251,8 @@ def account_update(request, trigramme): cof_form = CofRestrictForm(request.POST, instance=account.cofprofile) user_form = UserRestrictTeamForm(request.POST, instance=account.user) group_form = UserGroupForm(request.POST, instance=account.user) + if hasattr(account, 'negative'): + negative_form = AccountNegativeForm(request.POST, instance=account.negative) if (request.user.has_perm('kfet.change_account') and account_form.is_valid() and cof_form.is_valid() @@ -254,6 +261,7 @@ def account_update(request, trigramme): # Fill data for Account.save() put_cleaned_data_in_dict(data, user_form) put_cleaned_data_in_dict(data, cof_form) + print(vars(account.negative)) # Updating account_form.save(data = data) @@ -262,6 +270,25 @@ def account_update(request, trigramme): if (request.user.has_perm('kfet.manage_perms') and group_form.is_valid()): group_form.save() + print(vars(account.negative)) + + # Checking perm to manage negative + if hasattr(account, 'negative'): + balance_offset_old = 0 + if account.negative.balance_offset: + balance_offset_old = account.negative.balance_offset + if (hasattr(account, 'negative') + and request.user.has_perm('kfet.change_accountnegative') + and negative_form.is_valid()): + balance_offset_new = negative_form.cleaned_data['balance_offset'] + if not balance_offset_new: + balance_offset_new = 0 + balance_offset_diff = balance_offset_old - balance_offset_new + Account.objects.filter(pk=account.pk).update( + balance = F('balance') + balance_offset_diff) + negative_form.save() + if not balance_offset_new and Account.objects.get(pk=account.pk).balance >= 0: + AccountNegative.objects.get(account=account).delete() success = True messages.success(request, @@ -291,6 +318,7 @@ def account_update(request, trigramme): 'cof_form' : cof_form, 'user_form' : user_form, 'group_form' : group_form, + 'negative_form': negative_form, }) @permission_required('kfet.manage_perms')