From 3b7c690f0b7ead41c5231784f0a36b2b6c35b95b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Delobelle?= Date: Wed, 24 Aug 2016 19:52:07 +0200 Subject: [PATCH] Page Historique MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Reprise du JS d'historique dans `kfet/static/kfet/js/history.js` - Adapatation de K-Psul pour l'utiliser - Création page historique avec filtres (dates, caisses, comptes) --- kfet/forms.py | 5 +- kfet/static/kfet/js/history.js | 108 ++++++++++++++++++ kfet/static/kfet/js/kfet.js | 31 ++++++ kfet/templates/kfet/account.html | 2 +- kfet/templates/kfet/base_nav.html | 1 + kfet/templates/kfet/history.html | 140 +++++++++++++++++++++++ kfet/templates/kfet/kpsul.html | 178 +++++------------------------- kfet/urls.py | 2 + kfet/views.py | 14 ++- 9 files changed, 328 insertions(+), 153 deletions(-) create mode 100644 kfet/static/kfet/js/history.js create mode 100644 kfet/templates/kfet/history.html diff --git a/kfet/forms.py b/kfet/forms.py index e39ff9e1..8302067a 100644 --- a/kfet/forms.py +++ b/kfet/forms.py @@ -3,7 +3,6 @@ from django import forms from django.core.exceptions import ValidationError from django.contrib.auth.models import User, Group, Permission from django.contrib.contenttypes.models import ContentType -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, @@ -343,3 +342,7 @@ class SettingsForm(forms.ModelForm): self.cleaned_data['value_account'] = None cache.delete(name) super(SettingsForm, self).clean() + +class FilterHistoryForm(forms.Form): + checkouts = forms.ModelMultipleChoiceField(queryset = Checkout.objects.all()) + accounts = forms.ModelMultipleChoiceField(queryset = Account.objects.all()) diff --git a/kfet/static/kfet/js/history.js b/kfet/static/kfet/js/history.js new file mode 100644 index 00000000..220db669 --- /dev/null +++ b/kfet/static/kfet/js/history.js @@ -0,0 +1,108 @@ +function KHistory(options={}) { + $.extend(this, KHistory.default_options, options); + + this.$container = $(this.container); + + this.reset = function() { + this.$container.html(''); + }; + + this.addOpeGroup = function(opegroup) { + var $day = this._getOrCreateDay(opegroup['at']); + var $opegroup = this._opeGroupHtml(opegroup); + + $day.after($opegroup); + + var trigramme = opegroup['on_acc_trigramme']; + var is_cof = opegroup['is_cof']; + for (var i=0; i', + template_opegroup: '
', + template_ope: '
', + display_trigramme: true, +} diff --git a/kfet/static/kfet/js/kfet.js b/kfet/static/kfet/js/kfet.js index 151a7bd5..7c53b1e7 100644 --- a/kfet/static/kfet/js/kfet.js +++ b/kfet/static/kfet/js/kfet.js @@ -8,4 +8,35 @@ $(document).ready(function() { $('.col-content-right').removeClass('col-sm-offset-4 col-md-offset-3'); } }); + + // Retrieving csrf token + var csrftoken = Cookies.get('csrftoken'); + // Appending csrf token to ajax post requests + function csrfSafeMethod(method) { + // these HTTP methods do not require CSRF protection + return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method)); + } + $.ajaxSetup({ + beforeSend: function(xhr, settings) { + if (!csrfSafeMethod(settings.type) && !this.crossDomain) { + xhr.setRequestHeader("X-CSRFToken", csrftoken); + } + } + }); + }); + +function dateUTCToParis(date) { + return moment.tz(date, 'UTC').tz('Europe/Paris'); +} + +function amountDisplay(amount, is_cof=false, tri='') { + if (tri == 'LIQ') + return (- amount).toFixed(2) +'€'; + return amountToUKF(amount, is_cof); +} + +function amountToUKF(amount, is_cof=false) { + var coef_cof = is_cof ? 1 + settings['subvention_cof'] / 100 : 1; + return Math.round(amount * coef_cof * 10); +} diff --git a/kfet/templates/kfet/account.html b/kfet/templates/kfet/account.html index 7983d3a6..b0123020 100644 --- a/kfet/templates/kfet/account.html +++ b/kfet/templates/kfet/account.html @@ -8,7 +8,7 @@
-
+
{{ accounts|length|add:-1 }}
compte{{ accounts|length|add:-1|pluralize }}
diff --git a/kfet/templates/kfet/base_nav.html b/kfet/templates/kfet/base_nav.html index b4169824..8b30f0f3 100644 --- a/kfet/templates/kfet/base_nav.html +++ b/kfet/templates/kfet/base_nav.html @@ -34,6 +34,7 @@
  • Comptes
  • Caisses
  • Articles
  • +
  • Historique
  • {% if user.username != 'kfet_genericteam' %}
  • Connexion standard
  • {% endif %} diff --git a/kfet/templates/kfet/history.html b/kfet/templates/kfet/history.html new file mode 100644 index 00000000..b1f50422 --- /dev/null +++ b/kfet/templates/kfet/history.html @@ -0,0 +1,140 @@ +{% extends 'kfet/base.html' %} +{% load staticfiles %} +{% load l10n %} + +{% block extra_head %} + + + + + + + + + + + +{% endblock %} + +{% block title %}Historique{% endblock %} +{% block content-header-title %}Historique{% endblock %} + +{% block content %} + +
    +
    +
    +
    +
    +
    opérations
    +
    +

    Filtres

    +
    De
    +
    à
    +
    Caisses {{ filter_form.checkouts }}
    +
    Comtpes {{ filter_form.accounts }}
    +
    +
    +
    +
    +
    + {% include 'kfet/base_messages.html' %} +
    +
    +

    Général

    +
    +
    +
    +
    +

    Opérations

    + +
    +
    +
    +
    +
    + + + +{% endblock %} diff --git a/kfet/templates/kfet/kpsul.html b/kfet/templates/kfet/kpsul.html index 58a006f5..078ecfe5 100644 --- a/kfet/templates/kfet/kpsul.html +++ b/kfet/templates/kfet/kpsul.html @@ -11,6 +11,8 @@ + + {% endblock %} {% block title %}K-Psul{% endblock %} @@ -19,8 +21,6 @@ {% block content %} -{% csrf_token %} -
    @@ -98,6 +98,9 @@
    +
    +
    + @@ -111,29 +114,9 @@ $(document).ready(function() { // General // ----- - // Retrieving csrf token - var csrftoken = Cookies.get('csrftoken'); - // Appending csrf token to ajax post requests - function csrfSafeMethod(method) { - // these HTTP methods do not require CSRF protection - return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method)); - } - $.ajaxSetup({ - beforeSend: function(xhr, settings) { - if (!csrfSafeMethod(settings.type) && !this.crossDomain) { - xhr.setRequestHeader("X-CSRFToken", csrftoken); - } - } - }); - - function amountToUKF(amount, is_cof=false) { - var coef_cof = is_cof ? 1 + settings['subvention_cof'] / 100 : 1; - return Math.round(amount * coef_cof * 10); - } - // Retrieve settings - var settings = {} + settings = {} function resetSettings() { $.ajax({ @@ -979,114 +962,12 @@ $(document).ready(function() { // History // ----- - var history_container = $('#history'); - var history_operationgroup_html = '
    '; - var history_operation_html = '
    '; - - function getOpeHtml(ope, is_cof=false, trigramme='') { - var ope_html = $(history_operation_html); - var amount = parseFloat(ope['amount']); - var amountUKF = amountToUKF(amount, is_cof); - var infos1 = '', infos2 = ''; - - if (ope['type'] == 'deposit') { - infos1 = amount.toFixed(2)+'€'; - infos2 = 'Charge'; - } else if (ope['type'] == 'withdraw') { - infos1 = amount.toFixed(2)+'€'; - infos2 = 'Retrait'; - } else { - infos1 = ope['article_nb']; - infos2 = ope['article__name']; - } - - if (trigramme == 'LIQ') - amountUKF = (- amount).toFixed(2) +"€"; - - ope_html - .attr('data-ope', ope['id']) - .find('.amount').text(amountUKF).end() - .find('.infos1').text(infos1).end() - .find('.infos2').text(infos2).end(); - - if (ope['addcost_for__trigramme']) { - ope_html.find('.addcost').text('('+amountToUKF(ope['addcost_amount'],is_cof)+'UKF pour '+ope['addcost_for__trigramme']+')') - } - - if (ope['canceled_at']) { - ope_html.addClass('canceled'); - var cancel = 'Annulé'; - if (ope['canceled_by__trigramme']) - cancel += ' par '+ope['canceled_by__trigramme']; - var canceled_at = moment.tz(ope['canceled_at'],'UTC'); - cancel += ' le '+canceled_at.tz('Europe/Paris').format('DD/MM/YY à HH:mm:ss'); - ope_html.find('.canceled').text(cancel); - } - - return ope_html; - } - - function getOpegroupHtml(opegroup) { - var opegroup_html = $(history_operationgroup_html); - var at = moment.tz(opegroup['at'], 'UTC'); - var at_formated = at.tz('Europe/Paris').format('HH:mm:ss'); - var amount = parseFloat(opegroup['amount']); - var trigramme = opegroup['on_acc__trigramme']; - if (opegroup['on_acc__trigramme'] == 'LIQ') { - var amount = (- amount).toFixed(2) +'€'; - } else { - var amount = amountToUKF(amount, opegroup['is_cof']); - } - var valid_by = ''; - if (opegroup['valid_by__trigramme']) - valid_by = 'Par '+opegroup['valid_by__trigramme']; - var comment = opegroup['comment'] || ''; - - opegroup_html - .attr('data-opegroup', opegroup['id']) - .find('.time').text(at_formated).end() - .find('.amount').text(amount).end() - .find('.trigramme').text(trigramme).end() - .find('.valid_by').text(valid_by).end() - .find('.comment').text(comment).end(); - - return opegroup_html; - } - - function addOpeGroup(opegroup) { - checkOrCreateDay(opegroup['at']); - var $opegroup_html = getOpegroupHtml(opegroup); - history_container.find('.day').first().after($opegroup_html); - - // Ajout des opérations de ce groupe - var $opegroup = history_container.find('[data-opegroup='+opegroup['id']+']'); - for (var i=0; i < opegroup['opes'].length; i++) { - var $ope = getOpeHtml(opegroup['opes'][i], opegroup['is_cof'], opegroup['on_acc__trigramme']); - $ope.attr('data-opegroup', opegroup['id']); - $opegroup.after($ope); - } - } - - var history_day_default_html = '
    '; - - function checkOrCreateDay(at) { - var at = moment.tz(at, 'UTC').tz('Europe/Paris'); - var mostRecentDay = history_container.find('.day').first(); - if (mostRecentDay.length == 0 || at.month() != mostRecentDay.attr('data-month') || at.date() != mostRecentDay.attr('data-day')) { - var day_html = $(history_day_default_html); - day_html - .attr('data-month', at.month()) - .attr('data-day', at.date()) - .text(at.format('D MMMM')); - history_container.prepend(day_html); - } - } + khistory = new KHistory(); 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.history.json' %}", @@ -1095,15 +976,11 @@ $(document).ready(function() { }) .done(function(data) { for (var i=0; i 0) cancelOperations(opes_to_cancel); @@ -1206,24 +1085,25 @@ $(document).ready(function() { }); function cancelOpeGroup(opegroup) { - var opegroup_html = history_container.find('.opegroup[data-opegroup='+opegroup['id']+']'); - if (opegroup_html.find('.trigramme').text() == 'LIQ') { - var amount = (- parseFloat(opegroup['amount']).toFixed(2))+'€'; - } else { - var amount = amountToUKF(opegroup['amount'], opegroup['on_acc__is_cof']); - } - opegroup_html.find('.amount').text(amount); + var $opegroup = khistory.$container.find('.opegroup').filter(function() { + return $(this).data('opegroup') == opegroup['id'] + }); + var tri = $opegroup.find('.trigramme').text(); + var amount = amountDisplay( + parseFloat(opegroup['amount'], opegroup['is_cof'], tri)) + $opegroup.find('.amount').text(amount); } function cancelOpe(ope) { - var ope_html = history_container.find('[data-ope='+ope['id']+']'); - ope_html.addClass('canceled'); + var $ope = khistory.$container.find('.ope').filter(function() { + return $(this).data('ope') == ope['id'] + }); var cancel = 'Annulé'; + var canceled_at = dateUTCToParis(ope['canceled_at']); if (ope['canceled_by__trigramme']) cancel += ' par '+ope['canceled_by__trigramme']; - var canceled_at = moment.tz(ope['canceled_at'], 'UTC'); - cancel += ' le '+canceled_at.tz('Europe/Paris').format('DD/MM/YY à HH:mm:ss'); - ope_html.find('.canceled').text(cancel); + cancel += ' le '+canceled_at.format('DD/MM/YY à HH:mm:ss'); + $ope.addClass('canceled').find('.canceled').text(cancel); } // ----- @@ -1285,7 +1165,7 @@ $(document).ready(function() { coolReset(give_tri_focus); resetCheckout(); resetArticles(); - resetHistory(); + khistory.reset(); resetSettings(); getArticles(); getHistory(); diff --git a/kfet/urls.py b/kfet/urls.py index 330adca9..51e6c785 100644 --- a/kfet/urls.py +++ b/kfet/urls.py @@ -8,6 +8,8 @@ urlpatterns = [ name = 'kfet.home'), url(r'^login/genericteam$', views.login_genericteam, name = 'kfet.login.genericteam'), + url(r'^history$', views.history, + name = 'kfet.history'), # ----- # Account urls diff --git a/kfet/views.py b/kfet/views.py index 25fdfae8..4b9a6553 100644 --- a/kfet/views.py +++ b/kfet/views.py @@ -975,13 +975,13 @@ def kpsul_cancel_operations(request): # Need refresh from db cause we used update on querysets opegroups_pk = [ opegroup.pk for opegroup in to_groups_amounts ] opegroups = (OperationGroup.objects - .values('id','amount','on_acc__cofprofile__is_cof').filter(pk__in=opegroups_pk)) + .values('id','amount','is_cof').filter(pk__in=opegroups_pk)) for opegroup in opegroups: websocket_data['opegroups'].append({ 'cancellation': True, 'id': opegroup['id'], 'amount': opegroup['amount'], - 'on_acc__is_cof': opegroup['on_acc__cofprofile__is_cof'], + 'is_cof': opegroup['is_cof'], }) canceled_by__trigramme = canceled_by and canceled_by.trigramme or None for ope in opes: @@ -1088,6 +1088,16 @@ def kpsul_articles_data(request): .filter(is_sold=True)) return JsonResponse({ 'articles': list(articles) }) +@permission_required('kfet.is_team') +def history(request): + data = { + 'filter_form': FilterHistoryForm(), + 'settings': { + 'subvention_cof': Settings.SUBVENTION_COF(), + } + } + return render(request, 'kfet/history.html', data) + # ----- # Settings views # -----