From a609d7c9956790eda1e1d9b30ce096336c875950 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Fri, 10 Apr 2020 23:41:13 +0200 Subject: [PATCH] Fix credit/debit tabs --- apps/treasury/signals.py | 4 +- static/js/transfer.js | 65 ++++++++++++++++++++++++++++ templates/note/transaction_form.html | 52 +--------------------- 3 files changed, 69 insertions(+), 52 deletions(-) diff --git a/apps/treasury/signals.py b/apps/treasury/signals.py index 54c19c09..188be1a7 100644 --- a/apps/treasury/signals.py +++ b/apps/treasury/signals.py @@ -1,6 +1,7 @@ # Copyright (C) 2018-2020 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later +from note.models import NoteSpecial from treasury.models import SpecialTransactionProxy, RemittanceType @@ -8,5 +9,6 @@ def save_special_transaction(instance, created, **kwargs): """ When a special transaction is created, we create its linked proxy """ - if created and RemittanceType.objects.filter(note=instance.source).exists(): + if created and isinstance(instance.source, NoteSpecial) \ + and RemittanceType.objects.filter(note=instance.source).exists(): SpecialTransactionProxy.objects.create(transaction=instance, remittance=None).save() diff --git a/static/js/transfer.js b/static/js/transfer.js index 69fafac5..78a8ca08 100644 --- a/static/js/transfer.js +++ b/static/js/transfer.js @@ -34,6 +34,9 @@ $(document).ready(function() { if ($("#type_credit").is(":checked") || $("#type_debit").is(":checked")) { let arr = $("#type_credit").is(":checked") ? dests_notes_display : sources_notes_display; + if (arr.length === 0) + return; + let last = arr[arr.length - 1]; arr.length = 0; arr.push(last); @@ -65,6 +68,68 @@ $(document).ready(function() { autoCompleteNote("dest_note", "dest_note_list", dests, dests_notes_display, "dest_alias", "dest_note", "user_note", "profile_pic", checkUniqueNote); + let source = $("#source_note"); + let dest = $("#dest_note"); + + $("#type_gift").click(function() { + $("#special_transaction_div").addClass('d-none'); + source.attr('disabled', true); + source.val(username); + source.tooltip('hide'); + $("#source_note_list").addClass('d-none'); + dest.attr('disabled', false); + $("#dest_note_list").removeClass('d-none'); + }); + + $("#type_transfer").click(function() { + $("#special_transaction_div").addClass('d-none'); + source.attr('disabled', false); + $("#source_note_list").removeClass('d-none'); + dest.attr('disabled', false); + $("#dest_note_list").removeClass('d-none'); + }); + + $("#type_credit").click(function() { + $("#special_transaction_div").removeClass('d-none'); + $("#source_note_list").addClass('d-none'); + $("#dest_note_list").removeClass('d-none'); + source.attr('disabled', true); + source.val($("#credit_type option:selected").text()); + source.tooltip('hide'); + dest.attr('disabled', false); + dest.val(''); + dest.tooltip('hide'); + + if (dests_notes_display.length > 1) { + $("#dest_note_list").html(''); + dests_notes_display.length = 0; + } + }); + + $("#type_debit").click(function() { + $("#special_transaction_div").removeClass('d-none'); + $("#source_note_list").removeClass('d-none'); + $("#dest_note_list").addClass('d-none'); + source.attr('disabled', false); + source.val(''); + source.tooltip('hide'); + dest.attr('disabled', true); + dest.val($("#credit_type option:selected").text()); + dest.tooltip('hide'); + + if (sources_notes_display.length > 1) { + $("#source_note_list").html(''); + sources_notes_display.length = 0; + } + }); + + $("#credit_type").change(function() { + let type = $("#credit_type option:selected").text(); + if ($("#type_credit").is(":checked")) + source.val(type); + else + dest.val(type); + }); // Ensure we begin in gift mode. Removing these lines may cause problems when reloading. let type_gift = $("#type_gift"); // Default mode diff --git a/templates/note/transaction_form.html b/templates/note/transaction_form.html index d1cafa1f..c252f636 100644 --- a/templates/note/transaction_form.html +++ b/templates/note/transaction_form.html @@ -160,57 +160,7 @@ SPDX-License-Identifier: GPL-2.0-or-later TRANSFER_POLYMORPHIC_CTYPE = {{ polymorphic_ctype }}; SPECIAL_TRANSFER_POLYMORPHIC_CTYPE = {{ special_polymorphic_ctype }}; user_id = {{ user.note.pk }}; - - source = $("#source_note"); - dest = $("#dest_note"); - - $("#type_gift").click(function() { - $("#special_transaction_div").addClass('d-none'); - source.attr('disabled', true); - source.val("{{ user.username }}"); - source.tooltip('hide'); - $("#source_note_list").addClass('d-none'); - dest.attr('disabled', false); - $("#dest_note_list").removeClass('d-none'); - }); - - $("#type_transfer").click(function() { - $("#special_transaction_div").addClass('d-none'); - source.attr('disabled', false); - $("#source_note_list").removeClass('d-none'); - dest.attr('disabled', false); - $("#dest_note_list").removeClass('d-none'); - }); - - $("#type_credit").click(function() { - $("#special_transaction_div").removeClass('d-none'); - $("#source_note_list").addClass('d-none'); - $("#dest_note_list").removeClass('d-none'); - source.attr('disabled', true); - source.val($("#credit_type option:selected").text()); - source.tooltip('hide'); - dest.attr('disabled', false); - dest.val(''); - }); - - $("#type_debit").click(function() { - $("#special_transaction_div").removeClass('d-none'); - $("#source_note_list").removeClass('d-none'); - $("#dest_note_list").addClass('d-none'); - source.attr('disabled', false); - source.val(''); - dest.attr('disabled', true); - dest.val($("#credit_type option:selected").text()); - dest.tooltip('hide'); - }); - - $("#credit_type").change(function() { - let type = $("#credit_type option:selected").text(); - if ($("#type_credit").is(":checked")) - source.val(type); - else - dest.val(type); - }); + username = "{{ user.username }}"; {% endblock %}