Skip to content

Commit 7b8e255

Browse files
committed
Makeffile, formatting.
1 parent 4ed4f8f commit 7b8e255

File tree

12 files changed

+375
-164
lines changed

12 files changed

+375
-164
lines changed

Makefile

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
SRCPATH := $(CURDIR)
2+
PROJECTNAME := $(shell basename $(CURDIR))
3+
4+
define HELP
5+
Manage $(PROJECTNAME). Usage:
6+
7+
make run - Run $(PROJECTNAME).
8+
make deploy - Install requirements and run app for the first time.
9+
make update - Update pip dependencies via Python Poetry.
10+
make format - Format code with Python's `Black` library.
11+
make clean - Remove cached files and lock files.
12+
endef
13+
export HELP
14+
15+
.PHONY: run deploy update format clean help
16+
17+
18+
requirements: .requirements.txt
19+
env: .venv/bin/activate
20+
21+
22+
.requirements.txt: requirements.txt
23+
$(shell . .venv/bin/activate && pip install -r requirements.txt)
24+
25+
26+
all help:
27+
@echo "$$HELP"
28+
29+
30+
.PHONY: run
31+
run: env
32+
$(shell . .venv/bin/activate && flask run)
33+
34+
35+
.PHONY: deploy
36+
deploy:
37+
$(shell . ./deploy.sh)
38+
39+
40+
.PHONY: update
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
45+
46+
47+
.PHONY: format
48+
format: env
49+
$(shell . .venv/bin/activate && isort -rc ./)
50+
$(shell . .venv/bin/activate && black ./)
51+
52+
53+
.PHONY: clean
54+
clean:
55+
find . -name '*.pyc' -delete
56+
find . -name '__pycache__' -delete
57+
find . -name 'poetry.lock' -delete
58+
find . -name 'Pipefile.lock' -delete

README.md

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,50 +13,30 @@
1313
Source code for the accompanying tutorial found here: https://hackersandslackers.com/flask-wtforms-forms/
1414

1515

16-
## Installation
16+
# Getting Started
1717

18-
**Installation via `requirements.txt`**:
18+
Get set up locally in two steps:
1919

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

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

5324
* `FLASK_APP`: Entry point of your application (should be `wsgi.py`).
5425
* `FLASK_ENV`: The environment to run your app in (either `development` or `production`).
5526
* `SECRET_KEY`: Randomly generated string of characters used to encrypt your app's data.
5627

57-
5828
*Remember never to commit secrets saved in .env files to Github.*
5929

30+
### Installation
31+
32+
Get up and running with `make deploy`:
33+
34+
```shell
35+
$ git clone https://github.com/hackersandslackers/flask-wtform-tutorial.git
36+
$ cd flask-wtform-tutorial
37+
$ make deploy
38+
```
39+
6040
-----
6141

6242
**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: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
"""App configuration."""
22
from os import environ, path
3+
34
from dotenv import load_dotenv
45

56
# Load variables from .env
67
basedir = path.abspath(path.dirname(__file__))
7-
load_dotenv(path.join(basedir, '.env'))
8+
load_dotenv(path.join(basedir, ".env"))
89

910

1011
class Config:
1112
"""Set Flask configuration vars from .env file."""
1213

1314
# General Config
14-
SECRET_KEY = environ.get('SECRET_KEY')
15-
FLASK_APP = environ.get('FLASK_APP')
16-
FLASK_ENV = environ.get('FLASK_ENV')
15+
SECRET_KEY = environ.get("SECRET_KEY")
16+
FLASK_APP = environ.get("FLASK_APP")
17+
FLASK_ENV = environ.get("FLASK_ENV")
1718

1819
# Static Assets
19-
STATIC_FOLDER = 'static'
20-
TEMPLATES_FOLDER = 'templates'
20+
STATIC_FOLDER = "static"
21+
TEMPLATES_FOLDER = "templates"

deploy.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
3+
if [ -d ".venv" ]
4+
then
5+
source .venv/bin/activate
6+
pip install -r requirements.txt
7+
python3 wsgi.py
8+
else
9+
python3 -m venv .venv
10+
source .venv/bin/activate
11+
python3 -m pip install --upgrade pip
12+
pip install -r requirements.txt
13+
python3 wsgi.py
14+
fi

flask_wtforms_tutorial/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
def create_app():
66
"""Construct the core flask_wtforms_tutorial."""
77
app = Flask(__name__, instance_relative_config=False)
8-
app.config.from_object('config.Config')
9-
app.config['RECAPTCHA_PUBLIC_KEY'] = 'iubhiukfgjbkhfvgkdfm'
10-
app.config['RECAPTCHA_PARAMETERS'] = {'size': '100%'}
8+
app.config.from_object("config.Config")
9+
app.config["RECAPTCHA_PUBLIC_KEY"] = "iubhiukfgjbkhfvgkdfm"
10+
app.config["RECAPTCHA_PARAMETERS"] = {"size": "100%"}
1111

1212
with app.app_context():
1313
# Import parts of our flask_wtforms_tutorial

flask_wtforms_tutorial/forms.py

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,57 @@
11
"""Form class declaration."""
22
from flask_wtf import FlaskForm, RecaptchaField
3-
from wtforms import (StringField,
4-
TextAreaField,
5-
SubmitField,
6-
PasswordField,
7-
DateField,
8-
SelectField)
9-
from wtforms.validators import (DataRequired,
10-
Email,
11-
EqualTo,
12-
Length,
13-
URL)
3+
from wtforms import (
4+
DateField,
5+
PasswordField,
6+
SelectField,
7+
StringField,
8+
SubmitField,
9+
TextAreaField,
10+
)
11+
from wtforms.validators import URL, DataRequired, Email, EqualTo, Length
1412

1513

1614
class ContactForm(FlaskForm):
1715
"""Contact form."""
1816

19-
name = StringField('Name', [
20-
DataRequired()])
21-
email = StringField('Email', [
22-
Email(message='Not a valid email address.'),
23-
DataRequired()])
24-
body = TextAreaField('Message', [
25-
DataRequired(),
26-
Length(min=4, message='Your message is too short.')])
27-
submit = SubmitField('Submit')
17+
name = StringField("Name", [DataRequired()])
18+
email = StringField(
19+
"Email", [Email(message="Not a valid email address."), DataRequired()]
20+
)
21+
body = TextAreaField(
22+
"Message", [DataRequired(), Length(min=4, message="Your message is too short.")]
23+
)
24+
submit = SubmitField("Submit")
2825

2926

3027
class SignupForm(FlaskForm):
3128
"""Sign up for a user account."""
3229

33-
email = StringField('Email', [
34-
Email(message='Not a valid email address.'),
35-
DataRequired()])
36-
password = PasswordField('Password', [
37-
DataRequired(message="Please enter a password."),
38-
])
39-
confirmPassword = PasswordField('Repeat Password', [
40-
EqualTo(password, message='Passwords must match.')
41-
])
42-
title = SelectField('Title', [DataRequired()],
43-
choices=[('Farmer', 'farmer'),
44-
('Corrupt Politician', 'politician'),
45-
('No-nonsense City Cop', 'cop'),
46-
('Professional Rocket League Player', 'rocket'),
47-
('Lonely Guy At A Diner', 'lonely'),
48-
('Pokemon Trainer', 'pokemon')])
49-
website = StringField('Website', validators=[URL()])
50-
birthday = DateField('Your Birthday')
30+
email = StringField(
31+
"Email", [Email(message="Not a valid email address."), DataRequired()]
32+
)
33+
password = PasswordField(
34+
"Password",
35+
[
36+
DataRequired(message="Please enter a password."),
37+
],
38+
)
39+
confirmPassword = PasswordField(
40+
"Repeat Password", [EqualTo(password, message="Passwords must match.")]
41+
)
42+
title = SelectField(
43+
"Title",
44+
[DataRequired()],
45+
choices=[
46+
("Farmer", "farmer"),
47+
("Corrupt Politician", "politician"),
48+
("No-nonsense City Cop", "cop"),
49+
("Professional Rocket League Player", "rocket"),
50+
("Lonely Guy At A Diner", "lonely"),
51+
("Pokemon Trainer", "pokemon"),
52+
],
53+
)
54+
website = StringField("Website", validators=[URL()])
55+
birthday = DateField("Your Birthday")
5156
recaptcha = RecaptchaField()
52-
submit = SubmitField('Submit')
57+
submit = SubmitField("Submit")

flask_wtforms_tutorial/routes.py

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,30 @@
1-
from flask import url_for, render_template, redirect
21
from flask import current_app as app
2+
from flask import redirect, render_template, url_for
3+
34
from .forms import ContactForm, SignupForm
45

56

6-
@app.route('/')
7+
@app.route("/")
78
def home():
8-
return render_template('index.jinja2',
9-
template='home-template')
9+
return render_template("index.jinja2", template="home-template")
1010

1111

12-
@app.route('/contact', methods=('GET', 'POST'))
12+
@app.route("/contact", methods=("GET", "POST"))
1313
def contact():
1414
form = ContactForm()
1515
if form.validate_on_submit():
16-
return redirect(url_for('success'))
17-
return render_template('contact.jinja2',
18-
form=form,
19-
template='form-template')
16+
return redirect(url_for("success"))
17+
return render_template("contact.jinja2", form=form, template="form-template")
2018

2119

22-
@app.route('/signup', methods=('GET', 'POST'))
20+
@app.route("/signup", methods=("GET", "POST"))
2321
def signup():
2422
form = SignupForm()
2523
if form.validate_on_submit():
26-
return redirect(url_for('success'))
27-
return render_template('signup.jinja2',
28-
form=form,
29-
template='form-template')
24+
return redirect(url_for("success"))
25+
return render_template("signup.jinja2", form=form, template="form-template")
3026

3127

32-
@app.route('/success', methods=('GET', 'POST'))
28+
@app.route("/success", methods=("GET", "POST"))
3329
def success():
34-
return render_template('success.jinja2',
35-
template='success-template')
30+
return render_template("success.jinja2", template="success-template")

0 commit comments

Comments
 (0)