Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
5001CEM-auction/helpers.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
33 lines (28 sloc)
1.04 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import redirect, render_template, request, session, url_for | |
from functools import wraps | |
def login_required(f): | |
""" | |
Decorate routes to require login. | |
http://flask.pocoo.org/docs/0.11/patterns/viewdecorators/ | |
""" | |
@wraps(f) | |
def decorated_function(*args, **kwargs): | |
if session.get("user_id") is None: | |
return redirect(url_for("login", next=request.url)) | |
return f(*args, **kwargs) | |
return decorated_function | |
def apology(top="", bottom=""): | |
"""Renders message as an apology to user.""" | |
def escape(s): | |
""" | |
Escape special characters. | |
https://github.com/jacebrowning/memegen#special-characters | |
""" | |
for old, new in [("-", "--"), (" ", "-"), ("_", "__"), ("?", "~q"), | |
("%", "~p"), ("#", "~h"), ("/", "~s"), ("\"", "''")]: | |
s = s.replace(old, new) | |
return s | |
return render_template("apology.html", top=escape(top), bottom=escape(bottom)) | |
def usd(value): | |
"""Formats value as USD.""" | |
return "${:,.2f}".format(value) |