From 78c2e45ca46dc84610205836904377d8d8b3e535 Mon Sep 17 00:00:00 2001 From: Emanuele Date: Sun, 20 Oct 2024 11:24:24 +0200 Subject: [PATCH] Better inLine version with nested_admin --- techdb/flightslot/admin.py | 25 +++++--- ...06_course_student_active_student_course.py | 33 ++++++++++ techdb/flightslot/models.py | 63 ++++++++++++++++--- techdb/poetry.lock | 32 +++++++--- techdb/pyproject.toml | 2 +- techdb/techdb/settings.py | 2 +- 6 files changed, 132 insertions(+), 25 deletions(-) create mode 100644 techdb/flightslot/migrations/0006_course_student_active_student_course.py diff --git a/techdb/flightslot/admin.py b/techdb/flightslot/admin.py index e8d4780..3c9a4e5 100644 --- a/techdb/flightslot/admin.py +++ b/techdb/flightslot/admin.py @@ -1,28 +1,37 @@ from django.contrib import admin -from nested_inline.admin import NestedTabularInline, NestedModelAdmin +import nested_admin from .models import * # Register your models here. -admin.site.register(Student, StudentAdmin) -admin.site.register(MissionProfile) - -class HourBuildingLegInline(NestedTabularInline): +class HourBuildingLegInline(nested_admin.NestedTabularInline): model = HourBuildingLeg extra = 1 fk_name = 'hb' -class HourBuildingInLine(NestedTabularInline): +class HourBuildingInLine(nested_admin.NestedTabularInline): model = HourBuilding extra = 1 inlines = [HourBuildingLegInline] fk_name = 'weekpref' -class TrainingInLIne(NestedTabularInline): +class TrainingInLIne(nested_admin.NestedTabularInline): model = Training extra = 0 fk_name = 'weekpref' -class WeekPreferenceAdmin(NestedModelAdmin): +class WeekPreferenceAdmin(nested_admin.NestedModelAdmin): + list_display = ('week', 'student') + list_filter = ['week', 'student'] inlines = [TrainingInLIne, HourBuildingInLine] +class StudentAdmin(admin.ModelAdmin): + list_display = ("name", "surname", "course", "active") + list_filter = ["course", "active"] + +class CourseAdmin(admin.ModelAdmin): + list_filter = ["ctype"] + +admin.site.register(Course, CourseAdmin) +admin.site.register(MissionProfile) +admin.site.register(Student, StudentAdmin) admin.site.register(WeekPreference, WeekPreferenceAdmin) \ No newline at end of file diff --git a/techdb/flightslot/migrations/0006_course_student_active_student_course.py b/techdb/flightslot/migrations/0006_course_student_active_student_course.py new file mode 100644 index 0000000..920fde1 --- /dev/null +++ b/techdb/flightslot/migrations/0006_course_student_active_student_course.py @@ -0,0 +1,33 @@ +# Generated by Django 5.1.2 on 2024-10-20 09:14 + +import django.db.models.deletion +import django.db.models.functions.datetime +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('flightslot', '0005_hourbuilding_notes_training_notes_and_more'), + ] + + operations = [ + migrations.CreateModel( + name='Course', + fields=[ + ('id', models.AutoField(primary_key=True, serialize=False)), + ('ctype', models.CharField(choices=[('PPL', 'PPL'), ('ATPL', 'ATPL')])), + ('cnumber', models.PositiveSmallIntegerField(default=django.db.models.functions.datetime.ExtractYear(django.db.models.functions.datetime.Now()))), + ], + ), + migrations.AddField( + model_name='student', + name='active', + field=models.BooleanField(default=True), + ), + migrations.AddField( + model_name='student', + name='course', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.DO_NOTHING, to='flightslot.course'), + ), + ] diff --git a/techdb/flightslot/models.py b/techdb/flightslot/models.py index a3274eb..98dcfed 100644 --- a/techdb/flightslot/models.py +++ b/techdb/flightslot/models.py @@ -1,13 +1,31 @@ from django.db import models from django.contrib import admin -from django.db.models.functions import Now, ExtractWeek -from django.http import HttpRequest +from django.db.models.functions import Now, ExtractWeek, ExtractYear from django.utils.translation import gettext_lazy as _ # Create your models here. -class StudentAdmin(admin.ModelAdmin): - list_display = ("name", "surname", "email") - list_filter = [] + +class CourseTypes(models.TextChoices): + PPL = "PPL", _("PPL") + ATPL = "ATPL", _("ATPL") + +class Course(models.Model): + id = models.AutoField( + primary_key=True + ) + + ctype = models.CharField( + null=False, + choices=CourseTypes + ) + + cnumber = models.PositiveSmallIntegerField( + null=False, + default=ExtractYear(Now()) + ) + + def __str__(self): + return f"{self.ctype}-{self.cnumber}" class Student(models.Model): id = models.AutoField( @@ -29,6 +47,20 @@ class Student(models.Model): max_length=32 ) + course = models.ForeignKey( + Course, + on_delete=models.DO_NOTHING, + null=True + ) + + active = models.BooleanField( + null=False, + default=True + ) + + def __str__(self): + return f"{self.surname} {self.name[0]}. - {self.course}" + class WeekPreference(models.Model): id = models.BigAutoField( primary_key=True @@ -48,6 +80,9 @@ class WeekPreference(models.Model): on_delete=models.DO_NOTHING ) + def __str__(self): + return f"{self.week} - {self.student.surname} {self.student.name[0]}." + class MissionType(models.TextChoices): OTHER = "OTHER", _("OTHER") PPL = "PPL", _("PPL") @@ -80,7 +115,8 @@ class MissionProfile(models.Model): notes = models.TextField( max_length=140, - null=True + null=True, + blank=True ) def __str__(self): @@ -144,7 +180,8 @@ class HourBuilding(models.Model): notes = models.TextField( max_length=140, - null=True + null=True, + blank=True ) class HourBuildingLeg(models.Model): @@ -180,6 +217,12 @@ class HourBuildingLeg(models.Model): default=False ) + def __str__(self): + if self.stop: + return "Refuelling Stop" + else: + return f"Flight Leg: {self.departure} -> {self.destination}" + class Training(models.Model): id = models.BigAutoField( primary_key=True @@ -229,5 +272,9 @@ class Training(models.Model): notes = models.TextField( max_length=140, - null=True + null=True, + blank=True ) + + def __str__(self): + return f"{self.mission}" diff --git a/techdb/poetry.lock b/techdb/poetry.lock index 4ff8dae..51d3a7f 100644 --- a/techdb/poetry.lock +++ b/techdb/poetry.lock @@ -35,16 +35,23 @@ argon2 = ["argon2-cffi (>=19.1.0)"] bcrypt = ["bcrypt"] [[package]] -name = "django-nested-inline" -version = "0.4.6" -description = "Recursive nesting of inline forms for Django Admin" +name = "django-nested-admin" +version = "4.1.1" +description = "Django admin classes that allow for nested inlines" optional = false -python-versions = "*" +python-versions = ">=3.6" files = [ - {file = "django-nested-inline-0.4.6.tar.gz", hash = "sha256:e57b55858d112364dfb112bbcdabb888e581d1677d31c1cac3bdcef6c890dc61"}, - {file = "django_nested_inline-0.4.6-py2.py3-none-any.whl", hash = "sha256:4fc6f0e78b3b5411b4bb7f180bb984831b88874bda48e49a14307baff5da5f12"}, + {file = "django-nested-admin-4.1.1.tar.gz", hash = "sha256:645d63b38d579b034a65e0983f1f8cbb8824c75b4232f8d62a1583fa7a9f568f"}, + {file = "django_nested_admin-4.1.1-py3-none-any.whl", hash = "sha256:7e72ab7a8ae4c91074f6f709ce38fdf54f9c5f78d19e68772e0f05b83090ec9f"}, ] +[package.dependencies] +python-monkey-business = ">=1.0.0" + +[package.extras] +dev = ["Pillow", "black", "dj-database-url", "django-selenosis", "flake8", "pytest", "pytest-cov", "pytest-django", "pytest-xdist", "selenium"] +test = ["Pillow", "dj-database-url", "django-selenosis", "pytest", "pytest-cov", "pytest-django", "pytest-xdist", "selenium"] + [[package]] name = "psycopg2-binary" version = "2.9.10" @@ -121,6 +128,17 @@ files = [ {file = "psycopg2_binary-2.9.10-cp39-cp39-win_amd64.whl", hash = "sha256:30e34c4e97964805f715206c7b789d54a78b70f3ff19fbe590104b71c45600e5"}, ] +[[package]] +name = "python-monkey-business" +version = "1.1.0" +description = "Utility functions for monkey-patching python code" +optional = false +python-versions = "*" +files = [ + {file = "python-monkey-business-1.1.0.tar.gz", hash = "sha256:8393839cc741415ed5ddc2bd58e2d4ce07f966a7d26b7aebff19dcec64818edc"}, + {file = "python_monkey_business-1.1.0-py2.py3-none-any.whl", hash = "sha256:15b4f603c749ba9a7b4f1acd36af023a6c5ba0f7e591c945f8253f0ef44bf389"}, +] + [[package]] name = "sqlparse" version = "0.5.1" @@ -150,4 +168,4 @@ files = [ [metadata] lock-version = "2.0" python-versions = "^3.12" -content-hash = "1ef37b219ab2fd51b8fa93268a04d3baea8cbaef61a470a6a01005f624510b21" +content-hash = "17d19b9fd275a26e661d0db57222d91dd542a7a2f8a9106d79fd3eec4980ffb5" diff --git a/techdb/pyproject.toml b/techdb/pyproject.toml index 2b3b6f3..c987647 100644 --- a/techdb/pyproject.toml +++ b/techdb/pyproject.toml @@ -9,7 +9,7 @@ readme = "README.md" python = "^3.12" django = "^5.1.2" psycopg2-binary = "^2.9.10" -django-nested-inline = "^0.4.6" +django-nested-admin = "^4.1.1" [build-system] diff --git a/techdb/techdb/settings.py b/techdb/techdb/settings.py index abb06c1..02df5d8 100644 --- a/techdb/techdb/settings.py +++ b/techdb/techdb/settings.py @@ -37,7 +37,7 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', - 'nested_inline', + 'nested_admin', 'catops', 'flightslot' ]