excel file with students list

This commit is contained in:
Nikola Kubiczek 2021-12-09 01:26:37 +01:00
parent 173f3da52a
commit a1fb4c1f49
4 changed files with 72 additions and 13 deletions

1
.prettierignore Normal file
View File

@ -0,0 +1 @@
*.html

View File

@ -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')

View File

@ -1,9 +1,20 @@
{% extends 'admin/change_form.html' %}
{% block submit_buttons_bottom %}
{{ block.super }}
<div class="submit-row">
<input type="submit" value="Zaktualizuj wyniki uczniów" name="_update-student-scores">
<input type="submit" value="Wyłącz zgłoszenia" name="_update-disable-submissions">
</div>
{% endblock %}
{% extends 'admin/change_form.html' %} {% block submit_buttons_bottom %}
{{ block.super }}
<div class="submit-row">
<input
type="submit"
value="Zaktualizuj wyniki uczniów"
name="_update-student-scores"
/>
<input
type="submit"
value="Wygeneruj listę uczniów"
name="_generate-student-list"
/>
<input
type="submit"
value="Wyłącz zgłoszenia"
name="_update-disable-submissions"
/>
</div>
{% endblock %}

View File

@ -1,7 +1,8 @@
Django
Django==3.2.5
django-cleanup
django-crispy-forms
django-summernote
django-crispy-bulma
psycopg2-binary
python-dotenv
XlsxWriter