Export hlasování do CSV
This commit is contained in:
parent
fbd75d2f72
commit
7ca7093371
2 changed files with 52 additions and 1 deletions
|
@ -17,6 +17,11 @@ urlpatterns = [
|
|||
org_required(views.SeznamExportView),
|
||||
name='seznam-export'
|
||||
),
|
||||
path(
|
||||
'prednasky/seznam_prednasek/<int:seznam>/hlasovani.csv',
|
||||
org_required(views.PrednaskyExportView),
|
||||
name='seznam-export-csv'
|
||||
),
|
||||
path(
|
||||
'prednasky/seznam_prednasek/<int:seznam>/',
|
||||
org_required(views.SeznamListView.as_view()),
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
import csv
|
||||
import http
|
||||
import logging
|
||||
|
||||
from django.http import HttpResponse
|
||||
from django.shortcuts import render, get_object_or_404
|
||||
from django.views import generic
|
||||
from django.shortcuts import HttpResponseRedirect
|
||||
|
@ -31,7 +33,7 @@ def newPrednaska(request):
|
|||
}, status=http.HTTPStatus.NOT_FOUND)
|
||||
|
||||
osoba = Osoba.objects.filter(user=request.user).first()
|
||||
ucastnik = osoba.plne_jmeno() + ' ' + str(osoba.id)
|
||||
ucastnik = osoba.plne_jmeno() + ' ' + str(osoba.id) # id, kvůli kolizi jmen
|
||||
|
||||
if request.method == 'POST':
|
||||
form_set_prednasky = HlasovaniPrednaskaFormSet(request.POST, prefix=PREDNASKY_PREFIX)
|
||||
|
@ -170,3 +172,47 @@ def SeznamExportView(request, seznam):
|
|||
)
|
||||
|
||||
|
||||
def PrednaskyExportView(request, seznam: int, **kwargs):
|
||||
hlasovani = Hlasovani.objects.filter(seznam=seznam).select_related("prednaska")
|
||||
hlasovani_o_znalostech = HlasovaniOZnalostech.objects.filter(seznam=seznam).select_related('ucastnik', 'znalost')
|
||||
|
||||
prednasky = list(Prednaska.objects.filter(seznamy=seznam))
|
||||
znalosti = list(Znalost.objects.filter(seznamy=seznam))
|
||||
|
||||
prednasky_map: dict[int, int] = {p.id: i for i, p in enumerate(prednasky, 1)}
|
||||
offset = len(prednasky_map)
|
||||
znalosti_map: dict[int, int] = {z.id: i for i, z in enumerate(znalosti, offset + 1)}
|
||||
width = offset + len(znalosti_map)
|
||||
|
||||
table: [str, list[str|Prednaska|Znalost,]] = {}
|
||||
|
||||
for h in hlasovani:
|
||||
if h.ucastnik not in table:
|
||||
table[h.ucastnik] = [h.ucastnik] + ([""] * width)
|
||||
|
||||
if h.prednaska.id in prednasky_map:
|
||||
table[h.ucastnik][prednasky_map[h.prednaska.id]] = h.body
|
||||
else:
|
||||
pass # Padat hlasitě?
|
||||
|
||||
for h in hlasovani_o_znalostech:
|
||||
ucastnik = str(h.ucastnik) + ' ' + str(h.ucastnik.id) # id, kvůli kolizi jmen
|
||||
if ucastnik not in table:
|
||||
table[ucastnik] = [ucastnik] + ([""] * width)
|
||||
|
||||
if h.znalost.id in znalosti_map:
|
||||
table[ucastnik][znalosti_map[h.znalost.id]] = h.odpoved
|
||||
else:
|
||||
pass # Padat hlasitě?
|
||||
|
||||
|
||||
response = HttpResponse(content_type="text/csv", charset="utf-8")
|
||||
response["Content-Disposition"] = 'attachment; filename="hlasovani.csv"'
|
||||
|
||||
writer = csv.writer(response)
|
||||
writer.writerow(["jména \\ přednáška|znalost"] + list(map(str, prednasky + znalosti)))
|
||||
for row in table.values():
|
||||
writer.writerow(list(map(str, row)))
|
||||
return response
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue