2011-09-11 05:25:42 +02:00
|
|
|
import re
|
2011-09-11 22:11:36 +02:00
|
|
|
import inspect
|
|
|
|
|
2015-08-20 13:45:53 +02:00
|
|
|
from jinja2 import filters
|
2012-03-06 22:14:29 +01:00
|
|
|
from flask import redirect
|
2011-08-21 22:35:01 +02:00
|
|
|
from werkzeug.routing import HTTPException, RoutingException
|
2011-07-23 18:45:40 +02:00
|
|
|
|
2012-03-06 22:14:29 +01:00
|
|
|
|
2011-09-11 05:25:42 +02:00
|
|
|
def slugify(value):
|
|
|
|
"""Normalizes string, converts to lowercase, removes non-alpha characters,
|
|
|
|
and converts spaces to hyphens.
|
2011-07-23 18:45:40 +02:00
|
|
|
|
2011-09-11 05:25:42 +02:00
|
|
|
Copy/Pasted from ametaireau/pelican/utils itself took from django sources.
|
2011-08-10 19:47:06 +02:00
|
|
|
"""
|
2011-09-11 05:25:42 +02:00
|
|
|
if type(value) == unicode:
|
|
|
|
import unicodedata
|
|
|
|
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore')
|
|
|
|
value = unicode(re.sub('[^\w\s-]', '', value).strip().lower())
|
|
|
|
return re.sub('[-\s]+', '-', value)
|
2011-07-23 18:45:40 +02:00
|
|
|
|
2011-10-08 13:22:18 +02:00
|
|
|
|
2011-08-21 22:35:01 +02:00
|
|
|
class Redirect303(HTTPException, RoutingException):
|
|
|
|
"""Raise if the map requests a redirect. This is for example the case if
|
|
|
|
`strict_slashes` are activated and an url that requires a trailing slash.
|
2011-07-23 18:45:40 +02:00
|
|
|
|
2011-08-21 22:35:01 +02:00
|
|
|
The attribute `new_url` contains the absolute destination url.
|
2011-07-23 18:45:40 +02:00
|
|
|
"""
|
2011-08-21 22:35:01 +02:00
|
|
|
code = 303
|
2011-07-23 18:45:40 +02:00
|
|
|
|
2011-08-21 22:35:01 +02:00
|
|
|
def __init__(self, new_url):
|
|
|
|
RoutingException.__init__(self, new_url)
|
|
|
|
self.new_url = new_url
|
|
|
|
|
|
|
|
def get_response(self, environ):
|
|
|
|
return redirect(self.new_url, 303)
|
2015-08-19 22:47:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
class PrefixedWSGI(object):
|
|
|
|
'''
|
|
|
|
Wrap the application in this middleware and configure the
|
|
|
|
front-end server to add these headers, to let you quietly bind
|
|
|
|
this to a URL other than / and to an HTTP scheme that is
|
|
|
|
different than what is used locally.
|
|
|
|
|
|
|
|
It relies on "APPLICATION_ROOT" app setting.
|
|
|
|
|
|
|
|
Inspired from http://flask.pocoo.org/snippets/35/
|
|
|
|
|
|
|
|
:param app: the WSGI application
|
|
|
|
'''
|
|
|
|
def __init__(self, app):
|
|
|
|
self.app = app
|
|
|
|
self.wsgi_app = app.wsgi_app
|
|
|
|
|
|
|
|
def __call__(self, environ, start_response):
|
|
|
|
script_name = self.app.config['APPLICATION_ROOT']
|
|
|
|
if script_name:
|
|
|
|
environ['SCRIPT_NAME'] = script_name
|
|
|
|
path_info = environ['PATH_INFO']
|
|
|
|
if path_info.startswith(script_name):
|
|
|
|
environ['PATH_INFO'] = path_info[len(script_name):]
|
|
|
|
|
|
|
|
scheme = environ.get('HTTP_X_SCHEME', '')
|
|
|
|
if scheme:
|
|
|
|
environ['wsgi.url_scheme'] = scheme
|
|
|
|
return self.wsgi_app(environ, start_response)
|
2015-08-20 13:45:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
def minimal_round(*args, **kw):
|
|
|
|
""" Jinja2 filter: rounds, but display only non-zero decimals
|
|
|
|
|
|
|
|
from http://stackoverflow.com/questions/28458524/
|
|
|
|
"""
|
|
|
|
# Use the original round filter, to deal with the extra arguments
|
|
|
|
res = filters.do_round(*args, **kw)
|
|
|
|
# Test if the result is equivalent to an integer and
|
|
|
|
# return depending on it
|
|
|
|
ires = int(res)
|
|
|
|
return (res if res != ires else ires)
|