|
|
@ -3,6 +3,7 @@ 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 |
|
|
@ -108,7 +109,7 @@ class SoustredeniAbstraktyView(generic.DetailView): |
|
|
|
template_name = 'soustredeni/export_do_abstraktu.html' |
|
|
|
pk_url_kwarg = 'soustredeni' # v url bude <int:soustredeni> místo defaultně požadovaného <int:pk> |
|
|
|
|
|
|
|
# kontaktnicek |
|
|
|
# Kontaktníčky |
|
|
|
def soustredeniKontaktnicekPdfView(request, soustredeni): |
|
|
|
return soustredeniKontaktnicekView(request, soustredeni, "pdf") |
|
|
|
|
|
|
@ -117,22 +118,24 @@ def soustredeniKontaktnicekVcfView(request, soustredeni): |
|
|
|
|
|
|
|
def soustredeniKontaktnicekView(request, soustredeni, typ): |
|
|
|
soustredeni = get_object_or_404(Soustredeni, id=soustredeni) |
|
|
|
|
|
|
|
if (not request.user in [u.osoba.user for u in soustredeni.ucastnici.all()]): |
|
|
|
if not request.user.je_org: # nebyl jsi tam, nebo nejsi org |
|
|
|
raise Http404() |
|
|
|
if not soustredeni.kontaktnicek_pdf and typ == "pdf": # není k dispozici |
|
|
|
raise Http404() |
|
|
|
elif not soustredeni.kontaktnicek_vcf and typ == "vcf": # není k dispozici |
|
|
|
# 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() |
|
|
|
# není k dispozici |
|
|
|
if (not soustredeni.kontaktnicek_pdf and typ == "pdf") or (not soustredeni.kontaktnicek_vcf and typ == "vcf"): |
|
|
|
raise Http404() |
|
|
|
|
|
|
|
if typ == "pdf": |
|
|
|
with open(soustredeni.kontaktnicek_pdf.path, 'rb') as pdf: |
|
|
|
response = HttpResponse(pdf.read(), content_type='application/pdf') |
|
|
|
return response |
|
|
|
elif typ == "vcf": |
|
|
|
with open(soustredeni.kontaktnicek_vcf.path, 'rb') as vcf: |
|
|
|
response = HttpResponse(vcf.read(), content_type='text/vcard') |
|
|
|
return response |
|
|
|
else: |
|
|
|
raise ValueError("Nepodporovaný typ kontaktníčku") |
|
|
|
kontaktnicky = { |
|
|
|
'pdf': (soustredeni.kontaktnicek_pdf, 'applcation/pdf', 'rb'), |
|
|
|
'vcf': (soustredeni.kontaktnicek_vcf, 'text/vcard', 'rb'), # vcf je texťák, nevím, jestli je potřeba ho otevítat binárně. |
|
|
|
} |
|
|
|
|
|
|
|
try: |
|
|
|
field, mime, otevreni = kontaktnicky[typ] |
|
|
|
except KeyError as e: |
|
|
|
raise ValueError("Neznámý typ kontaktníčku") from e |
|
|
|
|
|
|
|
with open(field.path, otevreni) as kontaktnicek: |
|
|
|
response = HttpResponse(kontaktnicek.read(), content_type=mime) |
|
|
|
response['Content-Disposition'] = 'attachment; filename="kontaktnicek.{}"'.format(typ) |
|
|
|
return response |
|
|
|