Compare commits
2 Commits
master
...
middleware
Author | SHA1 | Date |
---|---|---|
Pavel 'LEdoian' Turinsky | 869048fe59 | 3 years ago |
Pavel 'LEdoian' Turinsky | dfcb1a234f | 3 years ago |
9 changed files with 49 additions and 0 deletions
@ -0,0 +1,3 @@ |
|||||
|
from django.contrib import admin |
||||
|
|
||||
|
# Register your models here. |
@ -0,0 +1,5 @@ |
|||||
|
from django.apps import AppConfig |
||||
|
|
||||
|
|
||||
|
class FanikConfig(AppConfig): |
||||
|
name = 'fanik' |
@ -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,0 +1,3 @@ |
|||||
|
from django.db import models |
||||
|
|
||||
|
# Create your models here. |
@ -0,0 +1,3 @@ |
|||||
|
from django.test import TestCase |
||||
|
|
||||
|
# Create your tests here. |
@ -0,0 +1,3 @@ |
|||||
|
from django.shortcuts import render |
||||
|
|
||||
|
# Create your views here. |
Loading…
Reference in new issue