Update search form

Fixes #6, #8
This commit is contained in:
Edgar P. Burkhart 2023-01-01 11:05:38 +01:00
parent 09fe3f6835
commit 8a84861e79
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.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)

View file

@ -53,14 +53,11 @@
accesskey="t">
{% translate "Transaction" %}
</a>
<form id="search" action="{% url 'search' %}" method="post">
{% csrf_token %}
<input type="text"
name="search"
placeholder="{% translate "Search" %}"
{% if search %}value="{{ search }}"{% endif %}/>
<input type="submit" value="{% translate "Search" %}" />
</form>
<a href="{% url 'search' %}"
class="{% if request.resolver_match.url_name == 'search' %}cur{% endif %}"
accesskey="r">
{% translate "Search" %}
</a>
<a href="{% url 'logout' %}" class="logout" accesskey="l">{% translate "Log out" %}</a>
</nav>
{% 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 %}
<h1>{% translate "Transactions" %}</h1>
{% if object %}<a href="{{ object.get_absolute_url }}">{{ object }}</a>{% endif %}
{% if search %}
<a href="{% url "search" %}">{% translate "Search" %}</a>
{% endif %}
{% if transactions %}
{% include "main/table/transaction.html" %}
{% 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(
"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"),
]

View file

@ -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 (