from datetime import datetime, date import random from django.conf import settings from header_fotky.models import FotkaUrlVazba def vzhled(request): ''' Podle casu prida do templatu, zdali je nebo neni noc ''' hodin = datetime.now().hour if (hodin <= 6) or (hodin >= 20): noc = True nedoba = 'den' doba = 'noc' else: noc = False nedoba = 'noc' doba = 'den' url = request.path fotky = FotkaUrlVazba.objects.exclude(denni_doba=nedoba) fotka = None # TODO rychlejší patternmatch? while (fotka is None) and (url != ''): presne = fotky.filter(url__exact=url) if presne.count() > 0: presne_doba = presne.filter(denni_doba=doba) if presne_doba.count() > 0: fotka = random.choice(presne_doba).url_fotky() else: fotka = random.choice(presne).url_fotky() url = url[:-1] index = url.rfind('/') if index != -1: url = url[:index+1] if fotka is None: fotka = settings.STATIC_URL + "images/header/vikendovka.jpg" return {'noc': noc, 'fotka': fotka}