Nová aplikace: 'Fakt Ale Nebudeš Interagovat Kámo'
Jo, určitě to to znamená… Anyway, je to při troše štěstí elementární ukázka, jak se píše a používá middleware. Ne že by na tom bylo něco objevného… TODO: Aktuálně se nejde odhlásit, takže má člověk smůlu. Asi bych to neměl považovat za featuru, ale aktuálně si tím nejsem moc jistý. Pokud se to nenasadí na produkci, tak to nikomu vadit asi nebude…
This commit is contained in:
parent
46743f39d7
commit
dfcb1a234f
9 changed files with 49 additions and 0 deletions
0
fanik/__init__.py
Normal file
0
fanik/__init__.py
Normal file
3
fanik/admin.py
Normal file
3
fanik/admin.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
5
fanik/apps.py
Normal file
5
fanik/apps.py
Normal file
|
@ -0,0 +1,5 @@
|
|||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class FanikConfig(AppConfig):
|
||||
name = 'fanik'
|
31
fanik/middleware.py
Normal file
31
fanik/middleware.py
Normal file
|
@ -0,0 +1,31 @@
|
|||
from random import choices
|
||||
from django.shortcuts import render
|
||||
|
||||
def fanik_middleware(get_response):
|
||||
# Mapování uživatelských jmen na blbý kecy (+ váhy), obsah odkazu ap.
|
||||
config = {
|
||||
'fanik': {
|
||||
'kecy': [
|
||||
(r'<h1>Co je web?</h1><p>– 8. 11. 2021</p>', 10),
|
||||
(r'<h1>Mě to nezajímá.</h1><p>– 8. 11. 2021</p>', 5),
|
||||
],
|
||||
'link': r'<a href="mailto:web@mam.mff.cuni.cz?subject=Omluva%20webařům&body=Převelice%20se%20omlouvám.">Svůj postoj jsem přehodnotil</a>',
|
||||
},
|
||||
}
|
||||
|
||||
def middleware(request):
|
||||
if request.user.is_authenticated:
|
||||
username = request.user.username
|
||||
if username in config:
|
||||
kecy = config[username]['kecy']
|
||||
vahy = [x[1] for x in kecy]
|
||||
kec = choices([x[0] for x in kecy], weights=vahy, k=1)[0]
|
||||
template_name = 'universal.html'
|
||||
context = {}
|
||||
context['raw_html'] = kec + config[username]['link']
|
||||
return render(request,template_name,context)
|
||||
|
||||
# Ve všech ostatních případech vrátíme výsledek původního dotazu
|
||||
return get_response(request)
|
||||
|
||||
return middleware
|
0
fanik/migrations/__init__.py
Normal file
0
fanik/migrations/__init__.py
Normal file
3
fanik/models.py
Normal file
3
fanik/models.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.db import models
|
||||
|
||||
# Create your models here.
|
3
fanik/tests.py
Normal file
3
fanik/tests.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
3
fanik/views.py
Normal file
3
fanik/views.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.shortcuts import render
|
||||
|
||||
# Create your views here.
|
|
@ -65,6 +65,7 @@ MIDDLEWARE = (
|
|||
'django.contrib.messages.middleware.MessageMiddleware',
|
||||
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
||||
'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
|
||||
'fanik.middleware.fanik_middleware',
|
||||
)
|
||||
|
||||
TEMPLATES = [
|
||||
|
|
Loading…
Reference in a new issue