62 lines
2.2 KiB
Python
62 lines
2.2 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 HourBuildingLegForm(forms.ModelForm):
|
|
class Meta:
|
|
model = HourBuildingLegFlight
|
|
fields = '__all__'
|
|
widgets = {
|
|
'time': TimeDurationWidget(show_days=False,
|
|
show_seconds=False)
|
|
}
|
|
|
|
# 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 = HourBuildingLegForm
|
|
class HourBuildingLegStopInLine(nested_admin.NestedStackedPolymorphicInline.Child):
|
|
model = HourBuildingLegStop
|
|
|
|
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
|
|
extra = 1
|
|
inlines = (HourBuildingLegBaseInLine,)
|
|
fk_name = 'weekpref'
|
|
verbose_name_plural = "Hour Buildings"
|
|
max_num = 7
|
|
formfield_overrides = {
|
|
models.CharField: {'widget': TextInput(attrs={'size':'20'})},
|
|
models.TextField: {'widget': Textarea(attrs={'rows':4, 'cols':35})},
|
|
}
|
|
|