143 lines
		
	
	
	
		
			5 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			143 lines
		
	
	
	
		
			5 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
| from django.shortcuts import get_object_or_404
 | |
| 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 various.views.generic
 | |
| from personalni.views import obalkyView
 | |
| 
 | |
| 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")
 | |
| 		)
 | |
| 
 | |
| 
 | |
| class KonkretniSoustredeniMixin:
 | |
| 	""" Přidá k View s parametrem `soustredeni` atribut `self.soustredeni` """
 | |
| 	def setup(self, request, *args, **kwargs):
 | |
| 		super().setup(request, *args, **kwargs)
 | |
| 		soustredeni_id = self.kwargs["soustredeni"]
 | |
| 		self.soustredeni = get_object_or_404(Soustredeni, id=soustredeni_id)
 | |
| 
 | |
| 
 | |
| class SoustredeniUcastniciBaseView(
 | |
| 	KonkretniSoustredeniMixin,
 | |
| 	various.views.generic.NeprazdnyListView,
 | |
| ):
 | |
| 	"""
 | |
| 		Slouží jako ListView účastníků soustředění
 | |
| 		+ háže inteligentní chybu při soustředění bez účastníků
 | |
| 	"""
 | |
| 	model = Soustredeni_Ucastnici
 | |
| 	if_prazdny_title = "K soustředění nejsou přidaní žádní účastníci"
 | |
| 	if_prazdny_text = "K tebou zvolenému soustředění nejsou přidaní žádní účastníci, tedy není co zobrazit. Můžeš to zkusit změnit v adminu, případně se zeptej webařů :-)"
 | |
| 
 | |
| 	def get_queryset(self):
 | |
| 		return Soustredeni_Ucastnici.objects.filter(
 | |
| 			soustredeni=self.soustredeni).select_related('resitel', 'resitel__osoba')
 | |
| 
 | |
| 
 | |
| # FIXME předělat jako ostatní (vyžaduje předělání `obalkyView`)
 | |
| def soustredeniObalkyView(request, soustredeni):
 | |
| 	soustredeni = get_object_or_404(Soustredeni, id=soustredeni)
 | |
| 	return obalkyView(request, soustredeni.ucastnici.all())
 | |
| 
 | |
| 
 | |
| class SoustredeniMailyUcastnikuView(SoustredeniUcastniciBaseView):
 | |
| 	""" Seznam e-mailů řešitelů oddělených čárkami. """
 | |
| 	template_name = 'soustredeni/maily_ucastniku.txt'
 | |
| 
 | |
| 
 | |
| class SoustredeniUcastniciView(SoustredeniUcastniciBaseView):
 | |
| 	""" HTML tabulka účastníků pro tisk. """
 | |
| 	template_name = 'soustredeni/seznam_ucastniku.html'
 | |
| 
 | |
| 
 | |
| class SoustredeniUcastniciExportView(SoustredeniUcastniciBaseView):
 | |
| 	""" CSV tabulka účastníků. """
 | |
| 	def render(self, request, *args, **kwargs):
 | |
| 		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 self.object_list:
 | |
| 			o = u.resitel.osoba
 | |
| 			writer.writerow([o.jmeno, o.prijmeni, str(u.resitel.rok_maturity), o.telefon, o.email, o.ulice, o.mesto, o.psc, o.stat.name])
 | |
| 		return response
 | |
| 
 | |
| 
 | |
| class SoustredeniStvrzenkyView(
 | |
| 	various.views.generic.TeXResponseMixin,
 | |
| 	SoustredeniUcastniciBaseView,
 | |
| ):
 | |
| 	template_name = 'soustredeni/stvrzenky.tex'
 | |
| 	dalsi_potrebne_soubory = [find('soustredeni/logomm.pdf')]
 | |
| 
 | |
| 	if_prazdny_title = "Není pro koho vyrobit stvrzenky."
 | |
| 	if_prazdny_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řů :-)"
 | |
| 
 | |
| 	def get_context_data(self, **kwargs):
 | |
| 		context = super().get_context_data(**kwargs)
 | |
| 
 | |
| 		context["castka"] = Nastaveni.get_solo().cena_sous
 | |
| 		context["soustredeni"] = self.soustredeni
 | |
| 		context["ucastnici"] = self.object_list
 | |
| 		return context
 | |
| 
 | |
| 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
 |