Buttons list didn't work as well

This commit is contained in:
Yohann D'ANELLO 2020-07-30 15:49:59 +02:00
parent 484560fe4b
commit 9d8c588b78
11 changed files with 42 additions and 73 deletions

View File

@ -131,12 +131,10 @@ class ButtonTable(tables.Table):
row_attrs = { row_attrs = {
'class': lambda record: 'table-row ' + ('table-success' if record.display else 'table-danger'), 'class': lambda record: 'table-row ' + ('table-success' if record.display else 'table-danger'),
'id': lambda record: "row-" + str(record.pk), 'id': lambda record: "row-" + str(record.pk),
'data-href': lambda record: record.pk
} }
model = TransactionTemplate model = TransactionTemplate
exclude = ('id',) exclude = ('id',)
order_by = ('type', '-display', 'destination__name', 'name',)
edit = tables.LinkColumn('note:template_update', edit = tables.LinkColumn('note:template_update',
args=[A('pk')], args=[A('pk')],

View File

@ -5,6 +5,7 @@ import json
from django.conf import settings from django.conf import settings
from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.db.models import Q
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from django.views.generic import CreateView, UpdateView from django.views.generic import CreateView, UpdateView
from django_tables2 import SingleTableView from django_tables2 import SingleTableView
@ -72,6 +73,19 @@ class TransactionTemplateListView(ProtectQuerysetMixin, LoginRequiredMixin, Sing
model = TransactionTemplate model = TransactionTemplate
table_class = ButtonTable table_class = ButtonTable
def get_queryset(self, **kwargs):
"""
Filter the user list with the given pattern.
"""
qs = super().get_queryset().distinct()
if "search" in self.request.GET:
pattern = self.request.GET["search"]
qs = qs.filter(Q(name__iregex="^" + pattern) | Q(destination__club__name__iregex="^" + pattern))
qs = qs.order_by('-display', 'category__name', 'destination__club__name', 'name')
return qs
class TransactionTemplateUpdateView(ProtectQuerysetMixin, LoginRequiredMixin, UpdateView): class TransactionTemplateUpdateView(ProtectQuerysetMixin, LoginRequiredMixin, UpdateView):
""" """

View File

@ -1784,10 +1784,6 @@ msgstr ""
msgid "Name of the button..." msgid "Name of the button..."
msgstr "" msgstr ""
#: templates/note/transactiontemplate_list.html:16
msgid "Display visible buttons only"
msgstr ""
#: templates/note/transactiontemplate_list.html:21 #: templates/note/transactiontemplate_list.html:21
msgid "New button" msgid "New button"
msgstr "" msgstr ""

View File

@ -1846,10 +1846,6 @@ msgstr "Chercher un bouton"
msgid "Name of the button..." msgid "Name of the button..."
msgstr "Nom du bouton ..." msgstr "Nom du bouton ..."
#: templates/note/transactiontemplate_list.html:16
msgid "Display visible buttons only"
msgstr "N'afficher que les boutons visibles uniquement"
#: templates/note/transactiontemplate_list.html:21 #: templates/note/transactiontemplate_list.html:21
msgid "New button" msgid "New button"
msgstr "Nouveau bouton" msgstr "Nouveau bouton"

View File

@ -13,7 +13,7 @@
"note": note_id "note": note_id
} }
).done(function(){ ).done(function(){
$("#alias_table").load(location.href+ " #alias_table"); $("#alias_table").load(location.pathname+ " #alias_table");
addMsg("Alias ajouté","success"); addMsg("Alias ajouté","success");
}) })
.fail(function(xhr, textStatus, error){ .fail(function(xhr, textStatus, error){
@ -29,7 +29,7 @@
}) })
.done(function(){ .done(function(){
addMsg('Alias supprimé','success'); addMsg('Alias supprimé','success');
$("#alias_table").load(location.href + " #alias_table"); $("#alias_table").load(location.pathname + " #alias_table");
}) })
.fail(function(xhr,textStatus, error){ .fail(function(xhr,textStatus, error){
errMsg(xhr.responseJSON); errMsg(xhr.responseJSON);

View File

@ -92,7 +92,7 @@
}) })
.done(function() { .done(function() {
addMsg('Invité supprimé','success'); addMsg('Invité supprimé','success');
$("#guests_table").load(location.href + " #guests_table"); $("#guests_table").load(location.pathname + " #guests_table");
}) })
.fail(function(xhr, textStatus, error) { .fail(function(xhr, textStatus, error) {
errMsg(xhr.responseJSON); errMsg(xhr.responseJSON);

View File

@ -51,7 +51,7 @@
if ((pattern === old_pattern || pattern === "") && !force) if ((pattern === old_pattern || pattern === "") && !force)
return; return;
$("#entry_table").load(location.href + "?search=" + pattern.replace(" ", "%20") + " #entry_table", init); $("#entry_table").load(location.pathname + "?search=" + pattern.replace(" ", "%20") + " #entry_table", init);
refreshBalance(); refreshBalance();
} }

View File

@ -36,7 +36,7 @@
function reloadTable() { function reloadTable() {
let pattern = searchbar_obj.val(); let pattern = searchbar_obj.val();
$("#club_table").load(location.href + "?search=" + pattern.replace(" ", "%20") + " #club_table", init); $("#club_table").load(location.pathname + "?search=" + pattern.replace(" ", "%20") + " #club_table", init);
} }
searchbar_obj.keyup(function() { searchbar_obj.keyup(function() {

View File

@ -34,7 +34,7 @@
if (pattern === old_pattern || pattern === "") if (pattern === old_pattern || pattern === "")
return; return;
$("#user_table").load(location.href + "?search=" + pattern.replace(" ", "%20") + " #user_table", init); $("#user_table").load(location.pathname + "?search=" + pattern.replace(" ", "%20") + " #user_table", init);
} }
searchbar_obj.keyup(function() { searchbar_obj.keyup(function() {

View File

@ -9,14 +9,6 @@
{% trans "Search button" %} {% trans "Search button" %}
</h4> </h4>
<input class="form-control mx-auto w-25" type="text" id="search_field" placeholder="{% trans "Name of the button..." %}"> <input class="form-control mx-auto w-25" type="text" id="search_field" placeholder="{% trans "Name of the button..." %}">
<div class="form-group">
<div id="div_active_only" class="form-check">
<label for="active_only" class="form-check-label">
<input type="checkbox" name="active_only" class="checkboxinput form-check-input" checked="" id="active_only">
{% trans "Display visible buttons only" %}
</label>
</div>
</div>
<hr> <hr>
<a class="btn btn-primary text-center my-1" href="{% url 'note:template_create' %}">{% trans "New button" %}</a> <a class="btn btn-primary text-center my-1" href="{% url 'note:template_create' %}">{% trans "New button" %}</a>
</div> </div>
@ -36,66 +28,39 @@
{% endblock %} {% endblock %}
{% block extrajavascript %} {% block extrajavascript %}
<script> <script type="text/javascript">
/* fonction appelée à la fin du timer */ $(document).ready(function() {
function getInfo() { let searchbar_obj = $("#search_field");
var asked = $("#search_field").val(); var timer_on = false;
/* on ne fait la requête que si on a au moins un caractère pour chercher */ var timer;
if (asked.length >= 1) {
$.getJSON("/api/note/transaction/template/?format=json&search=" + asked + ($("#active_only").is(":checked") ? "&display=true" : ""), function(buttons) {
console.log(buttons);
let selected_id = buttons.results.map((a => "#row-" + a.id));
console.log(".table-row " + selected_id.join());
$(".table-row " + selected_id.join()).removeClass('d-none');
$(".table-row").not(selected_id.join()).addClass('d-none');
});
}
else {
if ($("#active_only").is(":checked")) {
$('.table-success').removeClass('d-none');
$('.table-danger').addClass('d-none');
}
else {
// show everything
$('table tr').removeClass('d-none');
}
}
}
var timer; function reloadTable() {
var timer_on; let pattern = searchbar_obj.val();
/* Fontion appelée quand le texte change (délenche le timer) */ $("#buttons_table").load(location.pathname + "?search=" + pattern.replace(" ", "%20") + " #buttons_table");
function search_field_moved() {
if (timer_on) { // Si le timer a déjà été lancé, on réinitialise le compteur.
clearTimeout(timer);
timer = setTimeout(getInfo, 300);
} }
else { // Sinon, on le lance et on enregistre le fait qu'il tourne.
timer = setTimeout(getInfo, 300); searchbar_obj.keyup(function() {
if (timer_on)
clearTimeout(timer);
timer_on = true; timer_on = true;
} setTimeout(reloadTable, 0);
} });
});
// on click of button "delete" , call the API // on click of button "delete" , call the API
function delete_button(button_id) { function delete_button(button_id) {
$.ajax({ $.ajax({
url:"/api/note/transaction/template/"+button_id+"/", url:"/api/note/transaction/template/" + button_id + "/",
method:"DELETE", method:"DELETE",
headers: {"X-CSRFTOKEN": CSRF_TOKEN} headers: {"X-CSRFTOKEN": CSRF_TOKEN}
}) })
.done(function(){ .done(function() {
addMsg('{% trans "button successfully deleted "%}','success'); addMsg('{% trans "button successfully deleted "%}','success');
$("#buttons_table").load("{% url 'note:template_list' %} #buttons_table"); $("#buttons_table").load(location.pathname + "?search=" + $("#search_field").val().replace(" ", "%20") + " #buttons_table");
}) })
.fail(function(){ .fail(function() {
addMsg(' {% trans "Unable to delete button "%} #' + button_id,'danger' ) addMsg('{% trans "Unable to delete button "%} #' + button_id, 'danger')
}); });
} }
$(document).ready(function() {
$("#search_field").keyup(search_field_moved);
$("#active_only").change(search_field_moved);
search_field_moved();
});
</script> </script>
{% endblock %} {% endblock %}

View File

@ -32,7 +32,7 @@
if (pattern === old_pattern || pattern === "") if (pattern === old_pattern || pattern === "")
return; return;
$("#user_table").load(location.href + "?search=" + pattern.replace(" ", "%20") + " #user_table", init); $("#user_table").load(location.pathname + "?search=" + pattern.replace(" ", "%20") + " #user_table", init);
$(".table-row").click(function() { $(".table-row").click(function() {
window.document.location = $(this).data("href"); window.document.location = $(this).data("href");