from django.shortcuts import get_object_or_404, render from django.http import HttpResponse from django.views import generic from django.conf import settings from django.contrib.staticfiles.finders import find from seminar.models import Soustredeni, Resitel, Soustredeni_Ucastnici, Nastaveni # Tohle je stare a chceme se toho zbavit. Pouzivejte s.ToCoChci import csv import tempfile import shutil import subprocess from pathlib import Path from seminar.views import obalkyView class SoustredeniListView(generic.ListView): model = Soustredeni template_name = 'soustredeni/seznam_soustredeni.html' def get_queryset(self): return ( Soustredeni.objects .prefetch_related( "ucastnici", "ucastnici__osoba", "organizatori", "organizatori__osoba", "galerie_set" ) .select_related("rocnik") ) def soustredeniObalkyView(request, soustredeni): soustredeni = get_object_or_404(Soustredeni, id=soustredeni) return obalkyView(request, soustredeni.ucastnici.all()) class SoustredeniUcastniciBaseView(generic.ListView): model = Soustredeni_Ucastnici def get_queryset(self): soustredeni = get_object_or_404( Soustredeni, pk=self.kwargs["soustredeni"] ) return Soustredeni_Ucastnici.objects.filter( soustredeni=soustredeni).select_related('resitel') class SoustredeniMailyUcastnikuView(SoustredeniUcastniciBaseView): """ Seznam e-mailů řešitelů oddělených čárkami. """ model = Soustredeni_Ucastnici template_name = 'soustredeni/maily_ucastniku.txt' class SoustredeniUcastniciView(SoustredeniUcastniciBaseView): """ HTML tabulka účastníků pro tisk. """ model = Soustredeni_Ucastnici template_name = 'soustredeni/seznam_ucastniku.html' def soustredeniUcastniciExportView(request, soustredeni): soustredeni = get_object_or_404(Soustredeni, id=soustredeni) ucastnici = Resitel.objects.filter(soustredeni=soustredeni) response = HttpResponse(content_type='text/csv') response['Content-Disposition'] = 'attachment; filename="ucastnici.csv"' writer = csv.writer(response) writer.writerow(["jmeno", "prijmeni", "rok_maturity", "telefon", "email", "ulice", "mesto", "psc","stat"]) for u in ucastnici: o = u.osoba writer.writerow([o.jmeno, o.prijmeni, str(u.rok_maturity), o.telefon, o.email, o.ulice, o.mesto, o.psc, o.stat.name]) return response def soustredeniStvrzenkyView(request, soustredeni): soustredeni = get_object_or_404(Soustredeni, id=soustredeni) ucastnici = Resitel.objects.filter(soustredeni=soustredeni) castka = Nastaveni.get_solo().cena_sous tex = render(request, 'soustredeni/stvrzenky.tex', {'ucastnici': ucastnici, 'soustredeni': soustredeni, 'castka': castka}).content tempdir = Path(tempfile.mkdtemp()) with open(tempdir / "stvrzenky.tex", "w") as texfile: texfile.write(tex.decode()) shutil.copy(find('images/logomm.pdf'), tempdir) subprocess.call(["pdflatex", "stvrzenky.tex"], cwd = tempdir, stdout=subprocess.DEVNULL) with open(tempdir / "stvrzenky.pdf", "rb") as pdffile: response = HttpResponse(pdffile.read(), content_type='application/pdf') shutil.rmtree(tempdir) return response