Demo export action and added color badges where needed
This commit is contained in:
@@ -1,8 +1,13 @@
|
||||
from django import forms
|
||||
from django.db.models.query import QuerySet
|
||||
from django.contrib import admin
|
||||
from django.http import HttpRequest, HttpResponse
|
||||
from django.utils.safestring import SafeText
|
||||
from durationwidget.widgets import TimeDurationWidget
|
||||
from datetime import date
|
||||
|
||||
import nested_admin
|
||||
import csv
|
||||
|
||||
from .models.courses import Course
|
||||
from .models.hourbuildings import HourBuilding, HourBuildingLeg
|
||||
@@ -10,6 +15,9 @@ from .models.missions import Training, MissionProfile
|
||||
from .models.students import Student
|
||||
from .models.weekpref import WeekPreference
|
||||
|
||||
from .custom.colortag import course_color
|
||||
from .custom.defpassword import default_password
|
||||
|
||||
class TrainingForm(forms.ModelForm):
|
||||
model=Training
|
||||
|
||||
@@ -49,7 +57,43 @@ class TrainingInLIne(nested_admin.NestedTabularInline):
|
||||
|
||||
class WeekPreferenceAdmin(nested_admin.NestedModelAdmin):
|
||||
inlines = [TrainingInLIne, HourBuildingInLine]
|
||||
list_filter = ["week", "student__course", "student"]
|
||||
list_display = ("week", "student__name", "student__surname", "student__course", "course_color", "student_brief_mix")
|
||||
list_filter = ("week", "student__course", "student")
|
||||
actions = ("export_selected",)
|
||||
|
||||
@admin.action(description="Export Selected Preferences")
|
||||
def export_selected(modeladmin, request: HttpRequest, queryset: QuerySet[WeekPreference]) -> HttpResponse:
|
||||
filename = "weekpreferences_export.csv"
|
||||
response = HttpResponse(content_type='text/csv')
|
||||
response['Content-Disposition'] = f'attachment; filename="{filename}"'
|
||||
|
||||
writer = csv.writer(response)
|
||||
# intestazione — scegli i campi che vuoi esportare
|
||||
writer.writerow(['student_name', 'student_surname', 'course', 'course_color', "training", "hourbuilding"])
|
||||
|
||||
for q in queryset:
|
||||
writer.writerow([
|
||||
q.student.name,
|
||||
q.student.surname,
|
||||
q.student.course,
|
||||
q.student.course.color,
|
||||
"+".join([f"{t.mission.mtype}-{t.mission.mnum}" for t in Training.objects.filter(weekpref = q.id)]),
|
||||
"+".join([f"HB_{t.aircraft}" for t in HourBuilding.objects.filter(weekpref = q.id)]),
|
||||
])
|
||||
return response
|
||||
|
||||
@admin.display(description="Mission Count")
|
||||
def student_brief_mix(self, obj: WeekPreference) -> SafeText:
|
||||
if not obj.student.course:
|
||||
return SafeText("")
|
||||
return SafeText(f"{Training.objects.filter(weekpref = obj.id).count()}")
|
||||
|
||||
|
||||
@admin.display(description="Color")
|
||||
def course_color(self, obj: WeekPreference) -> SafeText:
|
||||
if not obj.student.course:
|
||||
return SafeText("")
|
||||
return course_color(obj.student.course.color)
|
||||
|
||||
def has_module_permission(self, request):
|
||||
if hasattr(request.user, 'student'):
|
||||
@@ -73,13 +117,13 @@ class WeekPreferenceAdmin(nested_admin.NestedModelAdmin):
|
||||
if 'student' in form.base_fields:
|
||||
form.base_fields['student'].initial = student
|
||||
form.base_fields['student'].disabled = True
|
||||
form.base_fields['week'].disabled = True
|
||||
|
||||
# If form contains the week field
|
||||
if 'week' in form.base_fields:
|
||||
# Set default value as current week
|
||||
current_week = date.today().isocalendar().week
|
||||
form.base_fields['week'].initial = current_week
|
||||
form.base_fields['week'].disabled = True
|
||||
return form
|
||||
|
||||
def save_model(self, request, obj, form, change):
|
||||
@@ -89,22 +133,41 @@ class WeekPreferenceAdmin(nested_admin.NestedModelAdmin):
|
||||
super().save_model(request, obj, form, change)
|
||||
|
||||
class StudentAdmin(admin.ModelAdmin):
|
||||
list_display = ("surname", "name", "course", "email","active")
|
||||
list_filter = ["course", "active"]
|
||||
list_display = ("surname", "name", "course", "course_color", "email", "phone", "password", "active")
|
||||
list_filter = ("course", "active")
|
||||
actions = ("disable_students",)
|
||||
|
||||
class CourseAdminForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = Course
|
||||
@admin.display(description="Color")
|
||||
def course_color(self, obj: Student) -> SafeText:
|
||||
if not obj.course:
|
||||
return SafeText("")
|
||||
return course_color(obj.course.color)
|
||||
|
||||
@admin.display(description="Password")
|
||||
def password(self, obj: Student) -> SafeText:
|
||||
return SafeText(default_password(student=obj))
|
||||
|
||||
@admin.action(description="Disable Students")
|
||||
def disable_students(modeladmin, request: HttpRequest, queryset: QuerySet[Student]):
|
||||
queryset.update(active = False)
|
||||
pass
|
||||
|
||||
class CourseAdmin(admin.ModelAdmin):
|
||||
list_display = ["ctype", "cnumber", "year", "color"]
|
||||
list_filter = ["ctype", "year"]
|
||||
form=CourseAdminForm
|
||||
list_display = ("ctype", "cnumber","color_display", "year")
|
||||
list_filter = ("ctype", "year")
|
||||
|
||||
# Dinamically add color_display property to show a colored dot
|
||||
@admin.display(description="Color")
|
||||
def color_display(self, obj: Course) -> SafeText:
|
||||
if not obj.pk:
|
||||
return SafeText("")
|
||||
return course_color(obj.color)
|
||||
|
||||
class MissionProfileAdmin(admin.ModelAdmin):
|
||||
list_display = ("mtype", "mnum")
|
||||
list_display = ("mtype", "mnum",)
|
||||
list_filter = ("mtype",)
|
||||
|
||||
admin.site.register(Course, CourseAdmin)
|
||||
admin.site.register(MissionProfile)
|
||||
admin.site.register(MissionProfile, MissionProfileAdmin)
|
||||
admin.site.register(Student, StudentAdmin)
|
||||
admin.site.register(WeekPreference, WeekPreferenceAdmin)
|
||||
|
||||
Reference in New Issue
Block a user