Add AccountSelect widget and update account field in forms
- Introduced AccountSelect widget for improved account selection in forms. - Updated AccountForm, StatementForm, and TransactionFiltersForm to use AccountSelect. - Modified account model options to include 'archived' in ordering. - Added new migration for account model options change.
This commit is contained in:
parent
ccbf433ec0
commit
a238401f13
7 changed files with 73 additions and 2 deletions
nummi
account
main/static/main/js
statement
transaction
|
@ -1,3 +1,4 @@
|
|||
from django.forms.widgets import Select
|
||||
from main.forms import IconInput, NummiForm
|
||||
|
||||
from .models import Account
|
||||
|
@ -15,3 +16,7 @@ class AccountForm(NummiForm):
|
|||
widgets = {
|
||||
"icon": IconInput(),
|
||||
}
|
||||
|
||||
|
||||
class AccountSelect(Select):
|
||||
template_name = "account/forms/widgets/account.html"
|
||||
|
|
20
nummi/account/migrations/0004_alter_account_options.py
Normal file
20
nummi/account/migrations/0004_alter_account_options.py
Normal file
|
@ -0,0 +1,20 @@
|
|||
# Generated by Django 4.2.7 on 2025-01-04 17:44
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [
|
||||
("account", "0003_account_archived"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterModelOptions(
|
||||
name="account",
|
||||
options={
|
||||
"ordering": ["-default", "archived", "name"],
|
||||
"verbose_name": "Account",
|
||||
"verbose_name_plural": "Accounts",
|
||||
},
|
||||
),
|
||||
]
|
|
@ -41,7 +41,7 @@ class Account(UserModel):
|
|||
)
|
||||
|
||||
class Meta:
|
||||
ordering = ["-default", "name"]
|
||||
ordering = ["-default", "archived", "name"]
|
||||
verbose_name = _("Account")
|
||||
verbose_name_plural = _("Accounts")
|
||||
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
{% load main_extras %}
|
||||
<span class="ico-input account-select">
|
||||
{{ "bank"|remix }}
|
||||
{% include "django/forms/widgets/select.html" %}
|
||||
</span>
|
|
@ -34,6 +34,21 @@ for (let form of forms) {
|
|||
setTimeout(setIcon, 0);
|
||||
});
|
||||
}
|
||||
let accountSelect = form.querySelector(".account-select");
|
||||
if (accountSelect) {
|
||||
let input = accountSelect.querySelector("select");
|
||||
let icon = accountSelect.querySelector("span");
|
||||
let icons = JSON.parse(input.dataset.icons);
|
||||
|
||||
function setIcon(event) {
|
||||
icon.className = `ri-${icons[input.value] || "bank"}-line`;
|
||||
}
|
||||
setIcon();
|
||||
input.addEventListener("input", setIcon);
|
||||
form.addEventListener("reset", (event) => {
|
||||
setTimeout(setIcon, 0);
|
||||
});
|
||||
}
|
||||
|
||||
let iconSelect = form.querySelector(".icon-select");
|
||||
if (iconSelect) {
|
||||
|
|
|
@ -1,3 +1,6 @@
|
|||
import json
|
||||
|
||||
from account.forms import AccountSelect
|
||||
from django import forms
|
||||
from django.forms.widgets import Select
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
@ -13,6 +16,7 @@ class StatementForm(NummiForm):
|
|||
fields = ["account", "start_date", "date", "start_value", "value", "file"]
|
||||
widgets = {
|
||||
"file": NummiFileInput,
|
||||
"account": AccountSelect,
|
||||
}
|
||||
|
||||
meta_fieldsets = [
|
||||
|
@ -40,6 +44,16 @@ class StatementForm(NummiForm):
|
|||
required=False,
|
||||
)
|
||||
|
||||
self.fields["account"].widget.attrs |= {
|
||||
"class": "account",
|
||||
"data-icons": json.dumps(
|
||||
{
|
||||
str(acc.id): acc.icon
|
||||
for acc in self.fields["account"].queryset.only("id", "icon")
|
||||
}
|
||||
),
|
||||
}
|
||||
|
||||
if _disable_account:
|
||||
self.fields["account"].disabled = True
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import json
|
||||
|
||||
from account.forms import AccountSelect
|
||||
from category.forms import CategorySelect
|
||||
from django import forms
|
||||
from django.forms import formset_factory
|
||||
|
@ -168,7 +169,9 @@ class TransactionFiltersForm(forms.Form):
|
|||
category = forms.ModelChoiceField(
|
||||
queryset=None, required=False, widget=CategorySelect()
|
||||
)
|
||||
account = forms.ModelChoiceField(queryset=None, required=False)
|
||||
account = forms.ModelChoiceField(
|
||||
queryset=None, required=False, widget=AccountSelect()
|
||||
)
|
||||
search = forms.CharField(label=_("Search"), required=False)
|
||||
sort_by = forms.ChoiceField(
|
||||
label=_("Sort by"),
|
||||
|
@ -198,3 +201,12 @@ class TransactionFiltersForm(forms.Form):
|
|||
}
|
||||
),
|
||||
}
|
||||
self.fields["account"].widget.attrs |= {
|
||||
"class": "account",
|
||||
"data-icons": json.dumps(
|
||||
{
|
||||
str(acc.id): acc.icon
|
||||
for acc in self.fields["account"].queryset.only("id", "icon")
|
||||
}
|
||||
),
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue