Skip to content

Commit d5c0a2d

Browse files
committed
Updated README, formatting.
1 parent a113441 commit d5c0a2d

File tree

12 files changed

+126
-553
lines changed

12 files changed

+126
-553
lines changed

Makefile

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ make clean - Remove cached files and lock files.
1212
endef
1313
export HELP
1414

15-
.PHONY: run restart deploy update clean help
15+
.PHONY: run deploy update format clean help
1616

1717

1818
requirements: .requirements.txt
19+
env: .venv/bin/activate
1920

2021

2122
.requirements.txt: requirements.txt
@@ -27,8 +28,8 @@ all help:
2728

2829

2930
.PHONY: run
30-
run:
31-
$(shell . .venv/bin/activate && python3 wsgi.py)
31+
run: env
32+
$(shell . .venv/bin/activate && flask run)
3233

3334

3435
.PHONY: deploy
@@ -37,17 +38,16 @@ deploy:
3738

3839

3940
.PHONY: update
40-
update:
41-
poetry shell && poetry update
42-
pip freeze > requirements.txt
43-
exit
41+
update: env
42+
.venv/bin/python3 -m pip install -U pip
43+
poetry update
44+
poetry export -f requirements.txt --output requirements.txt --without-hashes
4445

4546

4647
.PHONY: format
47-
format: requirements
48-
$(shell . .venv/bin/activate)
49-
$(shell isort -rc ./)
50-
$(shell black ./)
48+
format: env
49+
$(shell . .venv/bin/activate && isort -rc ./)
50+
$(shell . .venv/bin/activate && black ./)
5151

5252

5353
.PHONY: clean

Pipfile

Lines changed: 0 additions & 25 deletions
This file was deleted.

Pipfile.lock

Lines changed: 0 additions & 340 deletions
This file was deleted.

README.md

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -16,59 +16,39 @@
1616

1717
![Flask Session Redis Tutorial](https://github.com/hackersandslackers/flask-session-tutorial/blob/master/.github/flask-session2@2x.jpg?raw=true)
1818

19-
Source code for the accompanying tutorial found here: https://hackersandslackers.com/managing-user-session-variables-with-flask-sessions-and-redis/
19+
**Tutorial**: https://hackersandslackers.com/managing-user-session-variables-with-flask-sessions-and-redis/
2020

21+
# Getting Started
2122

22-
## Installation
23+
Get set up locally in two steps:
2324

24-
**Installation via `requirements.txt`**:
25-
26-
```shell
27-
$ git clone https://github.com/hackersandslackers/flask-session-tutorial.git
28-
$ cd flask-session-tutorial
29-
$ python3 -m venv myenv
30-
$ source myenv/bin/activate
31-
$ pip3 install -r requirements.txt
32-
$ flask run
33-
```
34-
35-
**Installation via [Pipenv](https://pipenv-fork.readthedocs.io/en/latest/)**:
36-
37-
```shell
38-
$ git clone https://github.com/hackersandslackers/flask-session-tutorial.git
39-
$ cd flask-session-tutorial
40-
$ pipenv shell
41-
$ pipenv update
42-
$ flask run
43-
```
44-
45-
**Installation via [Poetry](https://python-poetry.org/)**:
46-
47-
```shell
48-
$ git clone https://github.com/hackersandslackers/flask-session-tutorial.git
49-
$ cd flask-session-tutorial
50-
$ poetry shell
51-
$ poetry update
52-
$ poetry run
53-
```
54-
55-
## Usage
25+
### Environment Variables
5626

5727
Replace the values in **.env.example** with your values and rename this file to **.env**:
5828

59-
* `FLASK_APP`: Entry point of your application (should be `wsgi.py`).
60-
* `FLASK_ENV`: The environment to run your app in (either `development` or `production`).
29+
* `FLASK_APP`: Entry point of your application; should be `wsgi.py`.
30+
* `FLASK_ENV`: The environment in which to run your application; either `development` or `production`.
6131
* `SECRET_KEY`: Randomly generated string of characters used to encrypt your app's data.
6232
* `SQLALCHEMY_DATABASE_URI`: Connection URI of a SQL database.
6333
* `SESSION_REDIS`: Connection URI of a Redis instance.
64-
* `LESS_BIN`: Path to your local LESS installation via `which lessc` (optional for static assets).
65-
* `ASSETS_DEBUG`: Debug asset creation and bundling in `development` (optional).
66-
* `LESS_RUN_IN_DEBUG`: Debug LESS while in `development` (optional).
67-
* `COMPRESSOR_DEBUG`: Debug asset compression while in `development` (optional).
34+
* `LESS_BIN` *(optional for static assets)*: Path to your local LESS installation via `which lessc`.
35+
* `ASSETS_DEBUG` *(optional)*: Debug asset creation and bundling in `development`.
36+
* `LESS_RUN_IN_DEBUG` *(optional)*: Debug LESS while in `development`.
37+
* `COMPRESSOR_DEBUG` *(optional)*: Debug asset compression while in `development`.
6838

6939

7040
*Remember never to commit secrets saved in .env files to Github.*
7141

42+
### Installation
43+
44+
Get up and running with `make deploy`:
45+
46+
```shell
47+
$ git clone https://github.com/hackersandslackers/flask-session-tutorial.git
48+
$ cd flask-session-tutorial
49+
$ make deploy
50+
```
51+
7252
-----
7353

74-
**Hackers and Slackers** tutorials are free of charge. If you found this tutorial helpful, a [small donation](https://www.buymeacoffee.com/hackersslackers) would be greatly appreciated to keep us in business. All proceeds go towards coffee, and all coffee goes towards more content.
54+
**Hackers and Slackers** tutorials are free of charge. If you found this tutorial helpful, a [small donation](https://www.buymeacoffee.com/hackersslackers) would be greatly appreciated to keep us in business. All proceeds go towards coffee, and all coffee goes towards more content.

config.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,33 @@
55
from dotenv import load_dotenv
66

77
basedir = path.abspath(path.dirname(__file__))
8-
load_dotenv(path.join(basedir, '.env'))
8+
load_dotenv(path.join(basedir, ".env"))
99

1010

1111
class Config:
1212
"""Set Flask configuration variables from .env file."""
1313

1414
# General Config
15-
FLASK_APP = environ.get('FLASK_APP')
16-
FLASK_ENV = environ.get('FLASK_ENV')
17-
SECRET_KEY = environ.get('SECRET_KEY')
15+
FLASK_APP = environ.get("FLASK_APP")
16+
FLASK_ENV = environ.get("FLASK_ENV")
17+
SECRET_KEY = environ.get("SECRET_KEY")
1818

1919
# Flask-Session
20-
REDIS_URI = environ.get('SESSION_REDIS')
21-
SESSION_TYPE = 'redis'
20+
REDIS_URI = environ.get("SESSION_REDIS")
21+
SESSION_TYPE = "redis"
2222
SESSION_REDIS = redis.from_url(REDIS_URI)
2323

2424
# Flask-Assets
25-
LESS_BIN = environ.get('LESS_BIN')
25+
LESS_BIN = environ.get("LESS_BIN")
2626
ASSETS_DEBUG = False
2727
LESS_RUN_IN_DEBUG = False
2828

2929
# Static Assets
30-
STATIC_FOLDER = 'static'
31-
TEMPLATES_FOLDER = 'templates'
30+
STATIC_FOLDER = "static"
31+
TEMPLATES_FOLDER = "templates"
3232
COMPRESSOR_DEBUG = True
3333

3434
# Flask-SQLAlchemy
35-
SQLALCHEMY_DATABASE_URI = environ.get('SQLALCHEMY_DATABASE_URI')
35+
SQLALCHEMY_DATABASE_URI = environ.get("SQLALCHEMY_DATABASE_URI")
3636
SQLALCHEMY_TRACK_MODIFICATIONS = False
3737
SQLALCHEMY_ECHO = True

flask_session_tutorial/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
def create_app():
1313
"""Construct the core flask_session_tutorial."""
1414
app = Flask(__name__, instance_relative_config=False)
15-
app.config.from_object('config.Config')
15+
app.config.from_object("config.Config")
1616

1717
# Initialize Plugins
1818
db.init_app(app)
@@ -23,6 +23,7 @@ def create_app():
2323
from . import routes
2424
from . import auth
2525
from .assets import compile_static_assets, compile_auth_assets
26+
2627
app.register_blueprint(routes.main_bp)
2728
app.register_blueprint(auth.auth_bp)
2829

flask_session_tutorial/assets.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,18 @@ def compile_auth_assets(app):
99
Environment.debug = False
1010
# Stylesheets Bundle
1111
less_bundle = Bundle(
12-
'src/less/account.less',
13-
filters='less,cssmin',
14-
output='dist/css/account.css',
15-
extra={'rel': 'stylesheet/less'}
12+
"src/less/account.less",
13+
filters="less,cssmin",
14+
output="dist/css/account.css",
15+
extra={"rel": "stylesheet/less"},
1616
)
1717
# JavaScript Bundle
18-
js_bundle = Bundle(
19-
'src/js/main.js',
20-
filters='jsmin',
21-
output='dist/js/main.min.js'
22-
)
18+
js_bundle = Bundle("src/js/main.js", filters="jsmin", output="dist/js/main.min.js")
2319
# Register assets
24-
assets.register('less_all', less_bundle)
25-
assets.register('js_all', js_bundle)
20+
assets.register("less_all", less_bundle)
21+
assets.register("js_all", js_bundle)
2622
# Build assets in development mode
27-
if app.config['FLASK_ENV'] != 'production':
23+
if app.config["FLASK_ENV"] != "production":
2824
less_bundle.build(force=True)
2925
js_bundle.build()
3026

@@ -36,15 +32,15 @@ def compile_main_assets(app):
3632
Environment.debug = False
3733
# Stylesheets Bundle
3834
less_bundle = Bundle(
39-
'src/less/dashboard.less',
40-
filters='less,cssmin',
41-
output='dist/css/dashboard.css',
42-
extra={'rel': 'stylesheet/less'}
35+
"src/less/dashboard.less",
36+
filters="less,cssmin",
37+
output="dist/css/dashboard.css",
38+
extra={"rel": "stylesheet/less"},
4339
)
4440
# Register assets
45-
assets.register('less_all', less_bundle)
41+
assets.register("less_all", less_bundle)
4642
# Build assets in development mode
47-
if app.config['FLASK_ENV'] != 'production':
43+
if app.config["FLASK_ENV"] != "production":
4844
less_bundle.build(force=True)
4945

5046

flask_session_tutorial/auth.py

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@
1111

1212
# Blueprint Configuration
1313
auth_bp = Blueprint(
14-
'auth_bp', __name__,
15-
template_folder='templates',
16-
static_folder='static'
14+
"auth_bp", __name__, template_folder="templates", static_folder="static"
1715
)
1816

1917

20-
@auth_bp.route('/signup', methods=['GET', 'POST'])
18+
@auth_bp.route("/signup", methods=["GET", "POST"])
2119
def signup():
2220
"""
2321
Sign-up form to create new user accounts.
@@ -29,51 +27,51 @@ def signup():
2927
existing_user = User.query.filter_by(email=form.email.data).first()
3028
if existing_user is None:
3129
user = User(
32-
name=form.name.data,
33-
email=form.email.data,
34-
website=form.website.data
30+
name=form.name.data, email=form.email.data, website=form.website.data
3531
)
3632
user.set_password(form.password.data)
3733
db.session.add(user)
3834
db.session.commit() # Create new user
3935
login_user(user) # Log in as newly created user
4036
print(user)
41-
return redirect(url_for('main_bp.dashboard'))
42-
flash('A user already exists with that email address.')
37+
return redirect(url_for("main_bp.dashboard"))
38+
flash("A user already exists with that email address.")
4339
return render_template(
44-
'signup.jinja2',
45-
title='Create an Account.',
40+
"signup.jinja2",
41+
title="Create an Account.",
4642
form=form,
47-
template='signup-page',
48-
body="Sign up for a user account."
43+
template="signup-page",
44+
body="Sign up for a user account.",
4945
)
5046

5147

52-
@auth_bp.route('/login', methods=['GET', 'POST'])
48+
@auth_bp.route("/login", methods=["GET", "POST"])
5349
def login():
5450
"""
5551
Log-in page for registered users.
5652
GET: Serve Log-in page.
5753
POST: Validate form and redirect user to dashboard.
5854
"""
5955
if current_user.is_authenticated:
60-
return redirect(url_for('main_bp.dashboard')) # Bypass if user is logged in
56+
return redirect(url_for("main_bp.dashboard")) # Bypass if user is logged in
6157

6258
form = LoginForm()
6359
if form.validate_on_submit():
64-
user = User.query.filter_by(email=form.email.data).first() # Validate Login Attempt
60+
user = User.query.filter_by(
61+
email=form.email.data
62+
).first() # Validate Login Attempt
6563
if user and user.check_password(password=form.password.data):
6664
login_user(user)
67-
next_page = request.args.get('next')
68-
return redirect(next_page or url_for('main_bp.dashboard'))
69-
flash('Invalid username/password combination')
70-
return redirect(url_for('auth_bp.login'))
65+
next_page = request.args.get("next")
66+
return redirect(next_page or url_for("main_bp.dashboard"))
67+
flash("Invalid username/password combination")
68+
return redirect(url_for("auth_bp.login"))
7169
return render_template(
72-
'login.jinja2',
70+
"login.jinja2",
7371
form=form,
74-
title='Log in.',
75-
template='login-page',
76-
body="Log in with your User account."
72+
title="Log in.",
73+
template="login-page",
74+
body="Log in with your User account.",
7775
)
7876

7977

@@ -88,5 +86,5 @@ def load_user(user_id):
8886
@login_manager.unauthorized_handler
8987
def unauthorized():
9088
"""Redirect unauthorized users to Login page."""
91-
flash('You must be logged in to view that page.')
92-
return redirect(url_for('auth_bp.login'))
89+
flash("You must be logged in to view that page.")
90+
return redirect(url_for("auth_bp.login"))

0 commit comments

Comments
 (0)