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 from assignments.models import Assignment, StudentSubmission, SubmissionFeedback class StudentSubmissionInline(admin.TabularInline): model = StudentSubmission readonly_fields = ('link', 'document', 'student', 'final',) exclude = ('text',) extra = 0 def link(self, obj): return format_html('{}'.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): pass @admin.register(SubmissionFeedback) class SubmissionFeedbackAdmin(admin.ModelAdmin): pass