Skip to content

Commit a4c9659

Browse files
committed
polished
1 parent a2f67b4 commit a4c9659

File tree

9 files changed

+306
-47
lines changed

9 files changed

+306
-47
lines changed

.gitignore

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,108 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
*.egg-info/
24+
.installed.cfg
25+
*.egg
26+
MANIFEST
27+
28+
# PyInstaller
29+
# Usually these files are written by a python script from a template
30+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
31+
*.manifest
32+
*.spec
33+
34+
# Installer logs
35+
pip-log.txt
36+
pip-delete-this-directory.txt
37+
38+
# Unit test / coverage reports
39+
htmlcov/
40+
.tox/
41+
.coverage
42+
.coverage.*
43+
.cache
44+
nosetests.xml
45+
coverage.xml
46+
*.cover
47+
.hypothesis/
48+
.pytest_cache/
49+
50+
# Translations
51+
*.mo
52+
*.pot
53+
54+
# Django stuff:
55+
*.log
56+
local_settings.py
57+
db.sqlite3
58+
59+
# Flask stuff:
60+
instance/
61+
.webassets-cache
62+
63+
# Scrapy stuff:
64+
.scrapy
65+
66+
# Sphinx documentation
67+
docs/_build/
68+
69+
# PyBuilder
70+
target/
71+
72+
# Jupyter Notebook
73+
.ipynb_checkpoints
74+
75+
# pyenv
76+
.python-version
77+
78+
# celery beat schedule file
79+
celerybeat-schedule
80+
81+
# SageMath parsed files
82+
*.sage.py
83+
84+
# Environments
185
.env
2-
Pipfile.lock
86+
.venv
87+
env/
88+
venv/
89+
ENV/
90+
env.bak/
91+
venv.bak/
92+
93+
# Spyder project settings
94+
.spyderproject
95+
.spyproject
96+
97+
# Rope project settings
98+
.ropeproject
99+
100+
# mkdocs documentation
101+
/site
102+
103+
# mypy
104+
.mypy_cache/
105+
106+
107+
# misc
3108
.DS_Store

Pipfile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ url = "https://pypi.org/simple"
44
verify_ssl = true
55

66
[dev-packages]
7-
python-dotenv="*"
7+
python-dotenv = "*"
88

99
[packages]
10-
Flask="*"
11-
Flask-SQLAlchemy="*"
12-
psycopg2-binary="*"
10+
Flask = "*"
11+
Flask-SQLAlchemy = "*"
12+
psycopg2-binary = "*"
1313

1414
[requires]
1515
python_version = "3.7"

Pipfile.lock

Lines changed: 147 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
# Flask-SQLAlchemy Tutorial
22

3-
![Python](https://img.shields.io/badge/Python-v3.7.2-blue.svg?logo=python&longCache=true&logoColor=white&colorB=23a8e2&style=flat-square&colorA=36363e)
4-
![Flask](https://img.shields.io/badge/Flask-v1.0.2-blue.svg?longCache=true&logo=flask&style=flat-square&logoColor=white&colorB=23a8e2&colorA=36363e)
5-
![Psycopg2-binary](https://img.shields.io/badge/Psycopg2--Binary-v2.7.7-red.svg?longCache=true&style=flat-square&logo=PostgreSQL&logoColor=white&colorA=36363e)
6-
![Flask-SQLAlchemy](https://img.shields.io/badge/Flask--SQLAlchemy-2.3.2-red.svg?longCache=true&style=flat-square&logo=scala&logoColor=white&colorA=36363e)
3+
![Python](https://img.shields.io/badge/Python-v3.7-blue.svg?logo=python&longCache=true&logoColor=white&colorB=5e81ac&style=flat-square&colorA=4c566a)
4+
![Flask](https://img.shields.io/badge/Flask-v1.0.2-blue.svg?longCache=true&logo=flask&style=flat-square&logoColor=white&colorB=5e81ac&colorA=4c566a)
5+
![Flask-SQLAlchemy](https://img.shields.io/badge/Flask--SQLAlchemy-2.3.2-red.svg?longCache=true&style=flat-square&logo=flask&logoColor=white&colorA=4c566a&colorB=5e81ac)
6+
![Psycopg2-binary](https://img.shields.io/badge/Psycopg2--Binary-v2.7.7-red.svg?longCache=true&style=flat-square&logo=PostgreSQL&logoColor=white&colorA=4c566a&colorB=bf616a)
7+
![GitHub Last Commit](https://img.shields.io/github/last-commit/google/skia.svg?style=flat-square&colorA=4c566a&colorB=a3be8c)
8+
[![GitHub Issues](https://img.shields.io/github/issues/toddbirchard/flasksqlalchemy-tutorial.svg?style=flat-square&colorA=4c566a&colorB=ebcb8b&logo=Github)](https://github.com/toddbirchard/flasksqlalchemy-tutorial/issues)
9+
[![GitHub Stars](https://img.shields.io/github/stars/toddbirchard/flasksqlalchemy-tutorial.svg?style=flat-square&colorB=ebcb8b&colorA=4c566a&logo=Github)](https://github.com/toddbirchard/flasksqlalchemy-tutorial/stargazers)
10+
[![GitHub Forks](https://img.shields.io/github/forks/toddbirchard/flasksqlalchemy-tutorial.svg?style=flat-square&colorA=4c566a&colorB=ebcb8b&logo=Github)](https://github.com/toddbirchard/flasksqlalchemy-tutorial/network)
711

812
![Flask-SQLAlchemy Tutorial](application/static/img/flask-sqlalchemy.jpg)
913

application/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
def create_app():
88
"""Construct the core application."""
99
app = Flask(__name__, instance_relative_config=False)
10-
db.init_app(app)
1110
app.config.from_object('config.Config')
11+
db.init_app(app)
1212

1313
with app.app_context():
1414
# Imports

application/models.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,32 @@
11
from . import db
22

3+
34
class User(db.Model):
4-
"""Model for user accounts."""
5+
"""Data model for user accounts."""
56

67
__tablename__ = 'flasksqlalchemy-users'
78
id = db.Column(db.Integer,
8-
primary_key=True
9-
)
9+
primary_key=True)
1010
username = db.Column(db.String(64),
1111
index=False,
1212
unique=True,
13-
nullable=False
14-
)
13+
nullable=False)
1514
email = db.Column(db.String(80),
1615
index=True,
1716
unique=True,
18-
nullable=False
19-
)
17+
nullable=False)
2018
created = db.Column(db.DateTime,
2119
index=False,
2220
unique=False,
23-
nullable=False
24-
)
21+
nullable=False)
2522
bio = db.Column(db.Text,
2623
index=False,
2724
unique=False,
28-
nullable=True
29-
)
25+
nullable=True)
3026
admin = db.Column(db.Boolean,
3127
index=False,
3228
unique=False,
33-
nullable=False
34-
)
29+
nullable=False)
3530

3631
def __repr__(self):
3732
return '<User {}>'.format(self.username)

application/routes.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
1-
from flask import request, render_template
1+
from flask import request, render_template, make_response
22
from datetime import datetime as dt
33
from flask import current_app as app
44
from .models import db, User
55

66

77
@app.route('/', methods=['GET'])
8-
def entry():
9-
"""Endpoint to create a user."""
10-
new_user = User(username='myuser3',
11-
email='myuser3@example.com',
12-
created=dt.now(),
13-
bio="Because he's the hero Gotham deserves, but not the one it needs right now.",
14-
admin=False
15-
)
16-
db.session.add(new_user)
17-
db.session.commit()
18-
users = User.query.all()
19-
return render_template('users.html', users=users, title="Show Users")
8+
def create_user():
9+
"""Create a user."""
10+
username = request.args.get('user')
11+
email = request.args.get('email')
12+
if username and email:
13+
existing_user = User.query.filter(User.username == username or User.email == email).first()
14+
if existing_user:
15+
return make_response(f'{username} ({email}) already created!')
16+
new_user = User(username=username,
17+
email=email,
18+
created=dt.now(),
19+
bio="In West Philadelphia born and raised, on the playground is where I spent most of my days",
20+
admin=False) # Create an instance of the User class
21+
db.session.add(new_user) # Adds new User record to database
22+
db.session.commit() # Commits all changes
23+
return render_template('users.html',
24+
users=User.query.all(),
25+
title="Show Users")

0 commit comments

Comments
 (0)