Skip to content

Commit 025589e

Browse files
1 parent 5b30983 commit 025589e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+3788
-3463
lines changed

examples/javascript/js_example/views.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
from js_example import app
44

55

6-
@app.route('/', defaults={'js': 'plain'})
7-
@app.route('/<any(plain, jquery, fetch):js>')
6+
@app.route("/", defaults={"js": "plain"})
7+
@app.route("/<any(plain, jquery, fetch):js>")
88
def index(js):
9-
return render_template('{0}.html'.format(js), js=js)
9+
return render_template("{0}.html".format(js), js=js)
1010

1111

12-
@app.route('/add', methods=['POST'])
12+
@app.route("/add", methods=["POST"])
1313
def add():
14-
a = request.form.get('a', 0, type=float)
15-
b = request.form.get('b', 0, type=float)
14+
a = request.form.get("a", 0, type=float)
15+
b = request.form.get("b", 0, type=float)
1616
return jsonify(result=a + b)

examples/javascript/setup.py

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,21 @@
22

33
from setuptools import find_packages, setup
44

5-
with io.open('README.rst', 'rt', encoding='utf8') as f:
5+
with io.open("README.rst", "rt", encoding="utf8") as f:
66
readme = f.read()
77

88
setup(
9-
name='js_example',
10-
version='1.0.0',
11-
url='http://flask.pocoo.org/docs/patterns/jquery/',
12-
license='BSD',
13-
maintainer='Pallets team',
14-
maintainer_email='[email protected]',
15-
description='Demonstrates making Ajax requests to Flask.',
9+
name="js_example",
10+
version="1.0.0",
11+
url="http://flask.pocoo.org/docs/patterns/jquery/",
12+
license="BSD",
13+
maintainer="Pallets team",
14+
maintainer_email="[email protected]",
15+
description="Demonstrates making Ajax requests to Flask.",
1616
long_description=readme,
1717
packages=find_packages(),
1818
include_package_data=True,
1919
zip_safe=False,
20-
install_requires=[
21-
'flask',
22-
],
23-
extras_require={
24-
'test': [
25-
'pytest',
26-
'coverage',
27-
'blinker',
28-
],
29-
},
20+
install_requires=["flask"],
21+
extras_require={"test": ["pytest", "coverage", "blinker"]},
3022
)

examples/javascript/tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from js_example import app
44

55

6-
@pytest.fixture(name='app')
6+
@pytest.fixture(name="app")
77
def fixture_app():
88
app.testing = True
99
yield app

examples/javascript/tests/test_js_example.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
from flask import template_rendered
44

55

6-
@pytest.mark.parametrize(('path', 'template_name'), (
7-
('/', 'plain.html'),
8-
('/plain', 'plain.html'),
9-
('/fetch', 'fetch.html'),
10-
('/jquery', 'jquery.html'),
11-
))
6+
@pytest.mark.parametrize(
7+
("path", "template_name"),
8+
(
9+
("/", "plain.html"),
10+
("/plain", "plain.html"),
11+
("/fetch", "fetch.html"),
12+
("/jquery", "jquery.html"),
13+
),
14+
)
1215
def test_index(app, client, path, template_name):
1316
def check(sender, template, context):
1417
assert template.name == template_name
@@ -17,12 +20,9 @@ def check(sender, template, context):
1720
client.get(path)
1821

1922

20-
@pytest.mark.parametrize(('a', 'b', 'result'), (
21-
(2, 3, 5),
22-
(2.5, 3, 5.5),
23-
(2, None, 2),
24-
(2, 'b', 2),
25-
))
23+
@pytest.mark.parametrize(
24+
("a", "b", "result"), ((2, 3, 5), (2.5, 3, 5.5), (2, None, 2), (2, "b", 2))
25+
)
2626
def test_add(client, a, b, result):
27-
response = client.post('/add', data={'a': a, 'b': b})
28-
assert response.get_json()['result'] == result
27+
response = client.post("/add", data={"a": a, "b": b})
28+
assert response.get_json()["result"] == result

examples/tutorial/flaskr/__init__.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ def create_app(test_config=None):
88
app = Flask(__name__, instance_relative_config=True)
99
app.config.from_mapping(
1010
# a default secret that should be overridden by instance config
11-
SECRET_KEY='dev',
11+
SECRET_KEY="dev",
1212
# store the database in the instance folder
13-
DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'),
13+
DATABASE=os.path.join(app.instance_path, "flaskr.sqlite"),
1414
)
1515

1616
if test_config is None:
1717
# load the instance config, if it exists, when not testing
18-
app.config.from_pyfile('config.py', silent=True)
18+
app.config.from_pyfile("config.py", silent=True)
1919
else:
2020
# load the test config if passed in
2121
app.config.update(test_config)
@@ -26,23 +26,25 @@ def create_app(test_config=None):
2626
except OSError:
2727
pass
2828

29-
@app.route('/hello')
29+
@app.route("/hello")
3030
def hello():
31-
return 'Hello, World!'
31+
return "Hello, World!"
3232

3333
# register the database commands
3434
from flaskr import db
35+
3536
db.init_app(app)
3637

3738
# apply the blueprints to the app
3839
from flaskr import auth, blog
40+
3941
app.register_blueprint(auth.bp)
4042
app.register_blueprint(blog.bp)
4143

