From 100a1fbcde914126c5234f22830ef0b6ddeba3c5 Mon Sep 17 00:00:00 2001 From: Murilo Rosa Date: Tue, 5 Mar 2024 12:49:24 +0000 Subject: [PATCH] create base models --- development_notebook/2024-03-04.md | 10 +- negligent_octopus/core/admin.py | 17 ++- .../core/migrations/0001_initial.py | 109 ++++++++++++++++++ negligent_octopus/core/models.py | 38 +++++- 4 files changed, 167 insertions(+), 7 deletions(-) create mode 100644 negligent_octopus/core/migrations/0001_initial.py diff --git a/development_notebook/2024-03-04.md b/development_notebook/2024-03-04.md index 664caf4..423b84a 100644 --- a/development_notebook/2024-03-04.md +++ b/development_notebook/2024-03-04.md @@ -10,7 +10,7 @@ The objective of today development session is to define basic models necessary f We'll iterate as per hakibenita's advice: 1. Define the business requirements. 2. Write down a naive implementation and model definition. -3. Challenge the solution. +3. Challenge the solution. 4. Refine and repeat. ## Business Requirements @@ -25,16 +25,16 @@ User <-1-N-> Account <-1-N-> Transactions ## Work Log * Create core app from 'manage.py startapp' +* Added app to project -* Add app to project - + Continued at 2024-03-05 * Define core models as above * Create superuser -* Define model admins +* Define model admins * Add data in model admins +* Commit changes # To Do []. [[Deploy a Django Project to pythonanywhere Free-tier]] []. [[Configure Django from Cookiecutter to MySQL]] - diff --git a/negligent_octopus/core/admin.py b/negligent_octopus/core/admin.py index 8c38f3f..2fcea2f 100644 --- a/negligent_octopus/core/admin.py +++ b/negligent_octopus/core/admin.py @@ -1,3 +1,18 @@ from django.contrib import admin -# Register your models here. +from .models import Account +from .models import Transaction + + +@admin.register(Account) +class AccountAdmin(admin.ModelAdmin): + list_display = ["name", "owner", "balance", "modified", "created", "is_removed"] + search_fields = ["name", "owner__username"] + list_filter = ["is_removed", "modified"] + + +@admin.register(Transaction) +class TransactionAdmin(admin.ModelAdmin): + list_display = ["title", "account", "get_account_owner", "date", "amount"] + search_fields = ["account__owner__username", "account__name", "title"] + list_filter = ["account__owner", "account", "date"] diff --git a/negligent_octopus/core/migrations/0001_initial.py b/negligent_octopus/core/migrations/0001_initial.py new file mode 100644 index 0000000..3c4edd2 --- /dev/null +++ b/negligent_octopus/core/migrations/0001_initial.py @@ -0,0 +1,109 @@ +# Generated by Django 4.2.10 on 2024-03-05 12:24 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion +import django.utils.timezone +import model_utils.fields + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name="Account", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ( + "created", + model_utils.fields.AutoCreatedField( + default=django.utils.timezone.now, + editable=False, + verbose_name="created", + ), + ), + ( + "modified", + model_utils.fields.AutoLastModifiedField( + default=django.utils.timezone.now, + editable=False, + verbose_name="modified", + ), + ), + ("is_removed", models.BooleanField(default=False)), + ("name", models.CharField(max_length=255)), + ("balance", models.FloatField(default=0.0, editable=False)), + ( + "owner", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + to=settings.AUTH_USER_MODEL, + ), + ), + ], + options={ + "verbose_name": "Account", + "verbose_name_plural": "Accounts", + "ordering": ["-modified", "name"], + }, + ), + migrations.CreateModel( + name="Transaction", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ( + "created", + model_utils.fields.AutoCreatedField( + default=django.utils.timezone.now, + editable=False, + verbose_name="created", + ), + ), + ( + "modified", + model_utils.fields.AutoLastModifiedField( + default=django.utils.timezone.now, + editable=False, + verbose_name="modified", + ), + ), + ("amount", models.FloatField()), + ("date", models.DateField(default=django.utils.timezone.now)), + ("title", models.CharField(max_length=127)), + ("description", models.TextField(blank=True)), + ( + "account", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, to="core.account" + ), + ), + ], + options={ + "verbose_name": "Transaction", + "verbose_name_plural": "Transactions", + "ordering": ["date", "-modified"], + }, + ), + ] diff --git a/negligent_octopus/core/models.py b/negligent_octopus/core/models.py index 71a8362..cfd2aae 100644 --- a/negligent_octopus/core/models.py +++ b/negligent_octopus/core/models.py @@ -1,3 +1,39 @@ from django.db import models +from django.utils import timezone +from model_utils.models import SoftDeletableModel +from model_utils.models import TimeStampedModel -# Create your models here. +from negligent_octopus.users.models import User + + +class Account(TimeStampedModel, SoftDeletableModel): + owner = models.ForeignKey(User, on_delete=models.CASCADE) + name = models.CharField(max_length=255) + balance = models.FloatField(default=0.0, editable=False) + + def __str__(self): + return self.name + + class Meta: + verbose_name = "Account" + verbose_name_plural = "Accounts" + ordering = ["-modified", "name"] + + +class Transaction(TimeStampedModel): + account = models.ForeignKey(Account, on_delete=models.CASCADE) + amount = models.FloatField() + date = models.DateField(default=timezone.now) + title = models.CharField(max_length=127) + description = models.TextField(blank=True) + + def get_account_owner(self): + return str(self.account.owner) + + def __str__(self): + return self.title + + class Meta: + verbose_name = "Transaction" + verbose_name_plural = "Transactions" + ordering = ["date", "-modified"]