Add automatic category sorting
This commit is contained in:
parent
ea9d871e25
commit
c91994d041
4 changed files with 56 additions and 4 deletions
|
@ -0,0 +1,23 @@
|
|||
# Generated by Django 4.0.4 on 2022-05-20 14:17
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('main', '0003_category_parent_alter_category_name'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterModelOptions(
|
||||
name='category',
|
||||
options={'ordering': ['-parent_id', 'name']},
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='category',
|
||||
name='full_name',
|
||||
field=models.CharField(default='', max_length=512),
|
||||
preserve_default=False,
|
||||
),
|
||||
]
|
18
nummi/main/migrations/0005_alter_category_full_name.py
Normal file
18
nummi/main/migrations/0005_alter_category_full_name.py
Normal file
|
@ -0,0 +1,18 @@
|
|||
# Generated by Django 4.0.4 on 2022-05-20 14:18
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('main', '0004_alter_category_options_category_full_name'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='category',
|
||||
name='full_name',
|
||||
field=models.CharField(default='', editable=False, max_length=512),
|
||||
),
|
||||
]
|
|
@ -6,10 +6,21 @@ from django.forms import ModelForm
|
|||
class Category(models.Model):
|
||||
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
||||
name = models.CharField(max_length=64)
|
||||
full_name = models.CharField(max_length=512, editable=False, default="")
|
||||
parent = models.ForeignKey("self", on_delete=models.SET_NULL, blank=True, null=True)
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
if self.parent is None: return self.name
|
||||
return f"{self.parent}>{self.name}"
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
self.full_name = str(self)
|
||||
super().save(*args, **kwargs)
|
||||
for child in self.__class__.objects.filter(parent=self):
|
||||
child.save()
|
||||
|
||||
class Meta:
|
||||
ordering = ['full_name']
|
||||
|
||||
|
||||
class Transaction(models.Model):
|
||||
|
|
|
@ -9,14 +9,14 @@ from .models import Transaction, TransactionForm, Invoice, InvoiceForm, Category
|
|||
@login_required
|
||||
def index(request):
|
||||
_transactions = Transaction.objects.order_by("-date")[:5]
|
||||
_categories = Category.objects.filter(parent=None).order_by("name")
|
||||
_categories = Category.objects.filter(parent=None)
|
||||
|
||||
def _cat_list(cat):
|
||||
children = []
|
||||
for child in Category.objects.filter(parent=cat):
|
||||
children += _cat_list(child)
|
||||
if len(children) == 0: return cat,
|
||||
return cat, children
|
||||
if len(children) == 0: return cat.name,
|
||||
return cat.name, children
|
||||
|
||||
_cats = []
|
||||
for cat in _categories:
|
||||
|
|
Loading…
Reference in a new issue