Jonas Havelka
4 years ago
8 changed files with 342 additions and 35 deletions
@ -0,0 +1,171 @@ |
|||||
|
[ |
||||
|
{ |
||||
|
"model": "mamweb.fotkaheader", |
||||
|
"pk": "baliky.jpg", |
||||
|
"fields": { |
||||
|
"cas": "2020-09-20T09:18:34.562Z", |
||||
|
"fotka": "header/baliky.jpg" |
||||
|
} |
||||
|
}, |
||||
|
{ |
||||
|
"model": "mamweb.fotkaheader", |
||||
|
"pk": "beh.jpg", |
||||
|
"fields": { |
||||
|
"cas": "2020-09-20T09:18:34.562Z", |
||||
|
"fotka": "header/beh.jpg" |
||||
|
} |
||||
|
}, |
||||
|
{ |
||||
|
"model": "mamweb.fotkaheader", |
||||
|
"pk": "kryptografie.jpg", |
||||
|
"fields": { |
||||
|
"cas": "2020-09-20T09:18:34.562Z", |
||||
|
"fotka": "header/kryptografie.jpg" |
||||
|
} |
||||
|
}, |
||||
|
{ |
||||
|
"model": "mamweb.fotkaheader", |
||||
|
"pk": "mam_cernobile.jpg", |
||||
|
"fields": { |
||||
|
"cas": "2020-09-20T09:18:34.562Z", |
||||
|
"fotka": "header/mam_cernobile.jpg" |
||||
|
} |
||||
|
}, |
||||
|
{ |
||||
|
"model": "mamweb.fotkaheader", |
||||
|
"pk": "noc.jpg", |
||||
|
"fields": { |
||||
|
"cas": "2020-09-20T09:18:34.562Z", |
||||
|
"fotka": "header/noc.jpg" |
||||
|
} |
||||
|
}, |
||||
|
{ |
||||
|
"model": "mamweb.fotkaheader", |
||||
|
"pk": "ohen.jpg", |
||||
|
"fields": { |
||||
|
"cas": "2020-09-20T09:18:34.562Z", |
||||
|
"fotka": "header/ohen.jpg" |
||||
|
} |
||||
|
}, |
||||
|
{ |
||||
|
"model": "mamweb.fotkaheader", |
||||
|
"pk": "snih.jpg", |
||||
|
"fields": { |
||||
|
"cas": "2020-09-20T09:18:34.562Z", |
||||
|
"fotka": "header/snih.jpg" |
||||
|
} |
||||
|
}, |
||||
|
{ |
||||
|
"model": "mamweb.fotkaheader", |
||||
|
"pk": "spolecna.jpg", |
||||
|
"fields": { |
||||
|
"cas": "2020-09-20T09:18:34.562Z", |
||||
|
"fotka": "header/spolecna.jpg" |
||||
|
} |
||||
|
}, |
||||
|
{ |
||||
|
"model": "mamweb.fotkaheader", |
||||
|
"pk": "stiny.jpg", |
||||
|
"fields": { |
||||
|
"cas": "2020-09-20T09:18:34.562Z", |
||||
|
"fotka": "header/stiny.jpg" |
||||
|
} |
||||
|
}, |
||||
|
{ |
||||
|
"model": "mamweb.fotkaheader", |
||||
|
"pk": "vikendovka.jpg", |
||||
|
"fields": { |
||||
|
"cas": "2020-09-20T09:18:34.562Z", |
||||
|
"fotka": "header/vikendovka.jpg" |
||||
|
} |
||||
|
}, |
||||
|
{ |
||||
|
"model": "mamweb.fotkaheader", |
||||
|
"pk": "vylet.jpg", |
||||
|
"fields": { |
||||
|
"cas": "2020-09-20T09:18:34.562Z", |
||||
|
"fotka": "header/vylet.jpg" |
||||
|
} |
||||
|
}, |
||||
|
{ |
||||
|
"model": "mamweb.fotkaurlvazba", |
||||
|
"pk": 1, |
||||
|
"fields": { |
||||
|
"url": "/archiv/", |
||||
|
"fotka": "stiny.jpg", |
||||
|
"denni_doba": "oboji" |
||||
|
} |
||||
|
}, |
||||
|
{ |
||||
|
"model": "mamweb.fotkaurlvazba", |
||||
|
"pk": 2, |
||||
|
"fields": { |
||||
|
"url": "/clanky/", |
||||
|
"fotka": "kryptografie.jpg", |
||||
|
"denni_doba": "den" |
||||
|
} |
||||
|
}, |
||||
|
{ |
||||
|
"model": "mamweb.fotkaurlvazba", |
||||
|
"pk": 3, |
||||
|
"fields": { |
||||
|
"url": "/clanky/", |
||||
|
"fotka": "ohen.jpg", |
||||
|
"denni_doba": "noc" |
||||
|
} |
||||
|
}, |
||||
|
{ |
||||
|
"model": "mamweb.fotkaurlvazba", |
||||
|
"pk": 4, |
||||
|
"fields": { |
||||
|
"url": "/zadani/", |
||||
|
"fotka": "baliky.jpg", |
||||
|
"denni_doba": "den" |
||||
|
} |
||||
|
}, |
||||
|
{ |
||||
|
"model": "mamweb.fotkaurlvazba", |
||||
|
"pk": 5, |
||||
|
"fields": { |
||||
|
"url": "/zadani/", |
||||
|
"fotka": "stiny.jpg", |
||||
|
"denni_doba": "noc" |
||||
|
} |
||||
|
}, |
||||
|
{ |
||||
|
"model": "mamweb.fotkaurlvazba", |
||||
|
"pk": 6, |
||||
|
"fields": { |
||||
|
"url": "/co-je-MaM/", |
||||
|
"fotka": "vikendovka.jpg", |
||||
|
"denni_doba": "den" |
||||
|
} |
||||
|
}, |
||||
|
{ |
||||
|
"model": "mamweb.fotkaurlvazba", |
||||
|
"pk": 7, |
||||
|
"fields": { |
||||
|
"url": "/co-je-MaM/", |
||||
|
"fotka": "noc.jpg", |
||||
|
"denni_doba": "noc" |
||||
|
} |
||||
|
}, |
||||
|
{ |
||||
|
"model": "mamweb.fotkaurlvazba", |
||||
|
"pk": 8, |
||||
|
"fields": { |
||||
|
"url": "/soustredeni/", |
||||
|
"fotka": "beh.jpg", |
||||
|
"denni_doba": "den" |
||||
|
} |
||||
|
}, |
||||
|
{ |
||||
|
"model": "mamweb.fotkaurlvazba", |
||||
|
"pk": 9, |
||||
|
"fields": { |
||||
|
"url": "/soustredeni/", |
||||
|
"fotka": "vylet.jpg", |
||||
|
"denni_doba": "noc" |
||||
|
} |
||||
|
} |
||||
|
] |
@ -1,12 +1,45 @@ |
|||||
|
|
||||
from datetime import datetime, date |
from datetime import datetime, date |
||||
|
import random |
||||
|
|
||||
|
from django.conf import settings |
||||
|
|
||||
|
from mamweb.models import FotkaUrlVazba |
||||
|
|
||||
|
|
||||
def vzhled(request): |
def vzhled(request): |
||||
''' Podle casu prida do templatu, zdali je nebo neni noc ''' |
''' Podle casu prida do templatu, zdali je nebo neni noc ''' |
||||
hodin = datetime.now().hour |
hodin = datetime.now().hour |
||||
if (hodin <= 6) or (hodin >= 20): |
if (hodin <= 6) or (hodin >= 20): |
||||
noc = True |
noc = True |
||||
|
nedoba = 'den' |
||||
|
doba = 'noc' |
||||
else: |
else: |
||||
noc = False |
noc = False |
||||
return {'noc' : noc} |
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} |
||||
|
|
||||
|
@ -0,0 +1,60 @@ |
|||||
|
# Generated by Django 2.2.15 on 2020-09-20 09:14 |
||||
|
|
||||
|
from django.db import migrations, models |
||||
|
import django.db.models.deletion |
||||
|
import django.utils.timezone |
||||
|
|
||||
|
from shutil import copytree |
||||
|
|
||||
|
|
||||
|
def zkopiruj_fotky(apps, schema_editor): |
||||
|
copytree("mamweb/static/images/header/", "media/header/", dirs_exist_ok=True) |
||||
|
|
||||
|
|
||||
|
class Migration(migrations.Migration): |
||||
|
initial = True |
||||
|
|
||||
|
dependencies = [ |
||||
|
] |
||||
|
|
||||
|
operations = [ |
||||
|
migrations.CreateModel( |
||||
|
name='FotkaHeader', |
||||
|
fields=[ |
||||
|
('cas', models.DateTimeField(default=django.utils.timezone.now, help_text='Čas vložení fotky', |
||||
|
verbose_name='čas vložení fotky')), |
||||
|
('nazev', |
||||
|
models.CharField(help_text='Název např. archiv_noc', max_length=50, primary_key=True, serialize=False, |
||||
|
unique=True, verbose_name='název fotky')), |
||||
|
('fotka', models.ImageField(upload_to='header')), |
||||
|
], |
||||
|
options={ |
||||
|
'verbose_name': 'fotka do pozadí menu', |
||||
|
'verbose_name_plural': 'fotky do pozadí menu', |
||||
|
'db_table': 'fotky_header', |
||||
|
'ordering': ['-cas'], |
||||
|
}, |
||||
|
), |
||||
|
migrations.CreateModel( |
||||
|
name='FotkaUrlVazba', |
||||
|
fields=[ |
||||
|
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), |
||||
|
('url', |
||||
|
models.CharField(blank=True, help_text='url prefix stránek např: /archiv/ nebo /', max_length=100, |
||||
|
verbose_name='URL')), |
||||
|
('denni_doba', models.CharField(choices=[('den', 'Zobrazit jen ve dne'), ('noc', 'Zobrazit jen v noci'), |
||||
|
('oboji', 'Zobrazovat pořád')], default='oboji', max_length=16, |
||||
|
verbose_name='denní doba')), |
||||
|
('fotka', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='mamweb.FotkaHeader', |
||||
|
verbose_name='fotka')), |
||||
|
], |
||||
|
options={ |
||||
|
'verbose_name': 'vazba url a fotky do pozadí menu', |
||||
|
'verbose_name_plural': 'vazby url a fotek do pozadí menu', |
||||
|
'db_table': 'fotky_url_vazby', |
||||
|
'ordering': ['url'], |
||||
|
}, |
||||
|
|
||||
|
), |
||||
|
migrations.RunPython(zkopiruj_fotky, migrations.RunPython.noop), |
||||
|
] |
@ -0,0 +1,65 @@ |
|||||
|
from django.core.exceptions import ValidationError |
||||
|
from django.db import models |
||||
|
from django.utils import timezone |
||||
|
|
||||
|
|
||||
|
class FotkaHeader(models.Model): |
||||
|
class Meta: |
||||
|
ordering = ['-cas'] |
||||
|
db_table = 'fotky_header' |
||||
|
verbose_name = u'fotka do pozadí menu' |
||||
|
verbose_name_plural = u'fotky do pozadí menu' |
||||
|
|
||||
|
cas = models.DateTimeField(u'čas vložení fotky', default=timezone.now, help_text='Čas vložení fotky') |
||||
|
|
||||
|
nazev = models.CharField( |
||||
|
u'název fotky', null=False, blank=False, unique=True, primary_key=True, |
||||
|
max_length=50, help_text='Název např. archiv_noc' |
||||
|
) |
||||
|
|
||||
|
fotka = models.ImageField(upload_to='header', null=False, blank=False) |
||||
|
|
||||
|
def __str__(self): |
||||
|
return self.nazev |
||||
|
|
||||
|
def clean(self): |
||||
|
if not self.fotka: |
||||
|
raise ValidationError("Chybí obrázek") |
||||
|
""" Kontroluje, zda sedí poměr stran """ |
||||
|
if abs(self.fotka.width - (self.fotka.height * 970 / 350)) > 2: |
||||
|
raise ValidationError("Obrázek by měl mít rozměry 970w na 350h, nebo alespoň podobný poměr stran.") |
||||
|
super().clean() |
||||
|
|
||||
|
|
||||
|
class FotkaUrlVazba(models.Model): |
||||
|
class Meta: |
||||
|
ordering = ['url'] |
||||
|
db_table = 'fotky_url_vazby' |
||||
|
verbose_name = u'vazba url a fotky do pozadí menu' |
||||
|
verbose_name_plural = u'vazby url a fotek do pozadí menu' |
||||
|
|
||||
|
url = models.CharField( |
||||
|
u'URL', blank=True, null=False, max_length=100, |
||||
|
help_text='url prefix stránek např: /archiv/ nebo /' |
||||
|
) |
||||
|
|
||||
|
fotka = models.ForeignKey( |
||||
|
FotkaHeader, blank=False, null=False, verbose_name='fotka', |
||||
|
on_delete=models.CASCADE |
||||
|
) |
||||
|
|
||||
|
DOBA_DEN = 'den' |
||||
|
DOBA_NOC = 'noc' |
||||
|
DOBA_OBOJI = 'oboji' |
||||
|
DOBA_CHOICES = [ |
||||
|
(DOBA_DEN, 'Zobrazit jen ve dne'), |
||||
|
(DOBA_NOC, 'Zobrazit jen v noci'), |
||||
|
(DOBA_OBOJI, 'Zobrazovat pořád')] |
||||
|
|
||||
|
denni_doba = models.CharField('denní doba', max_length=16, choices=DOBA_CHOICES, blank=False, default=DOBA_OBOJI) |
||||
|
|
||||
|
def __str__(self): |
||||
|
return self.url |
||||
|
|
||||
|
def url_fotky(self): |
||||
|
return self.fotka.fotka.url |
Loading…
Reference in new issue