1
0
mirror of https://gitlab.crans.org/bde/nk20 synced 2024-11-26 18:37:12 +00:00

Give a reason when a transaction is invalidated

This commit is contained in:
Yohann D'ANELLO 2020-03-25 13:13:01 +01:00
parent 0ac94547d1
commit 23db42e448
3 changed files with 59 additions and 12 deletions

View File

@ -115,11 +115,19 @@ class Transaction(PolymorphicModel):
verbose_name=_('reason'),
max_length=255,
)
valid = models.BooleanField(
verbose_name=_('valid'),
default=True,
)
invalidity_reason = models.CharField(
verbose_name=_('invalidity reason'),
max_length=255,
default=None,
null=True,
)
class Meta:
verbose_name = _("transaction")
verbose_name_plural = _("transactions")
@ -152,6 +160,10 @@ class Transaction(PolymorphicModel):
self.source.balance -= to_transfer
self.destination.balance += to_transfer
# When a transaction is declared valid, we ensure that the invalidity reason is null, if it was
# previously invalid
self.invalidity_reason = None
# We save first the transaction, in case of the user has no right to transfer money
super().save(*args, **kwargs)

View File

@ -5,6 +5,7 @@ import html
import django_tables2 as tables
from django.db.models import F
from django.utils.html import format_html
from django_tables2.utils import A
from django.utils.translation import gettext_lazy as _
@ -20,19 +21,26 @@ class HistoryTable(tables.Table):
'table table-condensed table-striped table-hover'
}
model = Transaction
exclude = ("id", "polymorphic_ctype", )
exclude = ("id", "polymorphic_ctype", "invalidity_reason")
template_name = 'django_tables2/bootstrap4.html'
sequence = ('...', 'type', 'total', 'valid', )
sequence = ('...', 'type', 'total', 'valid',)
orderable = False
type = tables.Column()
total = tables.Column() # will use Transaction.total() !!
valid = tables.Column(attrs={"td": {"id": lambda record: "validate_" + str(record.id),
"class": lambda record: str(record.valid).lower() + ' validate',
"onclick": lambda record: 'de_validate(' + str(record.id) + ', '
+ str(record.valid).lower() + ')'}})
valid = tables.Column(
attrs={
"td": {
"id": lambda record: "validate_" + str(record.id),
"class": lambda record: str(record.valid).lower() + ' validate',
"onclick": lambda record: 'in_validate(' + str(record.id) + ', ' + str(record.valid).lower() + ')',
"onmouseover": lambda record: 'hover_validation_btn(' + str(record.id) + ', true)',
"onmouseout": lambda record: 'hover_validation_btn(' + str(record.id) + ', false)',
}
}
)
def order_total(self, queryset, is_descending):
# needed for rendering
@ -53,8 +61,13 @@ class HistoryTable(tables.Table):
def render_reason(self, value):
return html.unescape(value)
def render_valid(self, value):
return "" if value else ""
def render_valid(self, value, record):
val = "" if value else ""
val += "<br><input type='text' class='form-control' id='invalidity_reason_" + str(record.id) \
+ "' value='" + (record.invalidity_reason.replace('\'', '&apos;') if record.invalidity_reason else "") \
+ "'" + ("" if value else " disabled") \
+ " style='display: none;'>"
return format_html(val)
# function delete_button(id) provided in template file

View File

@ -260,8 +260,29 @@ function autoCompleteNote(field_id, alias_matched_id, note_list_id, notes, notes
});
}
function hover_validation_btn(id, show) {
let reason_obj = $("#invalidity_reason_" + id);
console.log(reason_obj.val());
if (show) {
reason_obj.show();
reason_obj.focus();
}
else
reason_obj.hide();
}
// When a validate button is clicked, we switch the validation status
function de_validate(id, validated) {
function in_validate(id, validated) {
let invalidity_reason;
let reason_obj = $("#invalidity_reason_" + id);
if (validated)
invalidity_reason = reason_obj.val();
else
invalidity_reason = null;
$("#validate_" + id).html("<strong style=\"font-size: 16pt;\">⟳ ...</strong>");
// Perform a PATCH request to the API in order to update the transaction
@ -274,12 +295,13 @@ function de_validate(id, validated) {
"X-CSRFTOKEN": CSRF_TOKEN
},
data: {
"resourcetype": "RecurrentTransaction",
valid: !validated
resourcetype: "RecurrentTransaction",
valid: !validated,
invalidity_reason: invalidity_reason,
},
success: function () {
// Refresh jQuery objects
$(".validate").click(de_validate);
$(".validate").click(in_validate);
refreshBalance();
// error if this method doesn't exist. Please define it.