From 8a84861e792b856f0093c4440fe75954a01b30c1 Mon Sep 17 00:00:00 2001 From: "Edgar P. Burkhart" Date: Sun, 1 Jan 2023 11:05:38 +0100 Subject: [PATCH] Update search form Fixes #6, #8 --- nummi/main/forms.py | 11 +++++++--- nummi/main/templates/main/base.html | 13 +++++------- nummi/main/templates/main/form/search.html | 7 +++++++ .../main/templates/main/list/transaction.html | 3 +++ nummi/main/templates/main/search.html | 19 +++++++++++++++++ nummi/main/urls.py | 2 +- nummi/main/views.py | 21 +++++++++++++++---- 7 files changed, 60 insertions(+), 16 deletions(-) create mode 100644 nummi/main/templates/main/form/search.html create mode 100644 nummi/main/templates/main/search.html diff --git a/nummi/main/forms.py b/nummi/main/forms.py index 7ade755..146432a 100644 --- a/nummi/main/forms.py +++ b/nummi/main/forms.py @@ -1,11 +1,11 @@ +from django import forms from django.db import models, transaction -from django.forms import CheckboxSelectMultiple, ModelForm, MultipleChoiceField from django.utils.translation import gettext_lazy as _ from .models import Account, Category, Invoice, Snapshot, Transaction -class NummiForm(ModelForm): +class NummiForm(forms.ModelForm): template_name = "main/form/base.html" def __init__(self, *args, user, **kwargs): @@ -73,7 +73,7 @@ class SnapshotForm(NummiForm): _user = kwargs.get("user") super().__init__(*args, **kwargs) self.fields["account"].queryset = Account.objects.filter(user=_user) - self.fields["transactions"] = MultipleChoiceField( + self.fields["transactions"] = forms.MultipleChoiceField( choices=( ((transaction.id), transaction) for transaction in Transaction.objects.filter(user=_user) @@ -90,3 +90,8 @@ class SnapshotForm(NummiForm): instance.transaction_set.add(*new_transactions, bulk=False) return instance + + +class SearchForm(forms.Form): + template_name = "main/form/search.html" + search = forms.CharField(label=_("Search"), max_length=128) diff --git a/nummi/main/templates/main/base.html b/nummi/main/templates/main/base.html index 7e035ba..31c3a5d 100644 --- a/nummi/main/templates/main/base.html +++ b/nummi/main/templates/main/base.html @@ -53,14 +53,11 @@ accesskey="t"> {% translate "Transaction" %} - + + {% translate "Search" %} + {% translate "Log out" %} {% endspaceless %} diff --git a/nummi/main/templates/main/form/search.html b/nummi/main/templates/main/form/search.html new file mode 100644 index 0000000..6b71216 --- /dev/null +++ b/nummi/main/templates/main/form/search.html @@ -0,0 +1,7 @@ +{% extends "main/form/base.html" %} +{% load i18n %} +{% block buttons %} +
+ +
+{% endblock %} diff --git a/nummi/main/templates/main/list/transaction.html b/nummi/main/templates/main/list/transaction.html index e6cbf81..f74b775 100644 --- a/nummi/main/templates/main/list/transaction.html +++ b/nummi/main/templates/main/list/transaction.html @@ -14,6 +14,9 @@ {% block body %}

{% translate "Transactions" %}

{% if object %}{{ object }}{% endif %} + {% if search %} + {% translate "Search" %} + {% endif %} {% if transactions %} {% include "main/table/transaction.html" %} {% if page_obj %} diff --git a/nummi/main/templates/main/search.html b/nummi/main/templates/main/search.html new file mode 100644 index 0000000..727b8fe --- /dev/null +++ b/nummi/main/templates/main/search.html @@ -0,0 +1,19 @@ +{% extends "main/base.html" %} +{% load static %} +{% load main_extras %} +{% load i18n %} +{% block link %} + {{ block.super }} + +{% endblock %} +{% block body %} +

{% translate "Search" %}

+ {% spaceless %} + + {% endspaceless %} +{% endblock %} diff --git a/nummi/main/urls.py b/nummi/main/urls.py index 2ea5506..007fa0f 100644 --- a/nummi/main/urls.py +++ b/nummi/main/urls.py @@ -78,6 +78,6 @@ urlpatterns = [ path( "snapshot//delete", views.SnapshotDeleteView.as_view(), name="del_snapshot" ), - path("search", views.SearchView.as_view(), name="search"), + path("search", views.SearchFormView.as_view(), name="search"), path("search/", views.SearchView.as_view(), name="search"), ] diff --git a/nummi/main/views.py b/nummi/main/views.py index e024617..8439100 100644 --- a/nummi/main/views.py +++ b/nummi/main/views.py @@ -16,13 +16,21 @@ from django.views import View from django.views.generic import ( CreateView, DeleteView, + FormView, ListView, TemplateView, UpdateView, ) from django.views.static import serve -from .forms import AccountForm, CategoryForm, InvoiceForm, SnapshotForm, TransactionForm +from .forms import ( + AccountForm, + CategoryForm, + InvoiceForm, + SearchForm, + SnapshotForm, + TransactionForm, +) from .models import Account, Category, Invoice, Snapshot, Transaction @@ -331,10 +339,15 @@ class CategoryTListView(CategoryMixin, TransactionListView): pass -class SearchView(TransactionListView): - def post(self, *args, **kwargs): - return redirect("search", search=self.request.POST.get("search")) +class SearchFormView(LoginRequiredMixin, FormView): + template_name = "main/search.html" + form_class = SearchForm + def form_valid(self, form): + return redirect("search", search=form.cleaned_data.get("search")) + + +class SearchView(TransactionListView): def get_queryset(self): self.search = self.kwargs["search"] return (