From 48ff1d799c7a7417610500454f62b819a5e79851 Mon Sep 17 00:00:00 2001 From: Emanuele Date: Fri, 21 Nov 2025 17:49:51 +0100 Subject: [PATCH] Polymorphic model correctly saves, fixed formatting of admin --- cntmanage/cntmanage/urls.py | 4 -- cntmanage/flightslot/admin.py | 4 ++ .../flightslot/admins/hourbuilding_adm.py | 41 +++++++++++++++---- ...e_hourbuildinglegstop_location_and_more.py | 37 +++++++++++++++++ cntmanage/flightslot/models/hourbuildings.py | 39 ++++++++++++------ cntmanage/flightslot/models/students.py | 4 +- cntmanage/poetry.lock | 6 +-- 7 files changed, 105 insertions(+), 30 deletions(-) create mode 100644 cntmanage/flightslot/migrations/0019_remove_hourbuildinglegstop_location_and_more.py diff --git a/cntmanage/cntmanage/urls.py b/cntmanage/cntmanage/urls.py index 167fabb..c485b3c 100644 --- a/cntmanage/cntmanage/urls.py +++ b/cntmanage/cntmanage/urls.py @@ -9,7 +9,3 @@ urlpatterns = [ path('user/', flightslot_user.urls), path("", lambda r: redirect("/user/")), # la root porta gli utenti nella pagina giusta ] - -admin.site.site_header = "Flight Scheduler 🛫" -admin.site.site_title = "Flight Scheduler 🛫" -admin.site.index_title = "Welcome to CantorAir Flight Scheduler Portal" diff --git a/cntmanage/flightslot/admin.py b/cntmanage/flightslot/admin.py index a81539a..66444bb 100644 --- a/cntmanage/flightslot/admin.py +++ b/cntmanage/flightslot/admin.py @@ -32,6 +32,10 @@ flightslot_user = FlightSlotUserSite(name="user_site") # registra SOLO i modelli autorizzati flightslot_user.register(WeekPreference, WeekPreferenceAdmin) +admin.site.site_header = "Flight Scheduler Admin 🛫" +admin.site.site_title = "Flight Scheduler Admin 🛫" +admin.site.index_title = "Welcome to CantorAir Flight Scheduler Administrator Portal" + admin.site.register(Course, CourseAdmin) admin.site.register(MissionProfile, MissionProfileAdmin) admin.site.register(Student, StudentAdmin) diff --git a/cntmanage/flightslot/admins/hourbuilding_adm.py b/cntmanage/flightslot/admins/hourbuilding_adm.py index 186ebf6..7c6f07c 100644 --- a/cntmanage/flightslot/admins/hourbuilding_adm.py +++ b/cntmanage/flightslot/admins/hourbuilding_adm.py @@ -15,29 +15,52 @@ from datetime import date class HourBuildingLegForm(forms.ModelForm): class Meta: model = HourBuildingLegFlight - fields = '__all__' + fields = "__all__" widgets = { - 'time': TimeDurationWidget(show_days=False, + "time": TimeDurationWidget(show_days=False, show_seconds=False) } # Register your models here. class HourBuildingLegBaseInLine(nested_admin.NestedStackedPolymorphicInline): model = HourBuildingLegBase - fk_name = 'hb' + fk_name = "hb" verbose_name_plural = "Hour Building Legs" + + def get_inline_title(self, obj): + return "PIPPO" class HourBuildingLegFlightInLine(nested_admin.NestedStackedPolymorphicInline.Child): model = HourBuildingLegFlight form = HourBuildingLegForm + fk_name = "hourbuildinglegbase_ptr" + fields = ("departure", "time", "destination", "pax", ) + hide_title = True + + def get_inline_title(self, obj: HourBuildingLegFlight | None = None) -> str: + if obj: + return f"Flight Leg:" + else: + return f"New Flight Leg" + class HourBuildingLegStopInLine(nested_admin.NestedStackedPolymorphicInline.Child): model = HourBuildingLegStop + fk_name = "hourbuildinglegbase_ptr" + fields = ("time", "refuel", ) + + hide_title = True + + def get_inline_title(self, obj: HourBuildingLegStop | None = None) -> str: + if obj: + return f"Stop at:" + else: + return f"New Stop" child_inlines = (HourBuildingLegFlightInLine, HourBuildingLegStopInLine, ) # If user is a student deny edit permission for week past the current one def has_change_permission(self, request: HttpRequest, obj: HourBuilding | None = None): - if hasattr(request.user, 'student') and obj: + if hasattr(request.user, "student") and obj: current_week = date.today().isocalendar().week if not obj.DoesNotExist and current_week > obj.weekpref.week: return False @@ -49,13 +72,13 @@ class HourBuildingLegBaseInLine(nested_admin.NestedStackedPolymorphicInline): class HourBuildingInLine(nested_admin.NestedTabularInline): model = HourBuilding - extra = 1 inlines = (HourBuildingLegBaseInLine,) - fk_name = 'weekpref' - verbose_name_plural = "Hour Buildings" + extra = 0 max_num = 7 + fk_name = "weekpref" + verbose_name_plural = "Hour Buildings" formfield_overrides = { - models.CharField: {'widget': TextInput(attrs={'size':'20'})}, - models.TextField: {'widget': Textarea(attrs={'rows':4, 'cols':35})}, + models.CharField: {"widget": TextInput(attrs={"size":"20"})}, + models.TextField: {"widget": Textarea(attrs={"rows":4, "cols":35})}, } diff --git a/cntmanage/flightslot/migrations/0019_remove_hourbuildinglegstop_location_and_more.py b/cntmanage/flightslot/migrations/0019_remove_hourbuildinglegstop_location_and_more.py new file mode 100644 index 0000000..05d642b --- /dev/null +++ b/cntmanage/flightslot/migrations/0019_remove_hourbuildinglegstop_location_and_more.py @@ -0,0 +1,37 @@ +# Generated by Django 5.2.8 on 2025-11-21 16:27 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('flightslot', '0018_hourbuildinglegbase_hourbuildinglegflight_and_more'), + ] + + operations = [ + migrations.RemoveField( + model_name='hourbuildinglegstop', + name='location', + ), + migrations.AddField( + model_name='hourbuildinglegflight', + name='pax', + field=models.CharField(max_length=16, null=True), + ), + migrations.AlterField( + model_name='hourbuildinglegbase', + name='time', + field=models.DurationField(), + ), + migrations.AlterField( + model_name='hourbuildinglegflight', + name='departure', + field=models.CharField(max_length=4), + ), + migrations.AlterField( + model_name='hourbuildinglegflight', + name='destination', + field=models.CharField(max_length=4), + ), + ] diff --git a/cntmanage/flightslot/models/hourbuildings.py b/cntmanage/flightslot/models/hourbuildings.py index 608f0f1..2259c68 100644 --- a/cntmanage/flightslot/models/hourbuildings.py +++ b/cntmanage/flightslot/models/hourbuildings.py @@ -78,36 +78,51 @@ class HourBuildingLegBase(PolymorphicModel): ) time = models.DurationField( - null=False, - default = timedelta(hours=1) + null=False ) class HourBuildingLegFlight(HourBuildingLegBase): departure = models.CharField( null=False, blank=False, - default="LILV", max_length=4 ) destination = models.CharField( null=False, blank=False, - default="LILV", max_length=4 ) + pax = models.CharField( + null=True, + blank=True, + max_length=16, + verbose_name="Pax (optional)" + ) + + class Meta(PolymorphicModel.Meta): + verbose_name = "Flight leg" + verbose_name_plural = "Flight legs" + def __str__(self): - return f"Flight Leg: {self.departure} -> {self.destination}" + return f"{self.departure} -> {self.destination}" + + def save(self, *args, **kwargs): + self.departure = self.departure.capitalize().strip() + self.destination = self.destination.capitalize().strip() + super().save(*args, **kwargs) class HourBuildingLegStop(HourBuildingLegBase): - location = models.CharField( - null=False, - blank=False, - default="LILV", - max_length=4 - ) refuel = models.BooleanField( default=False - ) \ No newline at end of file + ) + + class Meta(PolymorphicModel.Meta): + verbose_name = "Stop" + verbose_name_plural = "Stops" + + def __str__(self): + return f"Refuel" if self.refuel else f"No Refuel" + \ No newline at end of file diff --git a/cntmanage/flightslot/models/students.py b/cntmanage/flightslot/models/students.py index 973e9d7..5bcd21d 100644 --- a/cntmanage/flightslot/models/students.py +++ b/cntmanage/flightslot/models/students.py @@ -51,7 +51,7 @@ class Student(models.Model): # Override save method to add user for login upon Student creation def save(self, *args, **kwargs): - creating = self.pk is None + creating: bool = self.pk is None super().save(*args, **kwargs) if creating and not self.user: username = f"{self.name.lower()}.{self.surname.lower()}" @@ -68,7 +68,7 @@ class Student(models.Model): username=username, email=self.email, password=self.default_password(), - is_staff=True + is_staff=True # allows access to admin page ) student_group, _ = Group.objects.get_or_create(name="StudentGroup") diff --git a/cntmanage/poetry.lock b/cntmanage/poetry.lock index da7f0b3..b0c1f3c 100644 --- a/cntmanage/poetry.lock +++ b/cntmanage/poetry.lock @@ -2,14 +2,14 @@ [[package]] name = "asgiref" -version = "3.10.0" +version = "3.11.0" description = "ASGI specs, helper code, and adapters" optional = false python-versions = ">=3.9" groups = ["main"] files = [ - {file = "asgiref-3.10.0-py3-none-any.whl", hash = "sha256:aef8a81283a34d0ab31630c9b7dfe70c812c95eba78171367ca8745e88124734"}, - {file = "asgiref-3.10.0.tar.gz", hash = "sha256:d89f2d8cd8b56dada7d52fa7dc8075baa08fb836560710d38c292a7a3f78c04e"}, + {file = "asgiref-3.11.0-py3-none-any.whl", hash = "sha256:1db9021efadb0d9512ce8ffaf72fcef601c7b73a8807a1bb2ef143dc6b14846d"}, + {file = "asgiref-3.11.0.tar.gz", hash = "sha256:13acff32519542a1736223fb79a715acdebe24286d98e8b164a73085f40da2c4"}, ] [package.extras]