Exporter first version, merged cells broken
This commit is contained in:
@@ -1,13 +1,14 @@
|
||||
from django import forms
|
||||
from django.db.models.query import QuerySet
|
||||
from django.contrib import admin
|
||||
from django.http import HttpRequest, HttpResponse
|
||||
from django.contrib import admin
|
||||
from django.contrib import messages
|
||||
from django.utils.translation import ngettext
|
||||
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
|
||||
@@ -18,6 +19,8 @@ from .models.weekpref import WeekPreference
|
||||
from .custom.colortag import course_color
|
||||
from .custom.defpassword import default_password
|
||||
|
||||
from .actions.exportweek import export_selected
|
||||
|
||||
class TrainingForm(forms.ModelForm):
|
||||
model=Training
|
||||
|
||||
@@ -56,39 +59,24 @@ class TrainingInLIne(nested_admin.NestedTabularInline):
|
||||
max_num = 7
|
||||
|
||||
class WeekPreferenceAdmin(nested_admin.NestedModelAdmin):
|
||||
inlines = [TrainingInLIne, HourBuildingInLine]
|
||||
list_display = ("week", "student__name", "student__surname", "student__course", "course_color", "student_brief_mix")
|
||||
list_filter = ("week", "student__course", "student")
|
||||
actions = ("export_selected",)
|
||||
inlines = (TrainingInLIne, HourBuildingInLine,)
|
||||
list_display = ("week", "student__name", "student__surname", "student__course", "course_color", "student_brief_mix",)
|
||||
list_filter = ("week", "student__course", "student",)
|
||||
actions = ("export",)
|
||||
|
||||
@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
|
||||
|
||||
def export(weekpreferenceadmin, request: HttpRequest, queryset: QuerySet[WeekPreference]) -> HttpResponse | None:
|
||||
if queryset.count() == 0:
|
||||
return None
|
||||
weekpreferenceadmin.message_user(request, ngettext("Exporting %d row", "Exporting %d rows", queryset.count()) % queryset.count(), messages.SUCCESS)
|
||||
return export_selected(request=request, queryset=queryset)
|
||||
|
||||
@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:
|
||||
|
||||
Reference in New Issue
Block a user