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.
134 lines
5.1 KiB
134 lines
5.1 KiB
# coding: utf-8
|
|
|
|
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
|
|
from cStringIO import StringIO
|
|
from django.core.files.base import ContentFile
|
|
|
|
from seminar.models import Soustredeni
|
|
|
|
VZDY=0
|
|
ORG=1
|
|
NIKDY=2
|
|
VIDITELNOST = (
|
|
(VZDY, 'Vždy'),
|
|
(ORG, 'Organizátorům'),
|
|
(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(force_unicode(gal.nazev), cesta)
|
|
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=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=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)
|
|
datum = models.DateTimeField('Datum pořízení fotografie', blank = True, null = True) # TODO zjistit z EXIFU
|
|
galerie = models.ForeignKey('Galerie')
|
|
poradi = models.IntegerField('Pořadí', blank = True, null = True)
|
|
def __unicode__(self):
|
|
return self.nazev + " -- " + unicode(self.obrazek_velky.name) + " (" + str(self.datum) + ")"
|
|
class Meta:
|
|
verbose_name = 'Obrázek'
|
|
verbose_name_plural = 'Obrázky'
|
|
def save(self):
|
|
original = Image.open(self.obrazek_velky)
|
|
jmeno = os.path.basename(self.obrazek_velky.file.name)
|
|
if not self.obrazek_stredni:
|
|
Obrazek._vyrobMiniaturu(original, jmeno, 500, self.obrazek_stredni)
|
|
if not self.obrazek_maly:
|
|
Obrazek._vyrobMiniaturu(original, jmeno, 200, self.obrazek_maly)
|
|
super(Obrazek, self).save()
|
|
|
|
@staticmethod
|
|
def _vyrobMiniaturu(original, jmeno, maximum, field):
|
|
zmensenina = Obrazek._zmensiObrazek(original, maximum)
|
|
f = StringIO()
|
|
try:
|
|
zmensenina.save(f, format=original.format)
|
|
data = ContentFile(f.getvalue())
|
|
finally:
|
|
f.close()
|
|
field.save(jmeno, data, save = False)
|
|
|
|
@staticmethod
|
|
def _zmensiObrazek(original, maximum):
|
|
"""Preskaluje obrazek tak, aby byl zachovan pomer stran a zadny rozmer
|
|
nepresahoval maxRozmer. Pokud zadny rozmer nepresahuje maxRozmer, tak
|
|
vrati puvodni obrazek (tj. nedojde ke zvetseni obrazku)."""
|
|
novaVelikost = Obrazek._zmensiVelikost(original.size, maximum)
|
|
return original.resize(novaVelikost, Image.ANTIALIAS)
|
|
|
|
@staticmethod
|
|
def _zmensiVelikost(velikost, maximum):
|
|
maximum = float(maximum)
|
|
w, h = velikost
|
|
soucasneMaximum = max(w, h)
|
|
if soucasneMaximum <= maximum:
|
|
return velikost
|
|
pomer = maximum/soucasneMaximum
|
|
return (int(w * pomer), int(h * pomer))
|
|
|
|
|
|
class Galerie(models.Model):
|
|
nazev = models.CharField('Název', max_length=100)
|
|
datum_vytvoreni = models.DateTimeField('Datum vytvoření', auto_now_add = True)
|
|
datum_zmeny = models.DateTimeField('Datum poslední změny', auto_now = True)
|
|
popis = models.TextField('Popis', blank = True, null = True)
|
|
titulni_obrazek = models.ForeignKey(Obrazek, blank = True, null = True, related_name = "+", on_delete = models.SET_NULL)
|
|
zobrazit = models.IntegerField('Zobrazit?', default = ORG, choices = VIDITELNOST)
|
|
galerie_up = models.ForeignKey('Galerie', blank = True, null = True)
|
|
soustredeni = models.ForeignKey(Soustredeni, blank = True, null = True)
|
|
|
|
def __unicode__(self):
|
|
return self.nazev
|
|
class Meta:
|
|
verbose_name = 'Galerie'
|
|
verbose_name_plural = 'Galerie'
|
|
|
|
#def link_na_preview(self):
|
|
#"""Odkaz na galerii, používá se v admin rozhranní. """
|
|
#return '<a href="/fotogalerie/galerie/%s/">Preview</a>' % self.id
|
|
#link_na_preview.allow_tags = True
|
|
#link_na_preview.short_description = 'Zobrazit galerii'
|
|
#
|
|
#def je_publikovano(self):
|
|
#"""Vraci True, pokud je tato galerie publikovana. """
|
|
#if self.zobrazit == VZDY:
|
|
#return True
|
|
#if self.zobrazit == PODLE_CLANKU:
|
|
#for clanek in self.clanek_set.all():
|
|
#if clanek.je_publikovano():
|
|
#return True
|
|
#return False
|
|
#
|
|
#@staticmethod
|
|
#def publikovane_galerie():
|
|
#"""Vraci galerie, ktere uz maji byt publikovane."""
|
|
#clanky = Blog.models.Clanek.publikovane_clanky()
|
|
#return Galerie.objects.filter(Q(zobrazit=VZDY) | (Q(clanek__in=clanky) & Q(zobrazit=PODLE_CLANKU))).distinct()
|
|
|