From 2dbbb588d038efbe0b62fa6d0e35fd155aacac7c Mon Sep 17 00:00:00 2001 From: ticvac Date: Tue, 12 Nov 2024 20:15:09 +0100 Subject: [PATCH] opravy pro pull --- Dockerfile | 10 ++++++++++ docker-compose.yaml | 12 ++++++++++++ mamweb/settings_common.py | 1 + requirements.txt | 4 ++-- soustredeni/models.py | 10 ++++++---- soustredeni/urls.py | 4 ++-- soustredeni/views.py | 39 +++++++++++++++++++++------------------ 7 files changed, 54 insertions(+), 26 deletions(-) create mode 100644 Dockerfile create mode 100644 docker-compose.yaml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..ed660d66 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM python:3.11.10-bookworm +WORKDIR /usr/src/app + +RUN pip install --upgrade pip +COPY ./requirements.txt . +RUN pip install -r requirements.txt + +COPY . . + +CMD ["python3", "manage.py", "runserver", "0.0.0.0:8000"] diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 00000000..052fd593 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,12 @@ +version: '3' + +services: + django_app: + build: . + volumes: + - this_vol:/usr/src/app/ + ports: + - 8000:8000 + +volumes: + this_vol: diff --git a/mamweb/settings_common.py b/mamweb/settings_common.py index 19616ac1..55817b6d 100644 --- a/mamweb/settings_common.py +++ b/mamweb/settings_common.py @@ -342,6 +342,7 @@ SEMINAR_KONFERY_DIR = os.path.join('konfery') KOREKTURY_PDF_DIR = os.path.join('korektury', 'pdf') KOREKTURY_IMG_DIR = os.path.join('korektury', 'img') CISLO_IMG_DIR = os.path.join('cislo', 'img') +SOUSTREDENI_KONTAKTNICKY_DIR = os.path.join('soustredeni', 'kontaktnicky') diff --git a/requirements.txt b/requirements.txt index bba1bad0..74ee4c6b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,8 +1,8 @@ --c constraints.txt +# -c constraints.txt setuptools # django-polymorphic má rozbité dependencies # basic libs -psycopg2 # PostgreSQL adaptér +# psycopg2 # PostgreSQL adaptér ipython # Interaktivní shell Unidecode # Přepisuje unicode do ASCII (např. soubory nebo e-maily) Pillow diff --git a/soustredeni/models.py b/soustredeni/models.py index c456aacd..e5d11ae9 100644 --- a/soustredeni/models.py +++ b/soustredeni/models.py @@ -18,9 +18,11 @@ from django.utils import timezone def generate_filename_kontaktnicek(self, filename): # generate random string length = 32 - fname = "kontaktnicky/" + timezone.now().strftime('%Y-%m-%d-%H_%M') + file_type = filename.split('.')[-1] + fname = "kontaktnicky/" + timezone.now().strftime('%Y-%m-%d-%H_%M') + "" fname += ''.join(secrets.choice(string.ascii_uppercase + string.digits) for _ in range(length)) - return os.path.join(settings.KOREKTURY_PDF_DIR, fname) + fname += '.' + file_type + return os.path.join(settings.SOUSTREDENI_KONTAKTNICKY_DIR, fname) logger = logging.getLogger(__name__) @@ -76,8 +78,8 @@ class Soustredeni(SeminarModelBase): exportovat = models.BooleanField('export do AESOPa', db_column='exportovat', default=False, help_text='Exportuje se jen podle tohoto flagu (ne veřejnosti)') - kontaktnicek_vcf = models.FileField('kontaktníček vcf', upload_to=generate_filename_kontaktnicek, blank=True,) - kontaktnicek_pdf = models.FileField('kontaktníček pdf', upload_to=generate_filename_kontaktnicek, blank=True,) + kontaktnicek_vcf = models.FileField('kontaktníček vcf', upload_to=generate_filename_kontaktnicek, blank=True, null=True) + kontaktnicek_pdf = models.FileField('kontaktníček pdf', upload_to=generate_filename_kontaktnicek, blank=True, null=True) def __str__(self): return '{} ({})'.format(self.misto, self.datum_zacatku) diff --git a/soustredeni/urls.py b/soustredeni/urls.py index af88e456..6e7f25e5 100644 --- a/soustredeni/urls.py +++ b/soustredeni/urls.py @@ -47,12 +47,12 @@ urlpatterns = [ path( 'kontaktnicek_pdf', views.soustredeniKontaktnicekPdfView, - name='soustredeni_kontaktnicek' + name='soustredeni_kontaktnicek_pdf' ), path( 'kontaktnicek_vcf', views.soustredeniKontaktnicekVcfView, - name='soustredeni_kontaktnicek' + name='soustredeni_kontaktnicek_vcf' ), path( 'fotogalerie/', diff --git a/soustredeni/views.py b/soustredeni/views.py index 615ba10d..cd326002 100644 --- a/soustredeni/views.py +++ b/soustredeni/views.py @@ -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 místo defaultně požadovaného -# 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