From a1fb4c1f4972e3068de46d190b4b2f7bc5df51af Mon Sep 17 00:00:00 2001 From: yaemiku Date: Thu, 9 Dec 2021 01:26:37 +0100 Subject: [PATCH] excel file with students list --- .prettierignore | 1 + app/admin.py | 52 ++++++++++++++++++++++-- app/templates/admin/edition_buttons.html | 29 +++++++++---- requirements.txt | 3 +- 4 files changed, 72 insertions(+), 13 deletions(-) create mode 100644 .prettierignore diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..0b84df0 --- /dev/null +++ b/.prettierignore @@ -0,0 +1 @@ +*.html \ No newline at end of file diff --git a/app/admin.py b/app/admin.py index 3d7b631..297dbf3 100644 --- a/app/admin.py +++ b/app/admin.py @@ -1,10 +1,14 @@ from django.apps import apps from django.contrib import admin +from django.shortcuts import HttpResponse from django.contrib.admin.sites import AlreadyRegistered from django_summernote.admin import SummernoteModelAdmin from .models import Announcement, Edition, School, Student from django.db import transaction +from io import BytesIO +import xlsxwriter + class AnnouncementModelAdmin(SummernoteModelAdmin): summernote_fields = ['content'] @@ -15,7 +19,7 @@ class EditionModelAdmin(admin.ModelAdmin): change_form_template = 'admin/edition_buttons.html' def response_change(self, request, obj): - if "_update-student-scores" in request.POST: + if '_update-student-scores' in request.POST: file = obj.scores.open(mode='r') content = filter(lambda x: x != '', file.read().split('\n')) file.close() @@ -29,7 +33,7 @@ class EditionModelAdmin(admin.ModelAdmin): Student.objects.filter(identifier=row[0]).update(score_first=row[1], score_second=row[2]) - if "_update-disable-submissions" in request.POST and obj.active and obj.submissions: + if '_update-disable-submissions' in request.POST and obj.active and obj.submissions: if obj.active is True: schools = School.objects.all() @@ -40,7 +44,49 @@ class EditionModelAdmin(admin.ModelAdmin): obj.submissions = False obj.save() - return super().response_change(request, obj) + if '_generate-student-list' in request.POST: + year = obj.year + students = Student.objects.filter(identifier__startswith=year) + schools_set = {int(student.identifier.split('-')[1]) for student in students} + + output = BytesIO() + workbook = xlsxwriter.Workbook(output) + + worksheet = workbook.add_worksheet("Lista uczniów") + + worksheet.write(0, 0, 'Identyfikator') + worksheet.write(0, 1, 'Wynik - eliminacje') + worksheet.write(0, 2, 'Wynik - finał') + worksheet.write(0, 3, 'Imię') + worksheet.write(0, 4, 'Nazwisko') + worksheet.write(0, 5, 'Klasa') + worksheet.write(0, 6, 'Szkoła - nazwa') + worksheet.write(0, 7, 'Szkoła - miejscowość') + worksheet.write(0, 8, 'Szkoła - adres') + + row = 1 + for school_id in schools_set: + for student in sorted(Student.objects.filter(identifier__startswith=f'{year}-{school_id}-'), key=lambda x: x.identifier.split('-')[2]): + worksheet.write(row, 0, student.identifier) # Identyfikator + worksheet.write(row, 1, student.score_first) # Wynik - eliminacje + worksheet.write(row, 2, student.score_second) # Wynik - finał + worksheet.write(row, 3, student.name) # Imię + worksheet.write(row, 4, student.surname) # Nazwisko + worksheet.write(row, 5, student.grade) # Klasa + worksheet.write(row, 6, student.school_name) # Szkoła - nazwa + worksheet.write(row, 7, student.school_town) # Szkoła - miejscowość + worksheet.write(row, 8, student.school_address) # Szkoła - adres + + row += 1 + + workbook.close() + xlsx_data = output.getvalue() + + response = HttpResponse(content_type='application/vnd.ms-excel') + response['Content-Disposition'] = 'attachment; filename=Uczniowie.xlsx' + response.write(xlsx_data) + + return response class StudentModelAdmin(admin.ModelAdmin): list_display = ('identifier', '__str__', 'grade') diff --git a/app/templates/admin/edition_buttons.html b/app/templates/admin/edition_buttons.html index 650679f..9bcd670 100644 --- a/app/templates/admin/edition_buttons.html +++ b/app/templates/admin/edition_buttons.html @@ -1,9 +1,20 @@ -{% extends 'admin/change_form.html' %} - -{% block submit_buttons_bottom %} - {{ block.super }} -
- - -
-{% endblock %} \ No newline at end of file +{% extends 'admin/change_form.html' %} {% block submit_buttons_bottom %} +{{ block.super }} +
+ + + +
+{% endblock %} diff --git a/requirements.txt b/requirements.txt index 74eeacf..61d1240 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,8 @@ -Django +Django==3.2.5 django-cleanup django-crispy-forms django-summernote django-crispy-bulma psycopg2-binary python-dotenv +XlsxWriter \ No newline at end of file