From 1dc9d3f3a4dc6e59df5e7f95a1c11801713fc968 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Valenta?= <git@imaniti.org>
Date: Thu, 13 Apr 2023 19:30:27 +0200
Subject: [PATCH] add searching

---
 contracts/templates/contracts/search.html | 36 +++++++++++++++++++++++
 contracts/views.py                        | 27 +++++++++++++++++
 2 files changed, 63 insertions(+)
 create mode 100644 contracts/templates/contracts/search.html

diff --git a/contracts/templates/contracts/search.html b/contracts/templates/contracts/search.html
new file mode 100644
index 0000000..e6eaf27
--- /dev/null
+++ b/contracts/templates/contracts/search.html
@@ -0,0 +1,36 @@
+{% extends "shared/includes/base.html" %}
+{% load add %}
+
+{% block content %}
+    {% include "contracts/includes/double_heading.html" with icon="ico--search" heading="Vyhledávání" subheading="dle názvu smlouvy" %}
+
+    <div class="flex flex-row justify-center">
+        <form method="post">
+            {% csrf_token %}
+            <input
+                class="bg-grey-150 w-56 h-10 px-4 text-lg xl:h-14 xl:px-5"
+                type="text"
+                value="{% if query %}{{ query }}{% endif %}"
+                placeholder="Hledaný název"
+                aria-label="Vyhledávací box"
+            >
+            <button type="submit" class="btn text-lg">
+                <div class="btn__body">
+                    <i class="ico--search"></i>
+                </div>
+            </button>
+        </form>
+    </div>
+
+    {% if query %}
+        <h2 class="text-lg font-bold mb-10">
+            Výsledky vyhledávání
+        </h2>
+
+        {% if page|length != 0 %}
+            {% include "contracts/includes/contract_list.html" with page=page paginator=paginator %}
+        {% else %}
+            <span class="text-gray-300">Žádné výsledky.</span>
+        {% endif %}
+    {% endif %}
+{% endblock %}
diff --git a/contracts/views.py b/contracts/views.py
index 27e352a..b84ce38 100644
--- a/contracts/views.py
+++ b/contracts/views.py
@@ -119,6 +119,33 @@ def view_contract(request, id: int):
     )
 
 
+def search(request):
+    query = request.GET.get("q")
+    page = paginator = None
+    title = "Vyhledávání"
+
+    if query is not None:
+        title = f"Vyhledávání - {query}"
+
+        # WARNING: PostgreSQL-dependent
+        page, paginator = get_paginated_contracts(
+            request,
+            models.Q(name__search=query)
+        )
+
+    return render(
+        "contracts/search.html",
+        {
+            **get_base_context(request),
+            "title": title,
+            "description": "Vyhledávání smluv v registru České Pirátské Strany.",
+            "page": page,
+            "paginator": paginator,
+            "query": query,
+        }
+    )
+
+
 # BEGIN Filtered contract + submodel views
 
 
-- 
GitLab