Browse Source

Organizator | model

remotes/origin/vysl
Bc. Petr Pecha 10 years ago
parent
commit
1777656d27
  1. 69
      seminar/models.py

69
seminar/models.py

@ -12,6 +12,10 @@ from django.utils.text import slugify
from django.core.urlresolvers import reverse
from django.core.cache import cache
from PIL import Image
import os
from cStringIO import StringIO
from django.core.files.base import ContentFile
from django_countries.fields import CountryField
from solo.models import SingletonModel
@ -637,3 +641,68 @@ class Novinky(models.Model):
verbose_name = 'Novinka'
verbose_name_plural = 'Novinky'
@python_2_unicode_compatible
class Organizator(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, verbose_name='Osoba',
help_text = 'Vyber účet spřažený s organizátorem.')
prezdivka = models.CharField('Přezdívka', max_length = 32,
null = True, blank = True)
organizuje_od_roku = models.IntegerField('Organizuje od roku',
null = True, blank = True)
organizuje_do_roku = models.IntegerField('Organizuje do roku',
null = True, blank = True)
studuje = models.CharField('Studuje', max_length = 256,
null = True, blank = True)
strucny_popis_organizatora = models.TextField('Stručný popis organizátora',
null = True, blank = True)
foto = models.ImageField('Fotografie organizátora',
upload_to='image_organizatori/%Y/', null = True, blank = True,
help_text = 'Vlož fotografii organizátora o libovoné velikosti')
foto_male = models.ImageField(upload_to='image_organizatori/male/%Y/',
null = True, blank = True, editable = False)
def __str__(self):
return str(self.user)
class Meta:
verbose_name = 'Organizátor'
verbose_name_plural = 'Organizátoři'
def save(self):
original = Image.open(self.foto)
jmeno = os.path.basename(self.foto.file.name)
Organizator._vyrobMiniaturu(original, jmeno, 500, self.foto)
Organizator._vyrobMiniaturu(original, jmeno, 200, self.foto_male)
super(Organizator, self).save()
@staticmethod
def _vyrobMiniaturu(original, jmeno, maximum, field):
zmensenina = Organizator._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 = Organizator._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))

Loading…
Cancel
Save