2021-01-05 20:43:39 +00:00
|
|
|
import flask; from flask import request
|
2017-05-02 04:23:16 +00:00
|
|
|
import functools
|
2021-06-05 04:29:23 +00:00
|
|
|
import werkzeug.datastructures
|
2021-01-05 20:43:39 +00:00
|
|
|
|
2021-06-05 04:29:23 +00:00
|
|
|
from voussoirkit import flasktools
|
2017-05-02 04:23:16 +00:00
|
|
|
|
2018-01-10 05:21:15 +00:00
|
|
|
import etiquette
|
|
|
|
|
|
|
|
def catch_etiquette_exception(function):
|
|
|
|
'''
|
|
|
|
If an EtiquetteException is raised, automatically catch it and convert it
|
2018-09-23 21:57:25 +00:00
|
|
|
into a json response so that the user isn't receiving error 500.
|
2018-01-10 05:21:15 +00:00
|
|
|
'''
|
|
|
|
@functools.wraps(function)
|
|
|
|
def wrapped(*args, **kwargs):
|
|
|
|
try:
|
|
|
|
return function(*args, **kwargs)
|
2020-09-19 10:08:45 +00:00
|
|
|
except etiquette.exceptions.EtiquetteException as exc:
|
|
|
|
if isinstance(exc, etiquette.exceptions.NoSuch):
|
2018-01-10 05:21:15 +00:00
|
|
|
status = 404
|
|
|
|
else:
|
|
|
|
status = 400
|
2021-01-01 20:56:05 +00:00
|
|
|
response = exc.jsonify()
|
2021-10-02 06:05:46 +00:00
|
|
|
response = flasktools.json_response(response, status=status)
|
2018-01-10 05:21:15 +00:00
|
|
|
flask.abort(response)
|
|
|
|
return wrapped
|
|
|
|
|
2021-06-05 04:29:23 +00:00
|
|
|
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
|