Web M&M
https://mam.matfyz.cz
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
142 lines
4.8 KiB
142 lines
4.8 KiB
from django.shortcuts import get_object_or_404, render
|
|
from django.http import HttpResponse
|
|
from django.views import generic
|
|
from django.contrib.staticfiles.finders import find
|
|
from django.http import Http404
|
|
from django.core.exceptions import PermissionDenied
|
|
|
|
import csv
|
|
import tempfile
|
|
import shutil
|
|
import subprocess
|
|
from pathlib import Path
|
|
import http
|
|
|
|
import personalni.views
|
|
|
|
from .models import Soustredeni, Soustredeni_Ucastnici
|
|
from various.models import Nastaveni
|
|
|
|
|
|
class SoustredeniListView(generic.ListView):
|
|
model = Soustredeni
|
|
template_name = 'soustredeni/seznam_soustredeni.html'
|
|
|
|
def get_queryset(self):
|
|
if not self.request.user.je_org:
|
|
return super().get_queryset()
|
|
return (
|
|
super().get_queryset()
|
|
.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 personalni.views.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 = soustredeni.ucastnici.all()
|
|
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 = soustredeni.ucastnici.all()
|
|
if ucastnici.count() == 0:
|
|
return HttpResponse(
|
|
render(request, 'universal.html', {
|
|
'title': 'Není pro koho vyrobit stvrzenky.',
|
|
'text': 'Právě ses pokusil/a vygenerovat stvrzenky pro prázdnou množinu lidí. Můžeš to zkusit změnit, případně se zeptej webařů :-)',
|
|
}),
|
|
status=http.HTTPStatus.NOT_FOUND,
|
|
)
|
|
castka = Nastaveni.get_solo().cena_sous
|
|
tex = render(request, 'soustredeni/stvrzenky.tex', {'ucastnici': ucastnici, 'soustredeni': soustredeni, 'castka': castka}).content
|
|
|
|
with tempfile.TemporaryDirectory() as tempdirfn:
|
|
tempdir = Path(tempdirfn)
|
|
with open(tempdir / "stvrzenky.tex", "w") as texfile:
|
|
texfile.write(tex.decode())
|
|
|
|
shutil.copy(find('soustredeni/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')
|
|
return response
|
|
|
|
class SoustredeniAbstraktyView(generic.DetailView):
|
|
model = Soustredeni
|
|
template_name = 'soustredeni/export_do_abstraktu.html'
|
|
pk_url_kwarg = 'soustredeni' # v url bude <int:soustredeni> místo defaultně požadovaného <int:pk>
|
|
|
|
# Kontaktníčky
|
|
def soustredeniKontaktnicekPdfView(request, soustredeni):
|
|
return soustredeniKontaktnicekView(request, soustredeni, "pdf")
|
|
|
|
def soustredeniKontaktnicekVcfView(request, soustredeni):
|
|
return soustredeniKontaktnicekView(request, soustredeni, "vcf")
|
|
|
|
def soustredeniKontaktnicekView(request, soustredeni, typ):
|
|
soustredeni = get_object_or_404(Soustredeni, id=soustredeni)
|
|
# nebyl jsi tam, nebo nejsi org
|
|
if (not request.user in [u.osoba.user for u in soustredeni.ucastnici.all()]) and not request.user.je_org:
|
|
raise PermissionDenied()
|
|
|
|
kontaktnicky = {
|
|
'pdf': (soustredeni.kontaktnicek_pdf, 'applcation/pdf', 'rb'),
|
|
'vcf': (soustredeni.kontaktnicek_vcf, 'text/vcard', 'rb'),
|
|
}
|
|
|
|
try:
|
|
field, mime, otevreni = kontaktnicky[typ]
|
|
except KeyError as e:
|
|
raise ValueError("Neznámý typ kontaktníčku") from e
|
|
|
|
# není k dispozici
|
|
if not field:
|
|
raise Http404()
|
|
|
|
with open(field.path, otevreni) as kontaktnicek:
|
|
response = HttpResponse(kontaktnicek.read(), content_type=mime)
|
|
response['Content-Disposition'] = 'attachment; filename="kontaktnicek.{}"'.format(typ)
|
|
return response
|
|
|