|
|
|
# coding: utf-8
|
|
|
|
|
|
|
|
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.is_staff:
|
|
|
|
preview = True;
|
|
|
|
else:
|
|
|
|
raise Http404
|
|
|
|
return preview
|
|
|
|
|
|
|
|
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')
|
|
|
|
obrazky = Obrazek.objects.filter(galerie = galerie).order_by('datum')
|
|
|
|
preview = zobrazit(galerie, request)
|
|
|
|
return render(request, 'galerie/GalerieNahled.html',
|
|
|
|
{'galerie' : galerie,
|
|
|
|
'podgalerie' : podgalerie,
|
|
|
|
'obrazky' : obrazky,
|
|
|
|
'preview' : preview,
|
|
|
|
})
|
|
|
|
|
|
|
|
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('datum')
|
|
|
|
|
|
|
|
# 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:
|
|
|
|
znacka = i
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
# Obrazek neni v galerii/stitku.
|
|
|
|
raise Http404
|
|
|
|
|
|
|
|
# Nacteni okolnich obrazku.
|
|
|
|
obrazky_dalsi = obrazky[znacka+1:znacka+NAHLEDU+1]
|
|
|
|
if znacka > NAHLEDU:
|
|
|
|
obrazky_predchozi = obrazky[znacka-NAHLEDU:znacka]
|
|
|
|
else:
|
|
|
|
obrazky_predchozi = obrazky[0:znacka]
|
|
|
|
|
|
|
|
# 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,
|
|
|
|
'obrazek' : obrazek,
|
|
|
|
'vyska' : vyska,
|
|
|
|
'sirka' : sirka,
|
|
|
|
'obrazky_predchozi' : obrazky_predchozi,
|
|
|
|
'obrazky_dalsi' : obrazky_dalsi,
|
|
|
|
'preview' : preview,
|
|
|
|
'form' : form,
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
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']
|
|
|
|
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('../../')
|
|
|
|
|