4244
# make url_for('index') == url_for('blog.index')
4345
# in another app, you might define a separate main index here with
4446
# app.route, while giving the blog blueprint a url_prefix, but for
4547
# the tutorial the blog will be the main index
46-
app.add_url_rule('/', endpoint='index')
48+
app.add_url_rule("/", endpoint="index")
4749

4850
return app

examples/tutorial/flaskr/auth.py

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
11
import functools
22

33
from flask import (
4-
Blueprint, flash, g, redirect, render_template, request, session, url_for
4+
Blueprint,
5+
flash,
6+
g,
7+
redirect,
8+
render_template,
9+
request,
10+
session,
11+
url_for,
512
)
613
from werkzeug.security import check_password_hash, generate_password_hash
714

815
from flaskr.db import get_db
916

10-
bp = Blueprint('auth', __name__, url_prefix='/auth')
17+
bp = Blueprint("auth", __name__, url_prefix="/auth")
1118

1219

1320
def login_required(view):
1421
"""View decorator that redirects anonymous users to the login page."""
22+
1523
@functools.wraps(view)
1624
def wrapped_view(**kwargs):
1725
if g.user is None:
18-
return redirect(url_for('auth.login'))
26+
return redirect(url_for("auth.login"))
1927

2028
return view(**kwargs)
2129

@@ -26,83 +34,84 @@ def wrapped_view(**kwargs):
2634
def load_logged_in_user():
2735
"""If a user id is stored in the session, load the user object from
2836
the database into ``g.user``."""
29-
user_id = session.get('user_id')
37+
user_id = session.get("user_id")
3038

3139
if user_id is None:
3240
g.user = None
3341
else:
34-
g.user = get_db().execute(
35-
'SELECT * FROM user WHERE id = ?', (user_id,)
36-
).fetchone()
42+
g.user = (
43+
get_db().execute("SELECT * FROM user WHERE id = ?", (user_id,)).fetchone()
44+
)
3745

3846

39-
@bp.route('/register', methods=('GET', 'POST'))
47+
@bp.route("/register", methods=("GET", "POST"))
4048
def register():
4149
"""Register a new user.
4250
4351
Validates that the username is not already taken. Hashes the
4452
password for security.
4553
"""
46-
if request.method == 'POST':
47-
username = request.form['username']
48-
password = request.form['password']
54+
if request.method == "POST":
55+
username = request.form["username"]
56+
password = request.form["password"]
4957
db = get_db()
5058
error = None
5159

5260
if not username:
53-
error = 'Username is required.'
61+
error = "Username is required."
5462
elif not password:
55-
error = 'Password is required.'
56-
elif db.execute(
57-
'SELECT id FROM user WHERE username = ?', (username,)
58-
).fetchone() is not None:
59-
error = 'User {0} is already registered.'.format(username)
63+
error = "Password is required."
64+
elif (
65+
db.execute("SELECT id FROM user WHERE username = ?", (username,)).fetchone()
66+
is not None
67+
):
68+
error = "User {0} is already registered.".format(username)
6069

6170
if error is None:
6271
# the name is available, store it in the database and go to
6372
# the login page
6473
db.execute(
65-
'INSERT INTO user (username, password) VALUES (?, ?)',
66-
(username, generate_password_hash(password))
74+
"INSERT INTO user (username, password) VALUES (?, ?)",
75+
(username, generate_password_hash(password)),
6776
)
6877
db.commit()
69-
return redirect(url_for('auth.login'))
78+
return redirect(url_for("auth.login"))
7079

7180
flash(error)
7281

73-
return render_template('auth/register.html')
82+
return render_template("auth/register.html")
7483

7584

76-
@bp.route('/login', methods=('GET', 'POST'))
85+
@bp.route("/login", methods=("GET", "POST"))
7786
def login():
7887
"""Log in a registered user by adding the user id to the session."""
79-
if request.method == 'POST':
80-
username = request.form['username']
81-
password = request.form['password']
88+
if request.method == "POST":
89+
username = request.form["username"]
90+
password = request.form["password"]
8291
db = get_db()
8392
error = None
8493
user = db.execute(
85-
'SELECT * FROM user WHERE username = ?', (username,)
94+
"SELECT * FROM user WHERE username = ?", (username,)
8695
).fetchone()
8796

8897
if user is None:
89-
error = 'Incorrect username.'
90-
elif not check_password_hash(user['password'], password):
91-
error = 'Incorrect password.'
98+
error = "Incorrect username."
99+
elif not check_password_hash(user["password"], password):
100+
error = "Incorrect password."
92101

93102
if error is None:
94103
# store the user id in a new session and return to the index
95104
session.clear()
96-
session['user_id'] = user['id']
97-
return redirect(url_for('index'))
105+
session["user_id"] = user["id"]
106+
return redirect(url_for("index"))
98107

99108
flash(error)
100109

101-
return render_template('auth/login.html')
110+
return render_template("auth/login.html")
102111

103112

104-
@bp.route('/logout')
113+
@bp.route("/logout")
105114
def logout():
106115
"""Clear the current session, including the stored user id."""
107116
session.clear()
108-
return redirect(url_for('index'))
117+
return redirect(url_for("index"))

0 commit comments

Comments
 (0)