Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0579c4e

Browse filesBrowse files
committed
Refactor.
1 parent ab204ea commit 0579c4e
Copy full SHA for 0579c4e
Expand file treeCollapse file tree

22 files changed

+540
-102
lines changed

‎.env.example

Copy file name to clipboard
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
FLASK_ENV=production
2+
SECRET_KEY=YOURSECRETKEY
3+
SQLALCHEMY_DATABASE_URI=mysql+pymysql://[USER]:[PASSWORD]@[HOST]:[PORT]/[DATABASE_NAME]

‎Pipfile

Copy file name to clipboardExpand all lines: Pipfile
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ verify_ssl = true
99
Flask = "*"
1010
Flask-SQLAlchemy = "*"
1111
PyMySQL = "*"
12-
python-dotenv = "*"
12+
Python-dotenv = "*"
1313

1414
[requires]
1515
python_version = "3.8"

‎Pipfile.lock

Copy file name to clipboardExpand all lines: Pipfile.lock
+39-24Lines changed: 39 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎README.md

Copy file name to clipboardExpand all lines: README.md
+9-7Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,17 @@ $ pipenv update
3535
$ flask run
3636
```
3737

38-
## Configuration
38+
## Usage
3939

40-
Configuration is handled by creating a **.env** file. This should contain the following variables (replace the values with your own):
40+
Replace the values in **.env.example** with your values and rename this file to **.env**:
41+
42+
* `FLASK_APP`: Entry point of your application (should be `wsgi.py`).
43+
* `FLASK_ENV`: The environment to run your app in (either `development` or `production`).
44+
* `SECRET_KEY`: Randomly generated string of characters used to encrypt your app's data.
45+
* `SQLALCHEMY_DATABASE_URI`: SQLAlchemy connection URI to a SQL database.
46+
47+
*Remember never to commit secrets saved in .env files to Github.*
4148

42-
```.env
43-
FLASK_ENV="production"
44-
SECRET_KEY="YOURSECRETKEY"
45-
SQLALCHEMY_DATABASE_URI="mysql+pymysql://[USER]:[PASSWORD]@[HOST]:[PORT]/[DATABASE_NAME]"
46-
```
4749
-----
4850

4951
**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.

‎application/models.py

Copy file name to clipboardExpand all lines: application/models.py
-32Lines changed: 0 additions & 32 deletions
This file was deleted.

‎application/routes.py

Copy file name to clipboardExpand all lines: application/routes.py
-25Lines changed: 0 additions & 25 deletions
This file was deleted.
-564 KB
Binary file not shown.

‎config.py

Copy file name to clipboardExpand all lines: config.py
+4-4Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"""Flask configuration variables."""
12
from os import environ, path
23
from dotenv import load_dotenv
34

@@ -6,13 +7,12 @@
67

78

89
class Config:
9-
"""Set Flask configuration variables from .env file."""
10+
"""Set Flask configuration from .env file."""
1011

11-
# General Flask Config
12+
# General Config
1213
SECRET_KEY = environ.get('SECRET_KEY')
14+
FLASK_APP = environ.get('FLASK_APP')
1315
FLASK_ENV = environ.get('FLASK_ENV')
14-
FLASK_APP = 'wsgi.py'
15-
FLASK_DEBUG = 1
1616

1717
# Database
1818
SQLALCHEMY_DATABASE_URI = environ.get("SQLALCHEMY_DATABASE_URI")

‎application/__init__.py renamed to ‎flask_sqlalchemy_tutorial/__init__.py

Copy file name to clipboardExpand all lines: flask_sqlalchemy_tutorial/__init__.py
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"""Initialize Flask app."""
12
from flask import Flask
23
from flask_sqlalchemy import SQLAlchemy
34

‎flask_sqlalchemy_tutorial/models.py

Copy file name to clipboard
+45Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""Data models."""
2+
from . import db
3+
4+
5+
class User(db.Model):
6+
"""Data model for user accounts."""
7+
8+
__tablename__ = 'flasksqlalchemy-tutorial-users'
9+
id = db.Column(
10+
db.Integer,
11+
primary_key=True
12+
)
13+
username = db.Column(
14+
db.String(64),
15+
index=False,
16+
unique=True,
17+
nullable=False
18+
)
19+
email = db.Column(
20+
db.String(80),
21+
index=True,
22+
unique=True,
23+
nullable=False
24+
)
25+
created = db.Column(
26+
db.DateTime,
27+
index=False,
28+
unique=False,
29+
nullable=False
30+
)
31+
bio = db.Column(
32+
db.Text,
33+
index=False,
34+
unique=False,
35+
nullable=True
36+
)
37+
admin = db.Column(
38+
db.Boolean,
39+
index=False,
40+
unique=False,
41+
nullable=False
42+
)
43+
44+
def __repr__(self):
45+
return '<User {}>'.format(self.username)

‎flask_sqlalchemy_tutorial/routes.py

Copy file name to clipboard
+34Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""Application routes."""
2+
from datetime import datetime as dt
3+
from flask import request, render_template, make_response, redirect, url_for
4+
from flask import current_app as app
5+
from .models import db, User
6+
7+
8+
@app.route('/', methods=['GET'])
9+
def user_records():
10+
"""Create a user via query string parameters."""
11+
username = request.args.get('user')
12+
email = request.args.get('email')
13+
if username and email:
14+
existing_user = User.query.filter(
15+
User.username == username or User.email == email
16+
).first()
17+
if existing_user:
18+
return make_response(f'{username} ({email}) already created!')
19+
new_user = User(
20+
username=username,
21+
email=email,
22+
created=dt.now(),
23+
bio="In West Philadelphia born and raised, \
24+
on the playground is where I spent most of my days",
25+
admin=False
26+
) # Create an instance of the User class
27+
db.session.add(new_user) # Adds new User record to database
28+
db.session.commit() # Commits all changes
29+
redirect(url_for('user_records'))
30+
return render_template(
31+
'users.jinja2',
32+
users=User.query.all(),
33+
title="Show Users"
34+
)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.