Historique d'un compte

- Reprise en utilisant `history.js`
- Fix csrf_token sur ajax POST K-Psul
- Fix annulation K-Psul
- Ajouts de select_related pour économiser de la requête BDD
This commit is contained in:
Aurélien Delobelle 2016-08-24 23:34:14 +02:00
parent 3b7c690f0b
commit c4fa4ea20c
5 changed files with 61 additions and 71 deletions

View file

@ -74,10 +74,11 @@ function KHistory(options={}) {
.data('opegroup', opegroup['id']) .data('opegroup', opegroup['id'])
.find('.time').text(at).end() .find('.time').text(at).end()
.find('.amount').text(amount).end() .find('.amount').text(amount).end()
.find('.comment').text(comment).end(); .find('.comment').text(comment).end()
.find('.trigramme').text(trigramme).end();
if (this.display_trigramme) if (!this.display_trigramme)
$opegroup_html.find('.trigramme').text(trigramme); $opegroup_html.find('.trigramme').remove();
if (opegroup['valid_by__trigramme']) if (opegroup['valid_by__trigramme'])
$opegroup_html.find('.valid_by').text('Par '+opegroup['valid_by__trigramme']); $opegroup_html.find('.valid_by').text('Par '+opegroup['valid_by__trigramme']);

View file

@ -10,7 +10,7 @@ $(document).ready(function() {
}); });
// Retrieving csrf token // Retrieving csrf token
var csrftoken = Cookies.get('csrftoken'); csrftoken = Cookies.get('csrftoken');
// Appending csrf token to ajax post requests // Appending csrf token to ajax post requests
function csrfSafeMethod(method) { function csrfSafeMethod(method) {
// these HTTP methods do not require CSRF protection // these HTTP methods do not require CSRF protection

View file

@ -1,5 +1,16 @@
{% extends "kfet/base.html" %} {% extends "kfet/base.html" %}
{% load staticfiles %}
{% load kfet_tags %} {% load kfet_tags %}
{% load l10n %}
{% block extra_head %}
<script type="text/javascript" src="{% static 'kfet/js/js.cookie.js' %}"></script>
<script type="text/javascript" src="{% static 'moment.js' %}"></script>
<script type="text/javascript" src="{% static 'moment-fr.js' %}"></script>
<script type="text/javascript" src="{% static 'moment-timezone-with-data-2010-2020.js' %}"></script>
<script type="text/javascript" src="{% static 'kfet/js/kfet.js' %}"></script>
<script type="text/javascript" src="{% static 'kfet/js/history.js' %}"></script>
{% endblock %}
{% block title %} {% block title %}
{% if account.user == request.user %} {% if account.user == request.user %}
@ -43,67 +54,42 @@
<div class="content-right-block"> <div class="content-right-block">
<h2>Historique</h2> <h2>Historique</h2>
<div id="history"> <div id="history">
{% spaceless %}
{% for ope in history %}
{% ifchanged ope.group.at|date:'dmY' %}
<div class="day">
{{ ope.group.at|date:'l j F' }}
</div>
{% endifchanged %}
{% ifchanged ope.group.pk %}
<div class="opegroup" data-opegroup="{{ ope.group.pk }}">
<span class="time">{{ ope.group.at|date:'H:i:s' }}</span>
<span class="amount">
{% if ope.group.on_acc.trigramme == 'LIQ' %}
{{ ope.group.amount|floatformat:2 }}
{% else %}
{{ ope.group.amount|ukf:ope.group.is_cof }}
{% endif %}
</span>
{% if perms.kfet.is_team and ope.group.valid_by %}
<span class="valid_by">Par {{ ope.group.valid_by.trigramme }}</span>
{% endif %}
{% if ope.group.comment %}
<span class="comment">{{ ope.group.comment }}</span>
{% endif %}
</div>
{% endifchanged %}
<div class="ope {% if ope.canceled_at %}canceled{% endif %}" data-ope="{{ ope.pk }}" data-opegroup="{{ ope.group.pk }}">
{% if ope.group.on_acc.trigramme == 'LIQ' %}
<span class="amount">{{ ope.amount|floatformat:2 }}€</span>
{% else %}
<span class="amount">{{ ope.amount|ukf:ope.group.is_cof }}</span>
{% endif %}
{% if ope.type == "purchase" %}
<span class="infos1">{{ ope.article_nb }}</span>
<span class="infos2">{{ ope.article.name }}</span>
{% else %}
<span class="infos1">{{ ope.amount|floatformat:2 }}€</span>
<span class="infos2">
{% if ope.type == "deposit" %}Charge{% else %}Retrait{% endif %}
</span>
{% endif %}
{% if ope.addcost_for %}
<span class="addcost">
{{ ope.addcost_amount|ukf:ope.group.is_cof }}UKF pour {{ ope.addcost_for.trigramme }}
</span>
{% endif %}
{% if ope.canceled_at %}
{% if perms.kfet.is_team and ope.canceled_by %}
<span class="canceled">
Annulé par {{ ope.canceled_by.trigramme }} le {{ ope.canceled_at }}
</span>
{% else %}
<span class="canceled">Annulé le {{ ope.canceled_at }}</span>
{% endif %}
{% endif %}
</div>
{% endfor %}
{% endspaceless %}
</div> </div>
</div> </div>
</div> </div>
</div> </div>
</div> </div>
<script type="text/javascript">
$(document).ready(function() {
settings = { 'subvention_cof': parseFloat({{ settings.subvention_cof|unlocalize }})}
khistory = new KHistory({
display_trigramme: false,
});
function getHistory() {
var data = {
'accounts': [{{ account.pk }}],
}
$.ajax({
dataType: "json",
url : "{% url 'kfet.history.json' %}",
method : "POST",
data : data,
})
.done(function(data) {
for (var i=0; i<data['opegroups'].length; i++) {
khistory.addOpeGroup(data['opegroups'][i]);
}
var nb_opes = khistory.$container.find('.ope:not(.canceled)').length;
$('#nb_opes').text(nb_opes);
});
}
getHistory();
});
</script>
{% endblock %} {% endblock %}

View file

@ -108,6 +108,8 @@
{{ operation_formset.empty_form }} {{ operation_formset.empty_form }}
</div> </div>
{% csrf_token %}
<script type="text/javascript"> <script type="text/javascript">
$(document).ready(function() { $(document).ready(function() {
// ----- // -----

View file

@ -58,7 +58,7 @@ def put_cleaned_data_in_dict(dict, form):
@login_required @login_required
@permission_required('kfet.is_team') @permission_required('kfet.is_team')
def account(request): def account(request):
accounts = Account.objects.order_by('trigramme') accounts = Account.objects.select_related('cofprofile__user').order_by('trigramme')
return render(request, "kfet/account.html", { 'accounts' : accounts }) return render(request, "kfet/account.html", { 'accounts' : accounts })
@login_required @login_required
@ -198,18 +198,16 @@ def account_create_ajax(request, username=None, login_clipper=None):
@login_required @login_required
def account_read(request, trigramme): def account_read(request, trigramme):
account = get_object_or_404(Account, trigramme=trigramme) try:
account = Account.objects.select_related('negative').get(trigramme=trigramme)
except Account.DoesNotExist:
raise Http404
# Checking permissions # Checking permissions
if not request.user.has_perm('kfet.is_team') \ if not request.user.has_perm('kfet.is_team') \
and request.user != account.user: and request.user != account.user:
raise PermissionDenied raise PermissionDenied
history = (Operation.objects
.select_related('group', 'group__valid_by', 'addcost_for', 'article')
.filter(group__on_acc=account)
.order_by('-group__at'))
addcosts = (OperationGroup.objects addcosts = (OperationGroup.objects
.filter(opes__addcost_for=account,opes__canceled_at=None) .filter(opes__addcost_for=account,opes__canceled_at=None)
.extra({'date':"date(at)"}) .extra({'date':"date(at)"})
@ -219,9 +217,9 @@ def account_read(request, trigramme):
return render(request, "kfet/account_read.html", { return render(request, "kfet/account_read.html", {
'account' : account, 'account' : account,
'history' : history,
'addcosts': addcosts, 'addcosts': addcosts,
}) 'settings': { 'subvention_cof': Settings.SUBVENTION_COF() },
})
# Account - Update # Account - Update
@ -1013,7 +1011,7 @@ def kpsul_cancel_operations(request):
data['warnings']['already_canceled'] = opes_already_canceled data['warnings']['already_canceled'] = opes_already_canceled
return JsonResponse(data) return JsonResponse(data)
@permission_required('kfet.is_team') @login_required
def history_json(request): def history_json(request):
# Récupération des paramètres # Récupération des paramètres
from_date = request.POST.get('from', None) from_date = request.POST.get('from', None)
@ -1041,6 +1039,9 @@ def history_json(request):
opegroups = opegroups.filter(checkout_id__in=checkouts) opegroups = opegroups.filter(checkout_id__in=checkouts)
if accounts: if accounts:
opegroups = opegroups.filter(on_acc_id__in=accounts) opegroups = opegroups.filter(on_acc_id__in=accounts)
# Un non-membre de l'équipe n'a que accès à son historique
if not request.user.has_perm('kfet.is_team'):
opegroups = opegroups.filter(on_acc=request.user.profile.account_kfet)
# Construction de la réponse # Construction de la réponse
opegroups_list = [] opegroups_list = []