|
|
|
import random
|
|
|
|
|
|
|
|
from django.http import HttpResponse, Http404
|
|
|
|
from django.shortcuts import render, HttpResponseRedirect, get_object_or_404
|
|
|
|
from django.template import RequestContext
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
from galerie.models import Obrazek, Galerie
|
|
|
|
from seminar.models import Soustredeni
|
|
|
|
from galerie.forms import KomentarForm, NewGalerieForm
|
|
|
|
|
|
|
|
def zobrazit(galerie, request):
|
|
|
|
preview = False
|
|
|
|
if galerie.zobrazit >= 1:
|
|
|
|
if request.user.je_org:
|
|
|
|
preview = True;
|
|
|
|
else:
|
|
|
|
raise Http404
|
|
|
|
return preview
|
|
|
|
|
|
|
|
|
|
|
|
def cesta_od_korene(g):
|
|
|
|
"""Vrátí seznam galerií od kořene ke g"""
|
|
|
|
cesta = []
|
|
|
|
while g is not None:
|
|
|
|
cesta.append(g)
|
|
|
|
g = g.galerie_up
|
|
|
|
return reversed(cesta)
|
|
|
|
|
|
|
|
|
|
|
|
def nahled(request, pk, soustredeni):
|
|
|
|
"""Zobrazeni nahledu vsech fotek ve skupine."""
|
|
|
|
galerie = get_object_or_404(Galerie, pk=pk)
|
|
|
|
|
|
|
|
podgalerie = Galerie.objects.filter(galerie_up = galerie).order_by('poradi')
|
|
|
|
if not request.user.je_org:
|
|
|
|
podgalerie = podgalerie.filter(zobrazit__lt=1)
|
|
|
|
|
|
|
|
obrazky = Obrazek.objects.filter(galerie = galerie).order_by('poradi', 'nazev')
|
|
|
|
preview = zobrazit(galerie, request)
|
|
|
|
|
|
|
|
sourozenci = []
|
|
|
|
if galerie.galerie_up:
|
|
|
|
sourozenci = galerie.galerie_up.galerie_set.all().order_by('poradi')
|
|
|
|
if not request.user.je_org:
|
|
|
|
sourozenci = sourozenci.filter(zobrazit__lt=1)
|
|
|
|
|
|
|
|
predchozi = None
|
|
|
|
nasledujici = None
|
|
|
|
minuly = None
|
|
|
|
for g in sourozenci:
|
|
|
|
if g.pk == galerie.pk:
|
|
|
|
predchozi = minuly
|
|
|
|
if minuly is not None and minuly.pk == galerie.pk:
|
|
|
|
nasledujici = g
|
|
|
|
break
|
|
|
|
minuly = g
|
|
|
|
|
|
|
|
cesta = cesta_od_korene(galerie)
|
|
|
|
|
|
|
|
return render(request, 'galerie/GalerieNahled.html',
|
|
|
|
{'galerie' : galerie,
|
|
|
|
'podgalerie' : podgalerie,
|
|
|
|
'obrazky' : obrazky,
|
|
|
|
'preview' : preview,
|
|
|
|
'cesta': cesta,
|
|
|
|
'sourozenci': sourozenci,
|
|
|
|
'predchozi': predchozi,
|
|
|
|
'nasledujici': nasledujici,
|
|
|
|
})
|
|
|
|
|
|
|
|
def detail(request, pk, fotka, soustredeni):
|
|
|
|
"""Zobrazeni nahledu fotky s id 'fotka'."""
|
|
|
|
MAX_VYSKA = 900
|
|
|
|
MAX_SIRKA = 900
|
|
|
|
MAX_VYSKA_MALA = 100
|
|
|
|
MAX_SIRKA_MALA = 200
|
|
|
|
NAHLEDU = 1
|
|
|
|
|
|
|
|
galerie = get_object_or_404(Galerie, pk=pk)
|
|
|
|
preview = zobrazit(galerie, request)
|
|
|
|
obrazek = get_object_or_404(Obrazek, pk=fotka)
|
|
|
|
obrazky = galerie.obrazek_set.all().order_by('poradi', 'nazev')
|
|
|
|
|
|
|
|
# vytvoreni a obslouzeni formulare
|
|
|
|
if request.method == 'POST':
|
|
|
|
form = KomentarForm(request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
obrazek.popis = form.cleaned_data['komentar']
|
|
|
|
obrazek.save()
|
|
|
|
else:
|
|
|
|
form = KomentarForm({'komentar': obrazek.popis})
|
|
|
|
|
|
|
|
# Poradi aktualniho obrazku v galerii/stitku.
|
|
|
|
for i in range(len(obrazky)):
|
|
|
|
if obrazky[i] == obrazek:
|
|
|
|
poradi = i
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
# Obrazek neni v galerii/stitku.
|
|
|
|
raise Http404
|
|
|
|
|
|
|
|
|
|
|
|
# Nacteni okolnich obrazku a galerii
|
|
|
|
# TODO vyjmout zjisteni predchozich a nasledujicich galerii
|
|
|
|
# a udelat z toho funkci, ktera se pouzije u nahledu
|
|
|
|
predchozi_galerie = None
|
|
|
|
nasledujici_galerie = None
|
|
|
|
obrazky_dalsi = obrazky[poradi+1:poradi+NAHLEDU+1]
|
|
|
|
if (poradi+1) > NAHLEDU:
|
|
|
|
obrazky_predchozi = obrazky[poradi-NAHLEDU:poradi]
|
|
|
|
else:
|
|
|
|
obrazky_predchozi = obrazky[0:poradi]
|
|
|
|
if galerie.poradi > 1:
|
|
|
|
predchozi_galerie = Galerie.objects.\
|
|
|
|
filter(galerie_up=galerie.galerie_up).\
|
|
|
|
filter(poradi=(galerie.poradi-1))
|
|
|
|
if predchozi_galerie:
|
|
|
|
predchozi_galerie = predchozi_galerie[0]
|
|
|
|
else:
|
|
|
|
predchozi_galerie = None
|
|
|
|
if (poradi+1) == len(obrazky): # Tohle je poslední obrázek
|
|
|
|
if (galerie.poradi is not None
|
|
|
|
and galerie.galerie_up is not None):
|
|
|
|
nasledujici_galerie = Galerie.objects.\
|
|
|
|
filter(galerie_up=galerie.galerie_up).\
|
|
|
|
filter(poradi=(galerie.poradi+1))
|
|
|
|
else:
|
|
|
|
nasledujici_galerie = None
|
|
|
|
if nasledujici_galerie:
|
|
|
|
nasledujici_galerie = nasledujici_galerie[0]
|
|
|
|
else:
|
|
|
|
nasledujici_galerie = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Preskalovani obrazku do vybraneho prostoru.
|
|
|
|
vyska = obrazek.obrazek_stredni.height
|
|
|
|
sirka = obrazek.obrazek_stredni.width
|
|
|
|
if vyska > MAX_VYSKA:
|
|
|
|
sirka = sirka * MAX_VYSKA / vyska
|
|
|
|
vyska = MAX_VYSKA
|
|
|
|
if sirka > MAX_SIRKA:
|
|
|
|
vyska = vyska * MAX_SIRKA / sirka
|
|
|
|
sirka = MAX_SIRKA
|
|
|
|
|
|
|
|
return render(request, 'galerie/Galerie.html',
|
|
|
|
{'galerie' : galerie,
|
|
|
|
'predchozi_galerie' : predchozi_galerie,
|
|
|
|
'nasledujici_galerie' : nasledujici_galerie,
|
|
|
|
'obrazek' : obrazek,
|
|
|
|
'vyska' : vyska,
|
|
|
|
'sirka' : sirka,
|
|
|
|
'obrazky_predchozi' : obrazky_predchozi,
|
|
|
|
'obrazky_dalsi' : obrazky_dalsi,
|
|
|
|
'preview' : preview,
|
|
|
|
'form' : form,
|
|
|
|
'cesta': cesta_od_korene(galerie),
|
|
|
|
})
|
|
|
|
|
|
|
|
def new_galerie(request, galerie, soustredeni):
|
|
|
|
|
|
|
|
# zjistime k jakemu soustredeni se vaze nove vytvarena galerie
|
|
|
|
soustredeni = get_object_or_404(Soustredeni, pk = soustredeni)
|
|
|
|
# pokud je parametr galerie 0, pak jde o hlavni galerii
|
|
|
|
# kdyz je nejaky jiny, pak je pk galerie pod kterou tu dalsi vytvarim
|
|
|
|
if int(galerie) == 0:
|
|
|
|
galerie_up = False
|
|
|
|
galerie_text = "Hlavní fotogalerie soustředění"
|
|
|
|
else:
|
|
|
|
galerie_up = get_object_or_404(Galerie, pk = int(galerie))
|
|
|
|
galerie_text = "podgalerii ke galerii " + str(galerie_up)
|
|
|
|
|
|
|
|
# obsluha formulare umoznujiciho multiple nahravani fotek
|
|
|
|
if request.method == 'POST':
|
|
|
|
form = NewGalerieForm(request.POST, request.FILES)
|
|
|
|
if form.is_valid():
|
|
|
|
# vytvoreni nove galerie
|
|
|
|
gal = Galerie()
|
|
|
|
gal.nazev = form.cleaned_data['nazev']
|
|
|
|
#gal.popis = form.cleaned_data['popis'] # popis nepouzivame
|
|
|
|
gal.zobrazit = 1 # galerie je v procesu vytvareni
|
|
|
|
''' pokud je to podgalerie pridej nadrazenou galerii
|
|
|
|
a nadrazene soustredeni nechej volne,
|
|
|
|
pokud je to hlavni galerie, tak nadrazena galerie neexistuje,
|
|
|
|
ale v takovem pripade musi byt nadrazene soustredeni a ne jinak '''
|
|
|
|
if galerie_up:
|
|
|
|
gal.galerie_up = galerie_up
|
|
|
|
else:
|
|
|
|
gal.soustredeni = soustredeni
|
|
|
|
if gal.galerie_up:
|
|
|
|
gal.poradi = int(len(gal.galerie_up.galerie_set.all())) + 1
|
|
|
|
gal.save()
|
|
|
|
|
|
|
|
# zpracovani obrazku v galerii
|
|
|
|
for obr in request.FILES.getlist('obr'):
|
|
|
|
o = Obrazek()
|
|
|
|
o.obrazek_velky = obr
|
|
|
|
o.nazev = str(obr)
|
|
|
|
o.galerie = gal
|
|
|
|
o.save()
|
|
|
|
|
|
|
|
# presmerovani na prave vzniklou galerii
|
|
|
|
return HttpResponseRedirect('../../' + str(gal.pk))
|
|
|
|
|
|
|
|
else:
|
|
|
|
form = NewGalerieForm()
|
|
|
|
|
|
|
|
|
|
|
|
return render(request, 'galerie/GalerieNew.html',
|
|
|
|
{
|
|
|
|
'form' : form,
|
|
|
|
'soustredeni' : soustredeni,
|
|
|
|
'galerie_text' : galerie_text,
|
|
|
|
})
|
|
|
|
|
|
|
|
def plus_galerie(request, galerie, soustredeni, subgalerie):
|
|
|
|
galerie = get_object_or_404(Galerie, pk=subgalerie)
|
|
|
|
if galerie.poradi:
|
|
|
|
galerie.poradi += 1
|
|
|
|
else:
|
|
|
|
galerie.poradi = int(len(galerie.galerie_up.galerie_set.all()))
|
|
|
|
galerie.save()
|
|
|
|
return HttpResponseRedirect('../../')
|
|
|
|
|
|
|
|
def minus_galerie(request, galerie, soustredeni, subgalerie):
|
|
|
|
galerie = get_object_or_404(Galerie, pk=subgalerie)
|
|
|
|
if galerie.poradi:
|
|
|
|
galerie.poradi -= 1
|
|
|
|
else:
|
|
|
|
galerie.poradi = 1
|
|
|
|
galerie.save()
|
|
|
|
return HttpResponseRedirect('../../')
|
|
|
|
|