Polymorphic model correctly saves, fixed formatting of admin
This commit is contained in:
@@ -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})},
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user