From c91994d041c8cbd0a6042b7946d507edb0a832f2 Mon Sep 17 00:00:00 2001 From: "Edgar P. Burkhart" Date: Fri, 20 May 2022 16:20:42 +0200 Subject: [PATCH] Add automatic category sorting --- ...ter_category_options_category_full_name.py | 23 +++++++++++++++++++ .../0005_alter_category_full_name.py | 18 +++++++++++++++ nummi/main/models.py | 13 ++++++++++- nummi/main/views.py | 6 ++--- 4 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 nummi/main/migrations/0004_alter_category_options_category_full_name.py create mode 100644 nummi/main/migrations/0005_alter_category_full_name.py diff --git a/nummi/main/migrations/0004_alter_category_options_category_full_name.py b/nummi/main/migrations/0004_alter_category_options_category_full_name.py new file mode 100644 index 0000000..931edf6 --- /dev/null +++ b/nummi/main/migrations/0004_alter_category_options_category_full_name.py @@ -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, + ), + ] diff --git a/nummi/main/migrations/0005_alter_category_full_name.py b/nummi/main/migrations/0005_alter_category_full_name.py new file mode 100644 index 0000000..c52a980 --- /dev/null +++ b/nummi/main/migrations/0005_alter_category_full_name.py @@ -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), + ), + ] diff --git a/nummi/main/models.py b/nummi/main/models.py index ef5a54e..a218d1a 100644 --- a/nummi/main/models.py +++ b/nummi/main/models.py @@ -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): diff --git a/nummi/main/views.py b/nummi/main/views.py index ee8960a..8d20f80 100644 --- a/nummi/main/views.py +++ b/nummi/main/views.py @@ -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: