Show Hour building only at ATPL or DL (volo) students

This commit is contained in:
2025-12-05 17:58:51 +01:00
parent 84cf41535c
commit aeb3aa30ce
6 changed files with 42 additions and 6 deletions

View File

@@ -2,8 +2,8 @@ import nested_admin
from django import forms from django import forms
from django.db import models from django.db import models
from django.forms import TextInput, Textarea
from django.http import HttpRequest from django.http import HttpRequest
from django.forms import TextInput, Textarea
from durationwidget.widgets import TimeDurationWidget from durationwidget.widgets import TimeDurationWidget

View File

@@ -45,6 +45,8 @@ class TrainingInLIne(nested_admin.NestedTabularInline):
Q(mtype=MissionTypes.CPL) | \ Q(mtype=MissionTypes.CPL) | \
Q(mtype=MissionTypes.CHK) Q(mtype=MissionTypes.CHK)
kwargs["queryset"] = MissionProfile.objects.filter(q).order_by("id") kwargs["queryset"] = MissionProfile.objects.filter(q).order_by("id")
case CourseTypes.FI:
kwargs["queryset"] = MissionProfile.objects.filter(mtype=MissionTypes.FI).order_by("mnum")
case _: case _:
pass pass
return super().formfield_for_foreignkey(db_field, request, **kwargs) return super().formfield_for_foreignkey(db_field, request, **kwargs)

View File

@@ -7,6 +7,7 @@ from django.contrib import admin, messages
from django.utils.translation import ngettext from django.utils.translation import ngettext
from django.utils.safestring import SafeText from django.utils.safestring import SafeText
from ..models.courses import CourseTypes
from ..models.students import Student from ..models.students import Student
from ..models.missions import Training from ..models.missions import Training
from ..models.weekpref import WeekPreference from ..models.weekpref import WeekPreference
@@ -22,11 +23,10 @@ from datetime import date
from typing import Dict, List, Any from typing import Dict, List, Any
class WeekPreferenceAdmin(nested_admin.NestedPolymorphicModelAdmin): class WeekPreferenceAdmin(nested_admin.NestedPolymorphicModelAdmin):
inlines = (TrainingInLIne, HourBuildingInLine, ) list_display = ("week", "student__surname", "student__name", "student__course", "course_color", "student_brief_mix", )
list_display = ("week", "student__surname","student__name", "student__course", "course_color", "student_brief_mix",) list_filter = ("week", "student__course", )
list_filter = ("week", "student__course",) search_fields = ("student__surname","student__name", )
search_fields = ("student__surname","student__name",) actions = ("export", )
actions = ("export",)
@admin.action(description="Export Selected Preferences") @admin.action(description="Export Selected Preferences")
def export(self, request: HttpRequest, queryset: QuerySet[WeekPreference]) -> HttpResponse | None: def export(self, request: HttpRequest, queryset: QuerySet[WeekPreference]) -> HttpResponse | None:
@@ -54,6 +54,19 @@ class WeekPreferenceAdmin(nested_admin.NestedPolymorphicModelAdmin):
return [] return []
return list_filter return list_filter
# Get available mission or HB depending on student course
def get_inline_instances(self, request: HttpRequest, obj: WeekPreference | None = None):
if hasattr(request.user, "student"):
student: Student = request.user.student
# Only ATPL students are able to book HourBuilding Missions
if student.course and student.course.ctype in (CourseTypes.ATPL, CourseTypes.DISTANCE):
return (
TrainingInLIne(self.model, self.admin_site),
HourBuildingInLine(self.model, self.admin_site),
)
# All other courses have only training
return (TrainingInLIne(self.model, self.admin_site), )
# If a user is registered as student do not show actions # If a user is registered as student do not show actions
def get_actions(self, request: HttpRequest) -> Dict[str, Any]: def get_actions(self, request: HttpRequest) -> Dict[str, Any]:
actions = super().get_actions(request) actions = super().get_actions(request)

View File

@@ -0,0 +1,18 @@
# Generated by Django 5.2.8 on 2025-12-05 16:57
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('flightslot', '0028_alter_availability_options'),
]
operations = [
migrations.AlterField(
model_name='course',
name='ctype',
field=models.CharField(choices=[('FI', 'FI'), ('PPL', 'PPL'), ('ATPL', 'ATPL'), ('DL', 'DISTANCE'), ('DL_VOLO', 'DISTANCE_VOLO'), ('OTHER', 'OTHER')], verbose_name='Course Type'),
),
]

View File

@@ -8,6 +8,7 @@ class CourseTypes(models.TextChoices):
PPL = "PPL", _("PPL") PPL = "PPL", _("PPL")
ATPL = "ATPL", _("ATPL") ATPL = "ATPL", _("ATPL")
DISTANCE = "DL", _("DISTANCE") DISTANCE = "DL", _("DISTANCE")
DISTANCE_VOLO = "DL_VOLO", _("DISTANCE_VOLO")
OTHER = "OTHER",_("OTHER") OTHER = "OTHER",_("OTHER")
class Course(models.Model): class Course(models.Model):

View File

@@ -117,6 +117,8 @@ class HourBuildingLegFlight(HourBuildingLegBase):
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
self.departure = self.departure.upper().strip() self.departure = self.departure.upper().strip()
self.destination = self.destination.upper().strip() self.destination = self.destination.upper().strip()
if self.pax:
self.pax = " ".join(c.capitalize() for c in self.pax.split())
super().save(*args, **kwargs) super().save(*args, **kwargs)
def __str__(self): def __str__(self):