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

View file

@ -123,8 +123,6 @@ urlpatterns = [
name = 'kfet.kpsul.perform_operations'), name = 'kfet.kpsul.perform_operations'),
url('^k-psul/cancel_operations$', views.kpsul_cancel_operations, url('^k-psul/cancel_operations$', views.kpsul_cancel_operations,
name = 'kfet.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, url('^k-psul/articles_data', views.kpsul_articles_data,
name = 'kfet.kpsul.articles_data'), name = 'kfet.kpsul.articles_data'),
url('^k-psul/update_addcost$', views.kpsul_update_addcost, url('^k-psul/update_addcost$', views.kpsul_update_addcost,
@ -132,8 +130,16 @@ urlpatterns = [
url('^k-psul/get_settings$', views.kpsul_get_settings, url('^k-psul/get_settings$', views.kpsul_get_settings,
name = 'kfet.kpsul.get_settings'), name = 'kfet.kpsul.get_settings'),
# -----
# JSON urls
# -----
url('^history.json$', views.history_json,
name = 'kfet.history.json'),
# ----- # -----
# Settings urls # Settings urls
# -----
url('^settings/$', url('^settings/$',
permission_required('kfet.change_settings')(views.SettingsList.as_view()), 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.http import HttpResponse, JsonResponse, Http404
from django.forms import modelformset_factory from django.forms import modelformset_factory
from django.db import IntegrityError, transaction 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.db.models.functions import Coalesce
from django.utils import timezone from django.utils import timezone
from django.utils.crypto import get_random_string from django.utils.crypto import get_random_string
@ -589,7 +589,6 @@ def kpsul(request):
data['checkout_form'] = KPsulCheckoutForm() data['checkout_form'] = KPsulCheckoutForm()
operation_formset = KPsulOperationFormSet(queryset=Operation.objects.none()) operation_formset = KPsulOperationFormSet(queryset=Operation.objects.none())
data['operation_formset'] = operation_formset data['operation_formset'] = operation_formset
addcost_for = Settings.ADDCOST_FOR()
return render(request, 'kfet/kpsul.html', data) return render(request, 'kfet/kpsul.html', data)
@permission_required('kfet.is_team') @permission_required('kfet.is_team')
@ -1015,25 +1014,71 @@ def kpsul_cancel_operations(request):
return JsonResponse(data) return JsonResponse(data)
@permission_required('kfet.is_team') @permission_required('kfet.is_team')
def kpsul_history(request): def history_json(request):
opegroups_list = (OperationGroup.objects # Récupération des paramètres
.values( from_date = request.POST.get('from', None)
'id', 'amount', 'at', 'checkout_id', 'is_cof', 'comment', to_date = request.POST.get('to', None)
'valid_by__trigramme', 'on_acc__trigramme') checkouts = request.POST.getlist('checkouts[]', None)
.select_related('valid_by', 'on_acc') accounts = request.POST.getlist('accounts[]', None)
.filter(at__gt=timezone.now()-datetime.timedelta(hours=24)))
opegroups = { opegroup['id']:opegroup for opegroup in opegroups_list } # 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: for opegroup in opegroups:
opegroups[opegroup]['opes'] = [] opegroup_dict = {
opes = (Operation.objects 'id' : opegroup.id,
.values( 'amount' : opegroup.amount,
'id', 'type', 'amount', 'addcost_amount', 'is_checkout', 'at' : opegroup.at,
'article__name', 'canceled_by__trigramme', 'canceled_at', 'checkout_id': opegroup.checkout_id,
'addcost_for__trigramme', 'article_nb', 'group_id') 'is_cof' : opegroup.is_cof,
.filter(group__in=opegroups.keys())) 'comment' : opegroup.comment,
for ope in opes: 'opes' : [],
opegroups[ope['group_id']]['opes'].append(ope) 'on_acc__trigramme':
return JsonResponse(opegroups) 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') @permission_required('kfet.is_team')
def kpsul_articles_data(request): def kpsul_articles_data(request):