Compare commits

...

2 commits

Author SHA1 Message Date
Pavel 'LEdoian' Turinsky
869048fe59 Oprava zkratky: "Fundamentální Algoritmus: Nežli Interagovat, Komunikuj"
Jen ranní myšlenka, tak ji píšu na nějaké ne úplně viditelné místo, ať
neprovokuju úplně moc :-)
2021-11-10 10:03:19 +01:00
Pavel 'LEdoian' Turinsky
dfcb1a234f 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…
2021-11-09 19:34:09 +01:00
9 changed files with 49 additions and 0 deletions

0
fanik/__init__.py Normal file
View file

3
fanik/admin.py Normal file
View file

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

5
fanik/apps.py Normal file
View file

@ -0,0 +1,5 @@
from django.apps import AppConfig
class FanikConfig(AppConfig):
name = 'fanik'

31
fanik/middleware.py Normal file
View 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

View file

3
fanik/models.py Normal file
View file

@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

3
fanik/tests.py Normal file
View file

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

3
fanik/views.py Normal file
View file

@ -0,0 +1,3 @@
from django.shortcuts import render
# Create your views here.

View file

@ -65,6 +65,7 @@ MIDDLEWARE = (
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
'fanik.middleware.fanik_middleware',
)
TEMPLATES = [