1
0
mirror of https://gitlab.crans.org/bde/nk20 synced 2025-06-21 09:58:23 +02:00

Added a search tab for the conso page

This commit is contained in:
Nicolas Margulies
2023-10-25 20:01:48 +02:00
parent ff6e207512
commit 0cc130092f
5 changed files with 153 additions and 62 deletions

View File

@ -103,6 +103,11 @@ SPDX-License-Identifier: GPL-3.0-or-later
</a>
</li>
{% endfor %}
<li class="nav-item">
<a class="nav-link font-weight-bold" data-toggle="tab" href="#search">
{% trans "Search" %}
</a>
</li>
</ul>
</div>
@ -123,6 +128,20 @@ SPDX-License-Identifier: GPL-3.0-or-later
</div>
</div>
{% endfor %}
<div class="tab-pane" id="search">
<input class="form-control mx-auto d-block mb-3"
placeholder="{% trans "Search button..." %}" type="text" id="search-input"/>
<div class="d-inline-flex flex-wrap justify-content-center" id="search-results">
{% for button in search_results %}
{% if button.display %}
<button class="btn btn-outline-dark rounded-0 flex-fill"
id="search_button{{ button.id }}" name="button" value="{{ button.name }}">
{{ button.name }} ({{ button.amount | pretty_money }})
</button>
{% endif %}
{% endfor %}
</div>
</div>
</div>
</div>
@ -182,5 +201,37 @@ SPDX-License-Identifier: GPL-3.0-or-later
{% endif %}
{% endfor %}
{% endfor %}
{% for button in search_results %}
{% if button.display %}
$("#search_button{{ button.id }}").click(function() {
addConso({{ button.destination_id }}, {{ button.amount }},
{{ polymorphic_ctype }}, {{ button.category_id }}, "{{ button.category.name|escapejs }}",
{{ button.id }}, "{{ button.name|escapejs }}");
});
{% endif %}
{% endfor %}
searchbar = document.getElementById("search-input")
const parser = new DOMParser();
old_pattern = null;
function updateSearch(force = false) {
let pattern = searchbar.value
if ((pattern === old_pattern || pattern === "") && !force)
return;
const xhr = new XMLHttpRequest();
xhr.open("GET", location.pathname + "?search=" +
encodeURI(pattern) + "#search", true)
xhr.onload = () => {
document.getElementById("search-results").innerHTML =
parser.parseFromString(xhr.responseText, "text/html").getElementById("search-results").innerHTML
};
xhr.send();
}
searchbar.addEventListener("input", function (e) {
debounce(updateSearch)()
});
</script>
{% endblock %}

View File

@ -10,12 +10,12 @@ from django.core.exceptions import PermissionDenied
from django.db.models import Q, F
from django.utils.translation import gettext_lazy as _
from django.views.generic import CreateView, UpdateView, DetailView
from django_tables2 import SingleTableView
from django.urls import reverse_lazy
from django_tables2 import SingleTableView
from activity.models import Entry
from note_kfet.inputs import AmountInput
from permission.backends import PermissionBackend
from permission.views import ProtectQuerysetMixin
from note_kfet.inputs import AmountInput
from .forms import TransactionTemplateForm, SearchTransactionForm
from .models import TemplateCategory, Transaction, TransactionTemplate, RecurrentTransaction, NoteSpecial, Note
@ -190,6 +190,14 @@ class ConsoView(ProtectQuerysetMixin, LoginRequiredMixin, SingleTableView):
).order_by('name').all()
context['polymorphic_ctype'] = ContentType.objects.get_for_model(RecurrentTransaction).pk
if "search" in self.request.GET and self.request.GET["search"]:
pattern = self.request.GET["search"]
context['search_results'] = TransactionTemplate.objects.filter(
name__iregex=pattern
).filter(
PermissionBackend.filter_queryset(self.request, TransactionTemplate, "view")
).filter(display=True).order_by('name').all()
return context