Add decorators.give_theme_cookie to decouple from render_template.
I realized it was not appropriate for render_template to be handling this responsibility, but previously did not think of another way. Now I did.
This commit is contained in:
parent
1554b86c11
commit
7f8a007e2d
2 changed files with 26 additions and 15 deletions
|
@ -66,6 +66,7 @@ def decorate_and_route(*route_args, **route_kwargs):
|
||||||
# same one multiple times. The _fully_decorated will track that.
|
# same one multiple times. The _fully_decorated will track that.
|
||||||
if not hasattr(endpoint, '_fully_decorated'):
|
if not hasattr(endpoint, '_fully_decorated'):
|
||||||
endpoint = flasktools.ensure_response_type(endpoint)
|
endpoint = flasktools.ensure_response_type(endpoint)
|
||||||
|
endpoint = decorators.give_theme_cookie(endpoint)
|
||||||
endpoint = decorators.catch_etiquette_exception(endpoint)
|
endpoint = decorators.catch_etiquette_exception(endpoint)
|
||||||
endpoint = session_manager.give_token(endpoint)
|
endpoint = session_manager.give_token(endpoint)
|
||||||
|
|
||||||
|
@ -162,10 +163,7 @@ def back_url():
|
||||||
|
|
||||||
def render_template(request, template_name, **kwargs):
|
def render_template(request, template_name, **kwargs):
|
||||||
session = session_manager.get(request)
|
session = session_manager.get(request)
|
||||||
|
theme = request.cookies.get('etiquette_theme', None)
|
||||||
old_theme = request.cookies.get('etiquette_theme', None)
|
|
||||||
new_theme = request.args.get('theme', None)
|
|
||||||
theme = new_theme or old_theme or 'slate'
|
|
||||||
|
|
||||||
response = flask.render_template(
|
response = flask.render_template(
|
||||||
template_name,
|
template_name,
|
||||||
|
@ -174,17 +172,6 @@ def render_template(request, template_name, **kwargs):
|
||||||
theme=theme,
|
theme=theme,
|
||||||
**kwargs,
|
**kwargs,
|
||||||
)
|
)
|
||||||
|
|
||||||
if not isinstance(response, flasktools.RESPONSE_TYPES):
|
|
||||||
response = flask.Response(response)
|
|
||||||
|
|
||||||
if new_theme is None:
|
|
||||||
pass
|
|
||||||
elif new_theme == '':
|
|
||||||
response.set_cookie('etiquette_theme', value='', expires=0)
|
|
||||||
elif new_theme != old_theme:
|
|
||||||
response.set_cookie('etiquette_theme', value=new_theme, expires=2147483647)
|
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
def send_file(filepath, override_mimetype=None):
|
def send_file(filepath, override_mimetype=None):
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
import flask; from flask import request
|
import flask; from flask import request
|
||||||
import functools
|
import functools
|
||||||
import time
|
import time
|
||||||
|
import werkzeug.datastructures
|
||||||
|
|
||||||
from voussoirkit import dotdict
|
from voussoirkit import dotdict
|
||||||
|
from voussoirkit import flasktools
|
||||||
from voussoirkit import passwordy
|
from voussoirkit import passwordy
|
||||||
from voussoirkit import sentinel
|
from voussoirkit import sentinel
|
||||||
|
|
||||||
|
@ -107,6 +109,28 @@ def catch_etiquette_exception(function):
|
||||||
flask.abort(response)
|
flask.abort(response)
|
||||||
return wrapped
|
return wrapped
|
||||||
|
|
||||||
|
def give_theme_cookie(function):
|
||||||
|
@functools.wraps(function)
|
||||||
|
def wrapped(*args, **kwargs):
|
||||||
|
old_theme = request.cookies.get('etiquette_theme', None)
|
||||||
|
new_theme = request.args.get('theme', None)
|
||||||
|
theme = new_theme or old_theme or 'slate'
|
||||||
|
|
||||||
|
request.cookies = werkzeug.datastructures.MultiDict(request.cookies)
|
||||||
|
request.cookies['etiquette_theme'] = theme
|
||||||
|
|
||||||
|
response = function(*args, **kwargs)
|
||||||
|
|
||||||
|
if new_theme is None:
|
||||||
|
pass
|
||||||
|
elif new_theme == '':
|
||||||
|
response.set_cookie('etiquette_theme', value='', expires=0)
|
||||||
|
elif new_theme != old_theme:
|
||||||
|
response.set_cookie('etiquette_theme', value=new_theme, expires=2147483647)
|
||||||
|
|
||||||
|
return response
|
||||||
|
return wrapped
|
||||||
|
|
||||||
def required_fields(fields, forbid_whitespace=False):
|
def required_fields(fields, forbid_whitespace=False):
|
||||||
'''
|
'''
|
||||||
Declare that the endpoint requires certain POST body fields. Without them,
|
Declare that the endpoint requires certain POST body fields. Without them,
|
||||||
|
|
Loading…
Reference in a new issue