forked from DGNum/gestioCOF
Use django-djconfig for kfet app.
Old configuration(/settings), based on Settings model, system is deleted: SettingsForm, Settings. New system use `django-djconfig` module. - `kfet.config` module provides `kfet_config` to access configuration concerning kfet app. - Views, forms, models, etc now use this object to retrieve conf values. - Views no longer add config values to context, instead templates use `kfet_config` provided by a new context_processor. - Enhance list and update views of settings. - Fix: settings can directly be used without having to visit a specific page... Misc - Delete some py2/3 imports - Delete unused imports in kfet.models and kfet.views - Some PEP8 compliance
This commit is contained in:
parent
a4a854bc50
commit
85caa6b058
16 changed files with 247 additions and 227 deletions
|
@ -22,9 +22,11 @@ from django.utils import timezone
|
|||
from django.utils.crypto import get_random_string
|
||||
from django.utils.decorators import method_decorator
|
||||
from gestioncof.models import CofProfile
|
||||
|
||||
from kfet.config import kfet_config
|
||||
from kfet.decorators import teamkfet_required
|
||||
from kfet.models import (
|
||||
Account, Checkout, Article, Settings, AccountNegative,
|
||||
Account, Checkout, Article, AccountNegative,
|
||||
CheckoutStatement, GenericTeamToken, Supplier, SupplierArticle, Inventory,
|
||||
InventoryArticle, Order, OrderArticle, Operation, OperationGroup,
|
||||
TransferGroup, Transfer)
|
||||
|
@ -35,9 +37,9 @@ from kfet.forms import (
|
|||
GroupForm, CheckoutForm, CheckoutRestrictForm, CheckoutStatementCreateForm,
|
||||
CheckoutStatementUpdateForm, ArticleForm, ArticleRestrictForm,
|
||||
KPsulOperationGroupForm, KPsulAccountForm, KPsulCheckoutForm,
|
||||
KPsulOperationFormSet, AddcostForm, FilterHistoryForm, SettingsForm,
|
||||
KPsulOperationFormSet, AddcostForm, FilterHistoryForm,
|
||||
TransferFormSet, InventoryArticleForm, OrderArticleForm,
|
||||
OrderArticleToInventoryForm
|
||||
OrderArticleToInventoryForm, KFetConfigForm
|
||||
)
|
||||
from collections import defaultdict
|
||||
from kfet import consumers
|
||||
|
@ -346,12 +348,14 @@ def account_create_ajax(request, username=None, login_clipper=None,
|
|||
'user_form' : forms['user_form'],
|
||||
})
|
||||
|
||||
|
||||
# Account - Read
|
||||
|
||||
@login_required
|
||||
def account_read(request, trigramme):
|
||||
try:
|
||||
account = Account.objects.select_related('negative').get(trigramme=trigramme)
|
||||
account = (Account.objects.select_related('negative')
|
||||
.get(trigramme=trigramme))
|
||||
except Account.DoesNotExist:
|
||||
raise Http404
|
||||
|
||||
|
@ -361,16 +365,17 @@ def account_read(request, trigramme):
|
|||
raise PermissionDenied
|
||||
|
||||
addcosts = (OperationGroup.objects
|
||||
.filter(opes__addcost_for=account,opes__canceled_at=None)
|
||||
.extra({'date':"date(at)"})
|
||||
.values('date')
|
||||
.annotate(sum_addcosts=Sum('opes__addcost_amount'))
|
||||
.order_by('-date'))
|
||||
.filter(opes__addcost_for=account,
|
||||
opes__canceled_at=None)
|
||||
.extra({'date': "date(at)"})
|
||||
.values('date')
|
||||
.annotate(sum_addcosts=Sum('opes__addcost_amount'))
|
||||
.order_by('-date')
|
||||
)
|
||||
|
||||
return render(request, "kfet/account_read.html", {
|
||||
'account' : account,
|
||||
'account': account,
|
||||
'addcosts': addcosts,
|
||||
'settings': { 'subvention_cof': Settings.SUBVENTION_COF() },
|
||||
})
|
||||
|
||||
# Account - Update
|
||||
|
@ -548,10 +553,6 @@ class AccountNegativeList(ListView):
|
|||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super(AccountNegativeList, self).get_context_data(**kwargs)
|
||||
context['settings'] = {
|
||||
'overdraft_amount': Settings.OVERDRAFT_AMOUNT(),
|
||||
'overdraft_duration': Settings.OVERDRAFT_DURATION(),
|
||||
}
|
||||
negs_sum = (AccountNegative.objects
|
||||
.exclude(account__trigramme='#13')
|
||||
.aggregate(
|
||||
|
@ -870,16 +871,18 @@ def kpsul(request):
|
|||
data['operation_formset'] = operation_formset
|
||||
return render(request, 'kfet/kpsul.html', data)
|
||||
|
||||
|
||||
@teamkfet_required
|
||||
def kpsul_get_settings(request):
|
||||
addcost_for = Settings.ADDCOST_FOR()
|
||||
addcost_for = kfet_config.addcost_for
|
||||
data = {
|
||||
'subvention_cof': Settings.SUBVENTION_COF(),
|
||||
'addcost_for' : addcost_for and addcost_for.trigramme or '',
|
||||
'addcost_amount': Settings.ADDCOST_AMOUNT(),
|
||||
'subvention_cof': kfet_config.subvention_cof,
|
||||
'addcost_for': addcost_for and addcost_for.trigramme or '',
|
||||
'addcost_amount': kfet_config.addcost_amount,
|
||||
}
|
||||
return JsonResponse(data)
|
||||
|
||||
|
||||
@teamkfet_required
|
||||
def account_read_json(request):
|
||||
trigramme = request.POST.get('trigramme', '')
|
||||
|
@ -919,37 +922,41 @@ def kpsul_checkout_data(request):
|
|||
raise Http404
|
||||
return JsonResponse(data)
|
||||
|
||||
|
||||
@teamkfet_required
|
||||
def kpsul_update_addcost(request):
|
||||
addcost_form = AddcostForm(request.POST)
|
||||
|
||||
if not addcost_form.is_valid():
|
||||
data = { 'errors': { 'addcost': list(addcost_form.errors) } }
|
||||
data = {'errors': {'addcost': list(addcost_form.errors)}}
|
||||
return JsonResponse(data, status=400)
|
||||
required_perms = ['kfet.manage_addcosts']
|
||||
if not request.user.has_perms(required_perms):
|
||||
data = {
|
||||
'errors': {
|
||||
'missing_perms': get_missing_perms(required_perms, request.user)
|
||||
'missing_perms': get_missing_perms(required_perms,
|
||||
request.user),
|
||||
}
|
||||
}
|
||||
return JsonResponse(data, status=403)
|
||||
|
||||
trigramme = addcost_form.cleaned_data['trigramme']
|
||||
account = trigramme and Account.objects.get(trigramme=trigramme) or None
|
||||
Settings.objects.filter(name='ADDCOST_FOR').update(value_account=account)
|
||||
Settings.objects.filter(name='ADDCOST_AMOUNT').update(value_decimal=addcost_form.cleaned_data['amount'])
|
||||
cache.delete('ADDCOST_FOR')
|
||||
cache.delete('ADDCOST_AMOUNT')
|
||||
amount = addcost_form.cleaned_data['amount']
|
||||
|
||||
cfg_form = KFetConfigForm(kfet_addcost_for=account,
|
||||
kfet_addcost_amount=amount)
|
||||
cfg_form.save()
|
||||
data = {
|
||||
'addcost': {
|
||||
'for': trigramme and account.trigramme or None,
|
||||
'amount': addcost_form.cleaned_data['amount'],
|
||||
'amount': amount,
|
||||
}
|
||||
}
|
||||
consumers.KPsul.group_send('kfet.kpsul', data)
|
||||
return JsonResponse(data)
|
||||
|
||||
|
||||
def get_missing_perms(required_perms, user):
|
||||
missing_perms_codenames = [ (perm.split('.'))[1]
|
||||
for perm in required_perms if not user.has_perm(perm)]
|
||||
|
@ -984,10 +991,10 @@ def kpsul_perform_operations(request):
|
|||
operations = operation_formset.save(commit = False)
|
||||
|
||||
# Retrieving COF grant
|
||||
cof_grant = Settings.SUBVENTION_COF()
|
||||
cof_grant = kfet_config.subvention_cof
|
||||
# Retrieving addcosts data
|
||||
addcost_amount = Settings.ADDCOST_AMOUNT()
|
||||
addcost_for = Settings.ADDCOST_FOR()
|
||||
addcost_amount = kfet_config.addcost_amount
|
||||
addcost_for = kfet_config.addcost_for
|
||||
|
||||
# Initializing vars
|
||||
required_perms = set() # Required perms to perform all operations
|
||||
|
@ -1170,7 +1177,7 @@ def kpsul_cancel_operations(request):
|
|||
opes = [] # Pas déjà annulée
|
||||
required_perms = set()
|
||||
stop_all = False
|
||||
cancel_duration = Settings.CANCEL_DURATION()
|
||||
cancel_duration = kfet_config.cancel_duration
|
||||
to_accounts_balances = defaultdict(lambda:0) # Modifs à faire sur les balances des comptes
|
||||
to_groups_amounts = defaultdict(lambda:0) # ------ sur les montants des groupes d'opé
|
||||
to_checkouts_balances = defaultdict(lambda:0) # ------ sur les balances de caisses
|
||||
|
@ -1397,34 +1404,28 @@ def kpsul_articles_data(request):
|
|||
.filter(is_sold=True))
|
||||
return JsonResponse({ 'articles': list(articles) })
|
||||
|
||||
|
||||
@teamkfet_required
|
||||
def history(request):
|
||||
data = {
|
||||
'filter_form': FilterHistoryForm(),
|
||||
'settings': {
|
||||
'subvention_cof': Settings.SUBVENTION_COF(),
|
||||
}
|
||||
}
|
||||
return render(request, 'kfet/history.html', data)
|
||||
|
||||
|
||||
# -----
|
||||
# Settings views
|
||||
# -----
|
||||
|
||||
class SettingsList(ListView):
|
||||
model = Settings
|
||||
context_object_name = 'settings'
|
||||
|
||||
class SettingsList(TemplateView):
|
||||
template_name = 'kfet/settings.html'
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
Settings.create_missing()
|
||||
return super(SettingsList, self).get_context_data(**kwargs)
|
||||
|
||||
class SettingsUpdate(SuccessMessageMixin, UpdateView):
|
||||
model = Settings
|
||||
form_class = SettingsForm
|
||||
class SettingsUpdate(SuccessMessageMixin, FormView):
|
||||
form_class = KFetConfigForm
|
||||
template_name = 'kfet/settings_update.html'
|
||||
success_message = 'Paramètre %(name)s mis à jour'
|
||||
success_message = 'Paramètres mis à jour'
|
||||
success_url = reverse_lazy('kfet.settings')
|
||||
|
||||
def form_valid(self, form):
|
||||
|
@ -1432,9 +1433,9 @@ class SettingsUpdate(SuccessMessageMixin, UpdateView):
|
|||
if not self.request.user.has_perm('kfet.change_settings'):
|
||||
form.add_error(None, 'Permission refusée')
|
||||
return self.form_invalid(form)
|
||||
# Creating
|
||||
Settings.empty_cache()
|
||||
return super(SettingsUpdate, self).form_valid(form)
|
||||
form.save()
|
||||
return super().form_valid(form)
|
||||
|
||||
|
||||
# -----
|
||||
# Transfer views
|
||||
|
@ -1560,7 +1561,7 @@ def cancel_transfers(request):
|
|||
transfers = [] # Pas déjà annulée
|
||||
required_perms = set()
|
||||
stop_all = False
|
||||
cancel_duration = Settings.CANCEL_DURATION()
|
||||
cancel_duration = kfet_config.cancel_duration
|
||||
to_accounts_balances = defaultdict(lambda:0) # Modifs à faire sur les balances des comptes
|
||||
for transfer in transfers_all:
|
||||
if transfer.canceled_at:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue