88 lines
3.0 KiB
Python
88 lines
3.0 KiB
Python
import nested_admin
|
|
|
|
from django import forms
|
|
from django.db import models
|
|
from django.forms import TextInput, Textarea
|
|
from django.http import HttpRequest
|
|
|
|
from durationwidget.widgets import TimeDurationWidget
|
|
|
|
from ..models.hourbuildings import HourBuilding, HourBuildingLegBase, HourBuildingLegFlight, HourBuildingLegStop
|
|
from ..models.weekpref import WeekPreference
|
|
|
|
from datetime import date
|
|
|
|
class HourBuildingLegFlightForm(forms.ModelForm):
|
|
class Meta:
|
|
model = HourBuildingLegFlight
|
|
fields = "__all__"
|
|
widgets = {
|
|
"time": TimeDurationWidget(show_days=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.
|
|
class HourBuildingLegBaseInLine(nested_admin.NestedStackedPolymorphicInline):
|
|
model = HourBuildingLegBase
|
|
fk_name = "hb"
|
|
verbose_name_plural = "Hour Building Legs"
|
|
|
|
class HourBuildingLegFlightInLine(nested_admin.NestedStackedPolymorphicInline.Child):
|
|
model = HourBuildingLegFlight
|
|
form = HourBuildingLegFlightForm
|
|
fk_name = "hourbuildinglegbase_ptr"
|
|
fields = ("departure", "time", "destination", "pax", )
|
|
hide_title = True
|
|
|
|
class HourBuildingLegStopInLine(nested_admin.NestedStackedPolymorphicInline.Child):
|
|
model = HourBuildingLegStop
|
|
form = HourBuildingLegFlightForm
|
|
fk_name = "hourbuildinglegbase_ptr"
|
|
fields = ("time", "refuel", )
|
|
|
|
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:
|
|
current_week = date.today().isocalendar().week
|
|
if not obj.DoesNotExist and current_week > obj.weekpref.week:
|
|
return False
|
|
return True
|
|
|
|
def has_delete_permission(self, request: HttpRequest, obj: HourBuilding | None = None):
|
|
return self.has_change_permission(request=request, obj=obj)
|
|
|
|
|
|
class HourBuildingInLine(nested_admin.NestedTabularInline):
|
|
model = HourBuilding
|
|
inlines = (HourBuildingLegBaseInLine,)
|
|
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})},
|
|
}
|
|
|