Update search form

Fixes #6, fixes #8
This commit is contained in:
Edgar P. Burkhart 2023-01-01 11:05:38 +01:00
parent 09fe3f6835
commit 344a1558af
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227
7 changed files with 60 additions and 16 deletions

View file

@ -1,11 +1,11 @@
from django import forms
from django.db import models, transaction from django.db import models, transaction
from django.forms import CheckboxSelectMultiple, ModelForm, MultipleChoiceField
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from .models import Account, Category, Invoice, Snapshot, Transaction from .models import Account, Category, Invoice, Snapshot, Transaction
class NummiForm(ModelForm): class NummiForm(forms.ModelForm):
template_name = "main/form/base.html" template_name = "main/form/base.html"
def __init__(self, *args, user, **kwargs): def __init__(self, *args, user, **kwargs):
@ -73,7 +73,7 @@ class SnapshotForm(NummiForm):
_user = kwargs.get("user") _user = kwargs.get("user")
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.fields["account"].queryset = Account.objects.filter(user=_user) self.fields["account"].queryset = Account.objects.filter(user=_user)
self.fields["transactions"] = MultipleChoiceField( self.fields["transactions"] = forms.MultipleChoiceField(
choices=( choices=(
((transaction.id), transaction) ((transaction.id), transaction)
for transaction in Transaction.objects.filter(user=_user) for transaction in Transaction.objects.filter(user=_user)
@ -90,3 +90,8 @@ class SnapshotForm(NummiForm):
instance.transaction_set.add(*new_transactions, bulk=False) instance.transaction_set.add(*new_transactions, bulk=False)
return instance return instance
class SearchForm(forms.Form):
template_name = "main/form/search.html"
search = forms.CharField(label=_("Search"), max_length=128)

View file

@ -53,14 +53,11 @@
accesskey="t"> accesskey="t">
{% translate "Transaction" %} {% translate "Transaction" %}
</a> </a>
<form id="search" action="{% url 'search' %}" method="post"> <a href="{% url 'search' %}"
{% csrf_token %} class="{% if request.resolver_match.url_name == 'search' %}cur{% endif %}"
<input type="text" accesskey="r">
name="search" {% translate "Search" %}
placeholder="{% translate "Search" %}" </a>
{% if search %}value="{{ search }}"{% endif %}/>
<input type="submit" value="{% translate "Search" %}" />
</form>
<a href="{% url 'logout' %}" class="logout" accesskey="l">{% translate "Log out" %}</a> <a href="{% url 'logout' %}" class="logout" accesskey="l">{% translate "Log out" %}</a>
</nav> </nav>
{% endspaceless %} {% endspaceless %}

View file

@ -0,0 +1,7 @@
{% extends "main/form/base.html" %}
{% load i18n %}
{% block buttons %}
<div class="buttons">
<input type="submit" value="{% translate "Search" %}" />
</div>
{% endblock %}

View file

@ -14,6 +14,9 @@
{% block body %} {% block body %}
<h1>{% translate "Transactions" %}</h1> <h1>{% translate "Transactions" %}</h1>
{% if object %}<a href="{{ object.get_absolute_url }}">{{ object }}</a>{% endif %} {% if object %}<a href="{{ object.get_absolute_url }}">{{ object }}</a>{% endif %}
{% if search %}
<a href="{% url "search" %}">{% translate "Search" %}</a>
{% endif %}
{% if transactions %} {% if transactions %}
{% include "main/table/transaction.html" %} {% include "main/table/transaction.html" %}
{% if page_obj %} {% if page_obj %}

View file

@ -0,0 +1,19 @@
{% extends "main/base.html" %}
{% load static %}
{% load main_extras %}
{% load i18n %}
{% block link %}
{{ block.super }}
<link rel="stylesheet"
href="{% static 'main/css/form.css' %}"
type="text/css"/>
{% endblock %}
{% block body %}
<h1>{% translate "Search" %}</h1>
{% spaceless %}
<form id="search" method="post">
{% csrf_token %}
{{ form }}
</form>
{% endspaceless %}
{% endblock %}

View file

@ -78,6 +78,6 @@ urlpatterns = [
path( path(
"snapshot/<pk>/delete", views.SnapshotDeleteView.as_view(), name="del_snapshot" "snapshot/<pk>/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/<search>", views.SearchView.as_view(), name="search"), path("search/<search>", views.SearchView.as_view(), name="search"),
] ]

View file

@ -16,13 +16,21 @@ from django.views import View
from django.views.generic import ( from django.views.generic import (
CreateView, CreateView,
DeleteView, DeleteView,
FormView,
ListView, ListView,
TemplateView, TemplateView,
UpdateView, UpdateView,
) )
from django.views.static import serve 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 from .models import Account, Category, Invoice, Snapshot, Transaction
@ -331,10 +339,15 @@ class CategoryTListView(CategoryMixin, TransactionListView):
pass pass
class SearchView(TransactionListView): class SearchFormView(LoginRequiredMixin, FormView):
def post(self, *args, **kwargs): template_name = "main/search.html"
return redirect("search", search=self.request.POST.get("search")) form_class = SearchForm
def form_valid(self, form):
return redirect("search", search=form.cleaned_data.get("search"))
class SearchView(TransactionListView):
def get_queryset(self): def get_queryset(self):
self.search = self.kwargs["search"] self.search = self.kwargs["search"]
return ( return (