polymorphic #1

Merged
Obbart merged 6 commits from polymorphic into flightslot 2025-11-24 12:22:02 +01:00
3 changed files with 28 additions and 23 deletions
Showing only changes of commit 7ad09e21b7 - Show all commits

View File

@@ -12,13 +12,32 @@ from ..models.weekpref import WeekPreference
from datetime import date from datetime import date
class HourBuildingLegForm(forms.ModelForm): class HourBuildingLegFlightForm(forms.ModelForm):
class Meta: class Meta:
model = HourBuildingLegFlight model = HourBuildingLegFlight
fields = "__all__" fields = "__all__"
widgets = { widgets = {
"time": TimeDurationWidget(show_days=False, "time": TimeDurationWidget(show_days=False,
show_seconds=False) show_seconds=False,
attrs={
"style": (
"margin-right:5px; margin-left:5px; width:40px; min:0; max:5"
)
})
}
class HourBuildingLegStopForm(forms.ModelForm):
class Meta:
model = HourBuildingLegStop
fields = "__all__"
widgets = {
"time": TimeDurationWidget(show_days=False,
show_seconds=False,
attrs={
"style": (
"margin-right:5px; margin-left:5px; width:40px;"
)
})
} }
# Register your models here. # Register your models here.
@@ -27,35 +46,19 @@ class HourBuildingLegBaseInLine(nested_admin.NestedStackedPolymorphicInline):
fk_name = "hb" fk_name = "hb"
verbose_name_plural = "Hour Building Legs" verbose_name_plural = "Hour Building Legs"
def get_inline_title(self, obj):
return "PIPPO"
class HourBuildingLegFlightInLine(nested_admin.NestedStackedPolymorphicInline.Child): class HourBuildingLegFlightInLine(nested_admin.NestedStackedPolymorphicInline.Child):
model = HourBuildingLegFlight model = HourBuildingLegFlight
form = HourBuildingLegForm form = HourBuildingLegFlightForm
fk_name = "hourbuildinglegbase_ptr" fk_name = "hourbuildinglegbase_ptr"
fields = ("departure", "time", "destination", "pax", ) fields = ("departure", "time", "destination", "pax", )
hide_title = True 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): class HourBuildingLegStopInLine(nested_admin.NestedStackedPolymorphicInline.Child):
model = HourBuildingLegStop model = HourBuildingLegStop
form = HourBuildingLegFlightForm
fk_name = "hourbuildinglegbase_ptr" fk_name = "hourbuildinglegbase_ptr"
fields = ("time", "refuel", ) 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, ) child_inlines = (HourBuildingLegFlightInLine, HourBuildingLegStopInLine, )
# If user is a student deny edit permission for week past the current one # If user is a student deny edit permission for week past the current one

View File

@@ -101,6 +101,7 @@ class HourBuildingLegFlight(HourBuildingLegBase):
verbose_name="Pax (optional)" verbose_name="Pax (optional)"
) )
# Change displayed name in the inline form
class Meta(PolymorphicModel.Meta): class Meta(PolymorphicModel.Meta):
verbose_name = "Flight leg" verbose_name = "Flight leg"
verbose_name_plural = "Flight legs" verbose_name_plural = "Flight legs"
@@ -119,6 +120,7 @@ class HourBuildingLegStop(HourBuildingLegBase):
default=False default=False
) )
# Change displayed name in the inline form
class Meta(PolymorphicModel.Meta): class Meta(PolymorphicModel.Meta):
verbose_name = "Stop" verbose_name = "Stop"
verbose_name_plural = "Stops" verbose_name_plural = "Stops"

View File

@@ -54,15 +54,15 @@ class Student(models.Model):
creating: bool = self.pk is None creating: bool = self.pk is None
super().save(*args, **kwargs) super().save(*args, **kwargs)
if creating and not self.user: if creating and not self.user:
username = f"{self.name.lower()}.{self.surname.lower()}" username: str = f"{self.name.lower()}.{self.surname.lower()}"
# Avoid username conflict with progressive number # Avoid username conflict with progressive number
base_username = username base_username = username
counter = 1 counter: int = 1
while User.objects.filter(username=username).exists(): while User.objects.filter(username=username).exists():
username = f"{base_username}{counter}" username = f"{base_username}{counter}"
counter += 1 counter += 1
# Create user # Create user
user = User.objects.create_user( user: User = User.objects.create_user(
first_name=self.name, first_name=self.name,
last_name=self.surname, last_name=self.surname,
username=username, username=username,