From 7cd8a37aec486580c78fa33a343738f82cea424d Mon Sep 17 00:00:00 2001
From: Pierre-antoine Comby <comby@crans.org>
Date: Mon, 23 Mar 2020 20:21:25 +0100
Subject: [PATCH] non dynamic search for button

---
 apps/note/api/views.py                       |  4 ++--
 apps/note/tables.py                          |  5 +++++
 apps/note/views.py                           | 11 +++++++++-
 templates/note/transactiontemplate_list.html | 23 ++++++++++++++++++--
 4 files changed, 38 insertions(+), 5 deletions(-)

diff --git a/apps/note/api/views.py b/apps/note/api/views.py
index f230a646..977441f7 100644
--- a/apps/note/api/views.py
+++ b/apps/note/api/views.py
@@ -89,9 +89,9 @@ class TransactionTemplateViewSet(ReadProtectedModelViewSet):
     """
     queryset = TransactionTemplate.objects.all()
     serializer_class = TransactionTemplateSerializer
-    filter_backends = [DjangoFilterBackend]
+    filter_backends = [SearchFilter, DjangoFilterBackend]
     filterset_fields = ['name', 'amount', 'display', 'category', ]
-
+    search_fields = ['$name', ]
 
 class TransactionViewSet(ReadProtectedModelViewSet):
     """
diff --git a/apps/note/tables.py b/apps/note/tables.py
index f50379c4..a2044a21 100644
--- a/apps/note/tables.py
+++ b/apps/note/tables.py
@@ -82,6 +82,11 @@ class ButtonTable(tables.Table):
             'class':
                 'table table condensed table-striped table-hover'
         }
+        row_attrs = {
+            'class': 'table-row',
+            'data-href': lambda record: record.pk
+        }
+
         model = TransactionTemplate
 
     def render_amount(self, value):
diff --git a/apps/note/views.py b/apps/note/views.py
index d656d7fc..a8118cb2 100644
--- a/apps/note/views.py
+++ b/apps/note/views.py
@@ -8,6 +8,7 @@ from django.db.models import Q
 from django.utils.translation import gettext_lazy as _
 from django.views.generic import CreateView, ListView, UpdateView
 from django_tables2 import SingleTableView
+from django.urls import reverse_lazy
 from permission.backends import PermissionBackend
 
 from .forms import TransactionTemplateForm
@@ -108,7 +109,7 @@ class TransactionTemplateCreateView(LoginRequiredMixin, CreateView):
     """
     model = TransactionTemplate
     form_class = TransactionTemplateForm
-
+    success_url = reverse_lazy('template_list')
 
 class TransactionTemplateListView(LoginRequiredMixin, SingleTableView):
     """
@@ -117,6 +118,14 @@ class TransactionTemplateListView(LoginRequiredMixin, SingleTableView):
     model = TransactionTemplate
     table_class = ButtonTable
 
+    def get_queryset(self):
+        name = self.request.GET.get('name','')
+        if (name != ''):
+            object_list = self.model.objects.filter(name__icontains = name)
+        else:
+            object_list = self.model.objects.all()
+        return object_list
+
 
 class TransactionTemplateUpdateView(LoginRequiredMixin, UpdateView):
     """
diff --git a/templates/note/transactiontemplate_list.html b/templates/note/transactiontemplate_list.html
index e8881988..d1934988 100644
--- a/templates/note/transactiontemplate_list.html
+++ b/templates/note/transactiontemplate_list.html
@@ -1,7 +1,26 @@
 {% extends "base.html" %}
 {% load pretty_money %}
+{% load i18n %}
 {% load render_table from django_tables2 %}
 {% block content %}
-<a class="btn btn-primary text-center" href="{% url 'note:template_create' %}">Créer un bouton</a>
-{% render_table table %}
+<div class="row justify-content-center mb-4">
+    <div class="col-md-10 text-center">
+        <h4>
+            {% trans "search button" %}
+        </h4>
+        <form action="" method="GET">
+            {% csrf_token %}
+            <input class="form-control mx-auto w-25" type="text" id="name" name="name"  value='{{ request.GET.name }}'/>
+        </form>
+        <ul class="list-group list-group-flush" id="alias_matched">
+        </ul>
+        <hr>
+        <a class="btn btn-primary text-center my-4" href="{% url 'note:template_create' %}">Créer un bouton</a>
+    </div>
+</div>
+<div class="row justify-content-center">   
+<div class="col-md-10">
+    {% render_table table %}
+</div>
+</div>
 {% endblock %}
-- 
GitLab