Add list filter and export options to assignment list

This commit is contained in:
Ramon Wenger 2021-10-18 13:58:51 +02:00
parent 19f44483f1
commit a79a8f9908
1 changed files with 36 additions and 0 deletions

View File

@ -1,6 +1,9 @@
import csv
from django.contrib import admin
# Register your models here.
from django.http import HttpResponse
from django.urls import reverse
from django.utils.html import format_html
@ -17,15 +20,48 @@ class StudentSubmissionInline(admin.TabularInline):
def link(self, obj):
return format_html('<a href={}>{}</a>'.format(reverse('admin:assignments_studentsubmission_change', args=(obj.id,)), obj.text))
@admin.register(Assignment)
class AssignmentAdmin(admin.ModelAdmin):
list_display = ('title', 'module', 'deleted', 'owner', )
autocomplete_fields = ('owner',)
actions = ['export_assignments_to_csv', 'export_submissions_to_csv']
list_filter = ('owner', 'module', 'user_created')
search_fields = ('id', 'title')
inlines = [
StudentSubmissionInline
]
def export_assignments_to_csv(self, request, queryset):
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment;filename=assignment-export.csv'
writer = csv.writer(response)
field_names = ['ID', 'Titel', 'Auftragstext', 'Modul']
writer.writerow(field_names)
for assignment in queryset.all():
writer.writerow([assignment.id, assignment.title, assignment.assignment, assignment.module])
return response
export_assignments_to_csv.short_description = 'Auftragstexte exportieren'
def export_submissions_to_csv(self, request, queryset):
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment;filename=assignment-submission-export.csv'
writer = csv.writer(response)
field_names = ['Assignment-ID', 'Text', 'Mit Lehrer geteilt',]
writer.writerow(field_names)
for assignment in queryset.all():
for submission in assignment.submissions.filter(final=True):
writer.writerow([submission.assignment.id, submission.text, submission.final])
return response
export_submissions_to_csv.short_description = 'Lösungstexte exportieren'
@admin.register(StudentSubmission)
class StudentSubmissionAdmin(admin.ModelAdmin):