diff --git a/setup.py b/setup.py index 9acd25b..c092008 100644 --- a/setup.py +++ b/setup.py @@ -16,5 +16,5 @@ package_dir={"": "src"}, packages=["cs50"], url="https://github.com/cs50/python-cs50", - version="3.0.0" + version="3.0.1" ) diff --git a/src/cs50/flask.py b/src/cs50/flask.py index ad79283..c7ed023 100644 --- a/src/cs50/flask.py +++ b/src/cs50/flask.py @@ -1,4 +1,5 @@ from distutils.version import StrictVersion +from os import getenv from pkg_resources import get_distribution from .cs50 import formatException @@ -9,12 +10,29 @@ # Only patch >= 1.0 version = StrictVersion(get_distribution("flask").version) assert version >= StrictVersion("1.0") - import flask.logging # Reformat logger's exceptions # http://flask.pocoo.org/docs/1.0/logging/ # https://docs.python.org/3/library/logging.html#logging.Formatter.formatException - flask.logging.default_handler.formatter.formatException = lambda exc_info: formatException(*exc_info) + try: + import flask.logging + flask.logging.default_handler.formatter.formatException = lambda exc_info: formatException(*exc_info) + except: + pass + + # Add support for Cloud9 proxy so that flask.redirect doesn't redirect from HTTPS to HTTP + # http://stackoverflow.com/a/23504684/5156190 + if getenv("C9_HOSTNAME") and not getenv("IDE_OFFLINE"): + try: + import flask + from werkzeug.contrib.fixers import ProxyFix + before = flask.Flask.__init__ + def after(self, *args, **kwargs): + before(self, *args, **kwargs) + self.wsgi_app = ProxyFix(self.wsgi_app) + flask.Flask.__init__ = after + except: + pass except: pass diff --git a/tests/redirect/application.py b/tests/redirect/application.py new file mode 100644 index 0000000..6aff187 --- /dev/null +++ b/tests/redirect/application.py @@ -0,0 +1,12 @@ +import cs50 +from flask import Flask, redirect, render_template + +app = Flask(__name__) + +@app.route("/") +def index(): + return redirect("/foo") + +@app.route("/foo") +def foo(): + return render_template("foo.html") diff --git a/tests/redirect/templates/foo.html b/tests/redirect/templates/foo.html new file mode 100644 index 0000000..257cc56 --- /dev/null +++ b/tests/redirect/templates/foo.html @@ -0,0 +1 @@ +foo