Single layer of categories

This commit is contained in:
Edgar P. Burkhart 2022-05-20 14:39:00 +02:00
parent 36be624f5c
commit e38b781ff9
Signed by: edpibu
GPG Key ID: 9833D3C5A25BD227
7 changed files with 69 additions and 80 deletions

View File

@ -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 from django.db import migrations, models
import django.db.models.deletion import django.db.models.deletion
@ -9,48 +9,43 @@ class Migration(migrations.Migration):
initial = True initial = True
dependencies = [] dependencies = [
]
operations = [ operations = [
migrations.CreateModel( migrations.CreateModel(
name="Transaction", name='Category',
fields=[ fields=[
( ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
"id", ('name', models.CharField(max_length=256)),
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()),
], ],
), ),
migrations.CreateModel( migrations.CreateModel(
name="Invoice", name='Transaction',
fields=[ fields=[
( ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
"id", ('name', models.CharField(max_length=256)),
models.UUIDField( ('description', models.TextField()),
default=uuid.uuid4, ('value', models.DecimalField(decimal_places=2, max_digits=12)),
editable=False, ('date', models.DateField()),
primary_key=True, ('category', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='main.category')),
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='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',),
),
] ]

View File

@ -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'),
),
]

View File

@ -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',
),
]

View File

@ -5,14 +5,10 @@ from django.forms import ModelForm
class Category(models.Model): class Category(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
name = models.CharField(max_length=256) name = models.CharField(max_length=64)
parent = models.ForeignKey("self", on_delete=models.SET_NULL, blank=True, null=True)
def __str__(self): def __str__(self):
if self.parent: return self.name
return f"{self.parent}>{self.name}"
else:
return f"{self.name}"
class Transaction(models.Model): class Transaction(models.Model):
@ -35,7 +31,7 @@ class Transaction(models.Model):
class TransactionForm(ModelForm): class TransactionForm(ModelForm):
class Meta: class Meta:
model = Transaction model = Transaction
fields = ["name", "description", "value", "date"] fields = ["name", "description", "value", "date", "category"]
class Invoice(models.Model): class Invoice(models.Model):

View File

@ -12,4 +12,13 @@
{% endfor %} {% endfor %}
</ul> </ul>
{% endif %} {% endif %}
{% if categories %}
<ul>
{% for cat in categories %}
<li>{{ cat }}</li>
{% endfor %}
</ul>
{% endif %}
{% endblock %} {% endblock %}

View File

@ -3,7 +3,7 @@ from django.urls import path
from . import views from . import views
urlpatterns = [ 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("transaction/<uuid>", views.transaction, name="transaction"), path("transaction/<uuid>", views.transaction, name="transaction"),
path( path(

View File

@ -1,17 +1,18 @@
from django.shortcuts import render, get_object_or_404, redirect from django.shortcuts import render, get_object_or_404, redirect
from django.http import HttpResponse 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")
context = {
class IndexView(generic.ListView): "transactions": _transactions,
template_name = "main/index.html" "categories": _categories,
context_object_name = "transactions" }
return render(request, "main/index.html", context)
def get_queryset(self):
return Transaction.objects.order_by("-date")[:5]
def transaction(request, uuid=None): def transaction(request, uuid=None):