Reprise de la vue historique

- Ajout de paramètres à cette vue
- `opegroups` contient maintenant une liste de groupes d'opérations et
  non plus un dictionnaire contenant `opegroup.pk` => `opegroup`
This commit is contained in:
Aurélien Delobelle 2016-08-24 02:05:05 +02:00
parent 54ff265b0f
commit b380984a1e
3 changed files with 82 additions and 26 deletions

View file

@ -1083,14 +1083,19 @@ $(document).ready(function() {
}
function getHistory() {
var data = {
from: moment().subtract(1, 'days').format('YYYY-MM-DD HH:mm:ss'),
};
console.log(data);
$.ajax({
dataType: "json",
url : "{% url 'kfet.kpsul.history' %}",
method : "GET",
url : "{% url 'kfet.history.json' %}",
method : "POST",
data : data,
})
.done(function(data) {
for (var opegroup_id in data) {
addOpeGroup(data[opegroup_id]);
for (var i=0; i<data['opegroups'].length; i++) {
addOpeGroup(data['opegroups'][i]);
}
});
}

View file

@ -123,8 +123,6 @@ urlpatterns = [
name = 'kfet.kpsul.perform_operations'),
url('^k-psul/cancel_operations$', views.kpsul_cancel_operations,
name = 'kfet.kpsul.cancel_operations'),
url('^k-psul/history$', views.kpsul_history,
name = 'kfet.kpsul.history'),
url('^k-psul/articles_data', views.kpsul_articles_data,
name = 'kfet.kpsul.articles_data'),
url('^k-psul/update_addcost$', views.kpsul_update_addcost,
@ -132,8 +130,16 @@ urlpatterns = [
url('^k-psul/get_settings$', views.kpsul_get_settings,
name = 'kfet.kpsul.get_settings'),
# -----
# JSON urls
# -----
url('^history.json$', views.history_json,
name = 'kfet.history.json'),
# -----
# Settings urls
# -----
url('^settings/$',
permission_required('kfet.change_settings')(views.SettingsList.as_view()),

View file

@ -12,7 +12,7 @@ from django.contrib.auth.models import User, Permission, Group
from django.http import HttpResponse, JsonResponse, Http404
from django.forms import modelformset_factory
from django.db import IntegrityError, transaction
from django.db.models import F, Sum
from django.db.models import F, Sum, Prefetch
from django.db.models.functions import Coalesce
from django.utils import timezone
from django.utils.crypto import get_random_string
@ -589,7 +589,6 @@ def kpsul(request):
data['checkout_form'] = KPsulCheckoutForm()
operation_formset = KPsulOperationFormSet(queryset=Operation.objects.none())
data['operation_formset'] = operation_formset
addcost_for = Settings.ADDCOST_FOR()
return render(request, 'kfet/kpsul.html', data)
@permission_required('kfet.is_team')
@ -1015,25 +1014,71 @@ def kpsul_cancel_operations(request):
return JsonResponse(data)
@permission_required('kfet.is_team')
def kpsul_history(request):
opegroups_list = (OperationGroup.objects
.values(
'id', 'amount', 'at', 'checkout_id', 'is_cof', 'comment',
'valid_by__trigramme', 'on_acc__trigramme')
.select_related('valid_by', 'on_acc')
.filter(at__gt=timezone.now()-datetime.timedelta(hours=24)))
opegroups = { opegroup['id']:opegroup for opegroup in opegroups_list }
def history_json(request):
# Récupération des paramètres
from_date = request.POST.get('from', None)
to_date = request.POST.get('to', None)
checkouts = request.POST.getlist('checkouts[]', None)
accounts = request.POST.getlist('accounts[]', None)
# Construction de la requête (sur les opérations) pour le prefetch
queryset_prefetch = Operation.objects.select_related(
'canceled_by__trigramme', 'addcost_for__trigramme',
'article__name')
# Construction de la requête principale
opegroups = (OperationGroup.objects
.prefetch_related(Prefetch('opes', queryset = queryset_prefetch))
.select_related('on_acc__trigramme', 'valid_by__trigramme')
.order_by('at')
)
# Application des filtres
if from_date:
opegroups = opegroups.filter(at__gte=from_date)
if to_date:
opegroups = opegroups.filter(at__lt=to_date)
if checkouts:
opegroups = opegroups.filter(checkout_id__in=checkouts)
if accounts:
opegroups = opegroups.filter(on_acc_id__in=accounts)
# Construction de la réponse
opegroups_list = []
for opegroup in opegroups:
opegroups[opegroup]['opes'] = []
opes = (Operation.objects
.values(
'id', 'type', 'amount', 'addcost_amount', 'is_checkout',
'article__name', 'canceled_by__trigramme', 'canceled_at',
'addcost_for__trigramme', 'article_nb', 'group_id')
.filter(group__in=opegroups.keys()))
for ope in opes:
opegroups[ope['group_id']]['opes'].append(ope)
return JsonResponse(opegroups)
opegroup_dict = {
'id' : opegroup.id,
'amount' : opegroup.amount,
'at' : opegroup.at,
'checkout_id': opegroup.checkout_id,
'is_cof' : opegroup.is_cof,
'comment' : opegroup.comment,
'opes' : [],
'on_acc__trigramme':
opegroup.on_acc and opegroup.on_acc.trigramme or None,
}
if request.user.has_perm('kfet.is_team'):
opegroup_dict['valid_by__trigramme'] = (
opegroup.valid_by and opegroup.valid_by.trigramme or None)
for ope in opegroup.opes.all():
ope_dict = {
'id' : ope.id,
'type' : ope.type,
'amount' : ope.amount,
'article_nb' : ope.article_nb,
'is_checkout' : ope.is_checkout,
'addcost_amount': ope.addcost_amount,
'canceled_at' : ope.canceled_at,
'article__name':
ope.article and ope.article.name or None,
'addcost_for__trigramme':
ope.addcost_for and ope.addcost_for.trigramme or None,
}
if request.user.has_perm('kfet.is_team'):
ope_dict['canceled_by__trigramme'] = (
ope.canceled_by and ope.canceled_by.trigramme or None)
opegroup_dict['opes'].append(ope_dict)
opegroups_list.append(opegroup_dict)
return JsonResponse({ 'opegroups': opegroups_list })
@permission_required('kfet.is_team')
def kpsul_articles_data(request):