From b1cf79271f328f50e07b53b92a9afa9fb0288bda Mon Sep 17 00:00:00 2001 From: "Bc. Petr Pecha" Date: Tue, 13 Oct 2015 12:49:00 +0200 Subject: [PATCH] galerie | ukladani fotek podle soustredeni --- galerie/models.py | 24 +++++++++++++++++++++--- galerie/views.py | 2 +- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/galerie/models.py b/galerie/models.py index 42dbaa50..3d8bddd4 100644 --- a/galerie/models.py +++ b/galerie/models.py @@ -4,6 +4,7 @@ from django.db import models import seminar.models from django.db.models import Q from django.utils import timezone +from django.utils.encoding import force_unicode from PIL import Image import os @@ -21,12 +22,29 @@ VIDITELNOST = ( (NIKDY, 'Nikdy'), ) +def obrazek_filename(self, filename): + return obrazek_filename_obecny(self, filename, "velky") + +def obrazek_filename_stredni(self, filename): + return obrazek_filename_obecny(self, filename, "stredni") + +def obrazek_filename_maly(self, filename): + return obrazek_filename_obecny(self, filename, "maly") + +def obrazek_filename_obecny(self, filename, typ): + gal = self.galerie + cesta = "" + while(not gal.soustredeni): + cesta = os.path.join(cesta, force_unicode(gal.nazev)) + gal = gal.galerie_up + return os.path.join('Galerie', force_unicode(gal.soustredeni), cesta, typ, force_unicode(self.nazev)) + class Obrazek(models.Model): # TODO vkladat do slozek podle soustredeni - obrazek_velky = models.ImageField(upload_to='Galerie/%Y/%m/%d', + obrazek_velky = models.ImageField(upload_to=obrazek_filename, help_text = "Lze vložit libovolně velký obrázek. Ideální je, aby alespoň jeden rozměr měl alespoň 500px.") - obrazek_stredni = models.ImageField(upload_to='Galerie/%Y/%m/%d/stredni', null = True, editable = False) - obrazek_maly = models.ImageField(upload_to='Galerie/%Y/%m/%d/male', null = True, editable = False) + obrazek_stredni = models.ImageField(upload_to=obrazek_filename_stredni, null = True, editable = False) + obrazek_maly = models.ImageField(upload_to=obrazek_filename_maly, null = True, editable = False) nazev = models.CharField('Název', max_length=50, blank = True, null = True) popis = models.TextField('Popis', blank = True, null = True) datum_vlozeni = models.DateTimeField('Datum vložení', auto_now_add = True) diff --git a/galerie/views.py b/galerie/views.py index f8b1d0f5..3af7066e 100644 --- a/galerie/views.py +++ b/galerie/views.py @@ -145,7 +145,7 @@ def new_galerie(request, galerie, soustredeni): for obr in request.FILES.getlist('obr'): o = Obrazek() o.obrazek_velky = obr - o.nazev = str(gal) + "/" + str(obr) + o.nazev = str(obr) o.datum = datetime.now() #TODO vycist z EXIFU o.galerie = gal o.save()