From e38b781ff94f33ce0e70608183d3d9d140e943cc Mon Sep 17 00:00:00 2001 From: "Edgar P. Burkhart" Date: Fri, 20 May 2022 14:39:00 +0200 Subject: [PATCH] Single layer of categories --- nummi/main/migrations/0001_initial.py | 65 +++++++++---------- .../0002_category_transaction_category.py | 28 -------- .../migrations/0002_delete_subcategory.py | 16 +++++ nummi/main/models.py | 10 +-- nummi/main/templates/main/index.html | 9 +++ nummi/main/urls.py | 2 +- nummi/main/views.py | 19 +++--- 7 files changed, 69 insertions(+), 80 deletions(-) delete mode 100644 nummi/main/migrations/0002_category_transaction_category.py create mode 100644 nummi/main/migrations/0002_delete_subcategory.py diff --git a/nummi/main/migrations/0001_initial.py b/nummi/main/migrations/0001_initial.py index eec40f2..3d62d2d 100644 --- a/nummi/main/migrations/0001_initial.py +++ b/nummi/main/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 4.0.4 on 2022-05-19 14:02 +# Generated by Django 4.0.4 on 2022-05-20 12:24 from django.db import migrations, models import django.db.models.deletion @@ -9,48 +9,43 @@ class Migration(migrations.Migration): initial = True - dependencies = [] + dependencies = [ + ] operations = [ migrations.CreateModel( - name="Transaction", + name='Category', fields=[ - ( - "id", - models.UUIDField( - default=uuid.uuid4, - editable=False, - primary_key=True, - serialize=False, - ), - ), - ("name", models.CharField(max_length=256)), - ("description", models.TextField()), - ("value", models.DecimalField(decimal_places=2, max_digits=12)), - ("date", models.DateField()), + ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)), + ('name', models.CharField(max_length=256)), ], ), migrations.CreateModel( - name="Invoice", + name='Transaction', fields=[ - ( - "id", - models.UUIDField( - default=uuid.uuid4, - editable=False, - primary_key=True, - serialize=False, - ), - ), - ("name", models.CharField(max_length=256)), - ("file", models.FileField(upload_to="invoices/")), - ( - "transaction", - models.ForeignKey( - on_delete=django.db.models.deletion.CASCADE, - to="main.transaction", - ), - ), + ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)), + ('name', models.CharField(max_length=256)), + ('description', models.TextField()), + ('value', models.DecimalField(decimal_places=2, max_digits=12)), + ('date', models.DateField()), + ('category', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='main.category')), ], ), + migrations.CreateModel( + name='Invoice', + fields=[ + ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)), + ('name', models.CharField(max_length=256)), + ('file', models.FileField(upload_to='invoices/')), + ('transaction', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='main.transaction')), + ], + ), + migrations.CreateModel( + name='SubCategory', + fields=[ + ('category_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='main.category')), + ('parent', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='category_parent', to='main.category')), + ], + bases=('main.category',), + ), ] diff --git a/nummi/main/migrations/0002_category_transaction_category.py b/nummi/main/migrations/0002_category_transaction_category.py deleted file mode 100644 index be19f25..0000000 --- a/nummi/main/migrations/0002_category_transaction_category.py +++ /dev/null @@ -1,28 +0,0 @@ -# Generated by Django 4.0.4 on 2022-05-20 11:34 - -from django.db import migrations, models -import django.db.models.deletion -import uuid - - -class Migration(migrations.Migration): - - dependencies = [ - ('main', '0001_initial'), - ] - - operations = [ - migrations.CreateModel( - name='Category', - fields=[ - ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)), - ('name', models.CharField(max_length=256)), - ('parent', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='main.category')), - ], - ), - migrations.AddField( - model_name='transaction', - name='category', - field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='main.category'), - ), - ] diff --git a/nummi/main/migrations/0002_delete_subcategory.py b/nummi/main/migrations/0002_delete_subcategory.py new file mode 100644 index 0000000..a41e256 --- /dev/null +++ b/nummi/main/migrations/0002_delete_subcategory.py @@ -0,0 +1,16 @@ +# Generated by Django 4.0.4 on 2022-05-20 12:30 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('main', '0001_initial'), + ] + + operations = [ + migrations.DeleteModel( + name='SubCategory', + ), + ] diff --git a/nummi/main/models.py b/nummi/main/models.py index 8e6a895..cafb07d 100644 --- a/nummi/main/models.py +++ b/nummi/main/models.py @@ -5,14 +5,10 @@ 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=256) - parent = models.ForeignKey("self", on_delete=models.SET_NULL, blank=True, null=True) + name = models.CharField(max_length=64) def __str__(self): - if self.parent: - return f"{self.parent}>{self.name}" - else: - return f"{self.name}" + return self.name class Transaction(models.Model): @@ -35,7 +31,7 @@ class Transaction(models.Model): class TransactionForm(ModelForm): class Meta: model = Transaction - fields = ["name", "description", "value", "date"] + fields = ["name", "description", "value", "date", "category"] class Invoice(models.Model): diff --git a/nummi/main/templates/main/index.html b/nummi/main/templates/main/index.html index 1566229..63a50c3 100644 --- a/nummi/main/templates/main/index.html +++ b/nummi/main/templates/main/index.html @@ -12,4 +12,13 @@ {% endfor %} {% endif %} + +{% if categories %} + +{% endif %} + {% endblock %} diff --git a/nummi/main/urls.py b/nummi/main/urls.py index 45a0bf5..1c114be 100644 --- a/nummi/main/urls.py +++ b/nummi/main/urls.py @@ -3,7 +3,7 @@ from django.urls import path from . import views urlpatterns = [ - path("", views.IndexView.as_view(), name="index"), + path("", views.index, name="index"), path("transaction", views.transaction, name="transaction"), path("transaction/", views.transaction, name="transaction"), path( diff --git a/nummi/main/views.py b/nummi/main/views.py index 619b9c2..f7929f9 100644 --- a/nummi/main/views.py +++ b/nummi/main/views.py @@ -1,17 +1,18 @@ from django.shortcuts import render, get_object_or_404, redirect from django.http import HttpResponse -from django.views import generic + +from .models import Transaction, TransactionForm, Invoice, InvoiceForm, Category -from .models import Transaction, TransactionForm, Invoice, InvoiceForm +def index(request): + _transactions = Transaction.objects.order_by("-date")[:5] + _categories = Category.objects.order_by("name") - -class IndexView(generic.ListView): - template_name = "main/index.html" - context_object_name = "transactions" - - def get_queryset(self): - return Transaction.objects.order_by("-date")[:5] + context = { + "transactions": _transactions, + "categories": _categories, + } + return render(request, "main/index.html", context) def transaction(request, uuid=None):