Skip to content

Commit cdd0e51

Browse files
committed
first commit
0 parents  commit cdd0e51

11 files changed

Lines changed: 153 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.env
2+
Pipfile.lock

Pipfile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[[source]]
2+
name = "pypi"
3+
url = "https://pypi.org/simple"
4+
verify_ssl = true
5+
6+
[dev-packages]
7+
python-dotenv="*"
8+
pylama="*"
9+
10+
[packages]
11+
Flask="*"
12+
Flask-SQLAlchemy="*"
13+
pymysql="*"
14+
15+
[requires]
16+
python_version = "3.7"

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# flasksqlalchemy-tutorial

application/__init__.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from flask import Flask
2+
from flask_sqlalchemy import SQLAlchemy
3+
4+
db = SQLAlchemy()
5+
6+
7+
def create_app():
8+
"""Construct the core application."""
9+
app = Flask(__name__, instance_relative_config=False)
10+
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
11+
app.config["SQLALCHEMY_ECHO"] = True
12+
db.init_app(app)
13+
app.config.from_object('config.Config')
14+
15+
with app.app_context():
16+
# Imports
17+
from . import routes
18+
# Initialize Global db
19+
db.create_all()
20+
21+
return app

application/models.py

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

application/routes.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from flask import request, render_template
2+
from datetime import datetime as dt
3+
from flask import current_app as app
4+
from .models import db, User
5+
6+
7+
@app.route('/', methods=['GET'])
8+
def entry():
9+
"""Endpoint to create a user."""
10+
new_user = User(username='myuser2',
11+
email='myuser2@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")

application/templates/layout.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="utf-8" />
6+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
7+
<title>{{title}}</title>
8+
<meta name="HandheldFriendly" content="True" />
9+
<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover" />
10+
<link rel="shortcut icon" href="" type="image/x-icon" />
11+
</head>
12+
13+
<body class="{{template}}">
14+
{% block content %}{% endblock %}
15+
</body>
16+
17+
</html>

application/templates/users.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{% extends "layout.html" %}
2+
3+
{% block content %}
4+
{% for user in users %}
5+
<ul id="user.username">
6+
<li>Username: {{ user.username }}</li>
7+
<li>Email: {{ user.email }}</li>
8+
<li>Created: {{ user.created }}</li>
9+
<li>Bio: {{ user.bio }}</li>
10+
<li>Admin: {{ user.admin }}</li>
11+
</ul>
12+
{% endfor %}
13+
{% endblock %}

config.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import os
2+
3+
4+
class Config:
5+
"""Set Flask configuration vars from .env file."""
6+
7+
# General
8+
TESTING = os.environ["TESTING"]
9+
SECRET_KEY = os.environ["SECRET_KEY"]
10+
FLASK_DEBUG = os.environ["FLASK_DEBUG"]
11+
12+
# Database
13+
SQLALCHEMY_DATABASE_URI = os.environ["SQLALCHEMY_DATABASE_URI"]
14+
# SQLALCHEMY_ECHO = os.environ["SQLALCHEMY_ECHO"]
15+
# SQLALCHEMY_TRACK_MODIFICATIONS = os.environ["SQLALCHEMY_TRACK_MODIFICATIONS"]

start.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# start.sh
2+
3+
export FLASK_APP=wsgi.py
4+
export FLASK_DEBUG=1
5+
export APP_CONFIG_FILE=config.ini
6+
flask run

0 commit comments

Comments
 (0)