Skip to content

Commit c68eebe

Browse files
committed
added test
1 parent a54125e commit c68eebe

File tree

715 files changed

+247137
-182
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

715 files changed

+247137
-182
lines changed

app.py

Lines changed: 57 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,64 +11,90 @@
1111
from resources.tag import blp as TagBlueprint
1212
from resources.user import blp as UserBlueprint
1313

14+
1415
def create_app(db_url=None):
1516
app = Flask(__name__)
1617

17-
#app.config['PROPAGATE_EXCEPTIONS'] = True
18-
app.config['API_TITLE'] = 'Stores REST API' #api needs this
19-
app.config['API_VERSION'] = 'v1' #api needs this
20-
app.config['OPENAPI_VERSION'] = '3.0.3' #api needs this
21-
app.config['OPENAPI_URL_PREFIX'] = '/' #swagger needs this
22-
app.config['OPENAPI_SWAGGER_UI_PATH'] = '/swagger-ui' #swagger needs this
23-
app.config['OPENAPI_SWAGGER_UI_URL'] = "https://cdn.jsdelivr.net/npm/swagger-ui-dist/" #swagger needs this
24-
app.config['SQLALCHEMY_DATABASE_URI'] = db_url or os.getenv('DATABASE_URL', 'sqlite:///data.db')
25-
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
18+
# app.config['PROPAGATE_EXCEPTIONS'] = True
19+
app.config["API_TITLE"] = "Stores REST API" # api needs this
20+
app.config["API_VERSION"] = "v1" # api needs this
21+
app.config["OPENAPI_VERSION"] = "3.0.3" # api needs this
22+
app.config["OPENAPI_URL_PREFIX"] = "/" # swagger needs this
23+
app.config["OPENAPI_SWAGGER_UI_PATH"] = "/swagger-ui" # swagger needs this
24+
app.config[
25+
"OPENAPI_SWAGGER_UI_URL"
26+
] = "https://cdn.jsdelivr.net/npm/swagger-ui-dist/" # swagger needs this
27+
app.config["SQLALCHEMY_DATABASE_URI"] = db_url or os.getenv(
28+
"DATABASE_URL", "sqlite:///data.db"
29+
)
30+
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
2631
db.init_app(app)
2732
migrate = Migrate(app, db)
2833
api = Api(app)
2934

30-
app.config['JWT_SECRET_KEY'] = 'jose'
35+
app.config["JWT_SECRET_KEY"] = "jose"
3136
jwt = JWTManager(app)
3237

3338
@jwt.token_in_blocklist_loader
3439
def check_if_token_in_blocklist(jwt_header, jwt_payload):
35-
return jwt_payload['jti'] in BLOCKLIST
36-
40+
return jwt_payload["jti"] in BLOCKLIST
41+
3742
@jwt.revoked_token_loader
3843
def revoked_token_callback(jwt_header, jwt_payload):
39-
return (jsonify({'descrition':'The token has been revoked','error':'token_revoked'}), 401,)
40-
44+
return (
45+
jsonify(
46+
{"descrition": "The token has been revoked", "error": "token_revoked"}
47+
),
48+
401,
49+
)
50+
4151
@jwt.needs_fresh_token_loader
4252
def token_not_fresh_callback(jwt_header, jwt_payload):
43-
return (jsonify({'description':'The token is not fresh', 'error':'fresh_token_required.'}), 401,)
53+
return (
54+
jsonify(
55+
{
56+
"description": "The token is not fresh",
57+
"error": "fresh_token_required.",
58+
}
59+
),
60+
401,
61+
)
4462

4563
@jwt.additional_claims_loader
4664
def add_claims_to_jwt(identity):
4765
if identity == 1:
48-
return {'is admin':True}
49-
return {'is admin':False}
66+
return {"is admin": True}
67+
return {"is admin": False}
5068

5169
@jwt.expired_token_loader
5270
def expired_token_callback(jwt_header, jwt_payload):
53-
return(jsonify({'message':'The token has expired', 'error':'token_expired'}), 401,)
54-
71+
return (
72+
jsonify({"message": "The token has expired", "error": "token_expired"}),
73+
401,
74+
)
75+
5576
@jwt.invalid_token_loader
5677
def invalid_token_callbak(error):
57-
return (jsonify({'message':'Signature verification failed','error':'invalid token'}), 401,)
58-
78+
return (
79+
jsonify(
80+
{"message": "Signature verification failed", "error": "invalid token"}
81+
),
82+
401,
83+
)
84+
5985
@jwt.unauthorized_loader
6086
def missing_token_callback(error):
61-
return (jsonify({'description':'Request does not contain an access token', 'error':'authorizatiion_required'}))
87+
return jsonify(
88+
{
89+
"description": "Request does not contain an access token",
90+
"error": "authorizatiion_required",
91+
}
92+
)
6293

63-
#with app.app_context():
64-
# db.create_all()
65-
api.register_blueprint(StoreBlueprint)
66-
api.register_blueprint(ItemBlueprint)
94+
# with app.app_context():
95+
# db.create_all()
96+
api.register_blueprint(StoreBlueprint)
97+
api.register_blueprint(ItemBlueprint)
6798
api.register_blueprint(TagBlueprint)
6899
api.register_blueprint(UserBlueprint)
69100
return app
70-
71-
72-
73-
74-

blocklist.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
BLOCKLIST = set() #for storing JWT is the best in the DATABASE or the best best is REDIS
1+
BLOCKLIST = (
2+
set()
3+
) # for storing JWT is the best in the DATABASE or the best best is REDIS

db.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
from flask_sqlalchemy import SQLAlchemy
22

3-
db = SQLAlchemy()
3+
db = SQLAlchemy()

migrations/env.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,31 @@
1212
# Interpret the config file for Python logging.
1313
# This line sets up loggers basically.
1414
fileConfig(config.config_file_name)
15-
logger = logging.getLogger('alembic.env')
15+
logger = logging.getLogger("alembic.env")
1616

1717

1818
def get_engine():
1919
try:
2020
# this works with Flask-SQLAlchemy<3 and Alchemical
21-
return current_app.extensions['migrate'].db.get_engine()
21+
return current_app.extensions["migrate"].db.get_engine()
2222
except TypeError:
2323
# this works with Flask-SQLAlchemy>=3
24-
return current_app.extensions['migrate'].db.engine
24+
return current_app.extensions["migrate"].db.engine
2525

2626

2727
def get_engine_url():
2828
try:
29-
return get_engine().url.render_as_string(hide_password=False).replace(
30-
'%', '%%')
29+
return get_engine().url.render_as_string(hide_password=False).replace("%", "%%")
3130
except AttributeError:
32-
return str(get_engine().url).replace('%', '%%')
31+
return str(get_engine().url).replace("%", "%%")
3332

3433

3534
# add your model's MetaData object here
3635
# for 'autogenerate' support
3736
# from myapp import mymodel
3837
# target_metadata = mymodel.Base.metadata
39-
config.set_main_option('sqlalchemy.url', get_engine_url())
40-
target_db = current_app.extensions['migrate'].db
38+
config.set_main_option("sqlalchemy.url", get_engine_url())
39+
target_db = current_app.extensions["migrate"].db
4140

4241
# other values from the config, defined by the needs of env.py,
4342
# can be acquired:
@@ -46,7 +45,7 @@ def get_engine_url():
4645

4746

4847
def get_metadata():
49-
if hasattr(target_db, 'metadatas'):
48+
if hasattr(target_db, "metadatas"):
5049
return target_db.metadatas[None]
5150
return target_db.metadata
5251

@@ -64,9 +63,7 @@ def run_migrations_offline():
6463
6564
"""
6665
url = config.get_main_option("sqlalchemy.url")
67-
context.configure(
68-
url=url, target_metadata=get_metadata(), literal_binds=True
69-
)
66+
context.configure(url=url, target_metadata=get_metadata(), literal_binds=True)
7067

7168
with context.begin_transaction():
7269
context.run_migrations()
@@ -84,11 +81,11 @@ def run_migrations_online():
8481
# when there are no changes to the schema
8582
# reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html
8683
def process_revision_directives(context, revision, directives):
87-
if getattr(config.cmd_opts, 'autogenerate', False):
84+
if getattr(config.cmd_opts, "autogenerate", False):
8885
script = directives[0]
8986
if script.upgrade_ops.is_empty():
9087
directives[:] = []
91-
logger.info('No changes in schema detected.')
88+
logger.info("No changes in schema detected.")
9289

9390
connectable = get_engine()
9491

@@ -97,7 +94,7 @@ def process_revision_directives(context, revision, directives):
9794
connection=connection,
9895
target_metadata=get_metadata(),
9996
process_revision_directives=process_revision_directives,
100-
**current_app.extensions['migrate'].configure_args
97+
**current_app.extensions["migrate"].configure_args
10198
)
10299

103100
with context.begin_transaction():

migrations/versions/1bbe1d722e6d_.py

Lines changed: 56 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,60 +10,77 @@
1010

1111

1212
# revision identifiers, used by Alembic.
13-
revision = '1bbe1d722e6d'
13+
revision = "1bbe1d722e6d"
1414
down_revision = None
1515
branch_labels = None
1616
depends_on = None
1717

1818

1919
def upgrade():
2020
# ### commands auto generated by Alembic - please adjust! ###
21-
op.create_table('stores',
22-
sa.Column('id', sa.Integer(), nullable=False),
23-
sa.Column('name', sa.String(length=80), nullable=False),
24-
sa.PrimaryKeyConstraint('id'),
25-
sa.UniqueConstraint('name')
21+
op.create_table(
22+
"stores",
23+
sa.Column("id", sa.Integer(), nullable=False),
24+
sa.Column("name", sa.String(length=80), nullable=False),
25+
sa.PrimaryKeyConstraint("id"),
26+
sa.UniqueConstraint("name"),
2627
)
27-
op.create_table('users',
28-
sa.Column('id', sa.Integer(), nullable=False),
29-
sa.Column('username', sa.String(length=80), nullable=False),
30-
sa.Column('password', sa.String(length=80), nullable=False),
31-
sa.PrimaryKeyConstraint('id'),
32-
sa.UniqueConstraint('username')
28+
op.create_table(
29+
"users",
30+
sa.Column("id", sa.Integer(), nullable=False),
31+
sa.Column("username", sa.String(length=80), nullable=False),
32+
sa.Column("password", sa.String(length=80), nullable=False),
33+
sa.PrimaryKeyConstraint("id"),
34+
sa.UniqueConstraint("username"),
3335
)
34-
op.create_table('items',
35-
sa.Column('id', sa.Integer(), nullable=False),
36-
sa.Column('name', sa.String(length=80), nullable=False),
37-
sa.Column('price', sa.Float(precision=2), nullable=False),
38-
sa.Column('store_id', sa.Integer(), nullable=False),
39-
sa.ForeignKeyConstraint(['store_id'], ['stores.id'], ),
40-
sa.PrimaryKeyConstraint('id'),
41-
sa.UniqueConstraint('name')
36+
op.create_table(
37+
"items",
38+
sa.Column("id", sa.Integer(), nullable=False),
39+
sa.Column("name", sa.String(length=80), nullable=False),
40+
sa.Column("price", sa.Float(precision=2), nullable=False),
41+
sa.Column("store_id", sa.Integer(), nullable=False),
42+
sa.ForeignKeyConstraint(
43+
["store_id"],
44+
["stores.id"],
45+
),
46+
sa.PrimaryKeyConstraint("id"),
47+
sa.UniqueConstraint("name"),
4248
)
43-
op.create_table('tags',
44-
sa.Column('id', sa.Integer(), nullable=False),
45-
sa.Column('name', sa.String(length=80), nullable=False),
46-
sa.Column('store_id', sa.Integer(), nullable=False),
47-
sa.ForeignKeyConstraint(['store_id'], ['stores.id'], ),
48-
sa.PrimaryKeyConstraint('id'),
49-
sa.UniqueConstraint('name')
49+
op.create_table(
50+
"tags",
51+
sa.Column("id", sa.Integer(), nullable=False),
52+
sa.Column("name", sa.String(length=80), nullable=False),
53+
sa.Column("store_id", sa.Integer(), nullable=False),
54+
sa.ForeignKeyConstraint(
55+
["store_id"],
56+
["stores.id"],
57+
),
58+
sa.PrimaryKeyConstraint("id"),
59+
sa.UniqueConstraint("name"),
5060
)
51-
op.create_table('items_tags',
52-
sa.Column('id', sa.Integer(), nullable=False),
53-
sa.Column('item_id', sa.Integer(), nullable=True),
54-
sa.Column('tag_id', sa.Integer(), nullable=True),
55-
sa.ForeignKeyConstraint(['item_id'], ['items.id'], ),
56-
sa.ForeignKeyConstraint(['tag_id'], ['tags.id'], ),
57-
sa.PrimaryKeyConstraint('id')
61+
op.create_table(
62+
"items_tags",
63+
sa.Column("id", sa.Integer(), nullable=False),
64+
sa.Column("item_id", sa.Integer(), nullable=True),
65+
sa.Column("tag_id", sa.Integer(), nullable=True),
66+
sa.ForeignKeyConstraint(
67+
["item_id"],
68+
["items.id"],
69+
),
70+
sa.ForeignKeyConstraint(
71+
["tag_id"],
72+
["tags.id"],
73+
),
74+
sa.PrimaryKeyConstraint("id"),
5875
)
5976
# ### end Alembic commands ###
6077

6178

6279
def downgrade():
6380
# ### commands auto generated by Alembic - please adjust! ###
64-
op.drop_table('items_tags')
65-
op.drop_table('tags')
66-
op.drop_table('items')
67-
op.drop_table('users')
68-
op.drop_table('stores')
81+
op.drop_table("items_tags")
82+
op.drop_table("tags")
83+
op.drop_table("items")
84+
op.drop_table("users")
85+
op.drop_table("stores")
6986
# ### end Alembic commands ###

migrations/versions/8ab68295c0d2_.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,23 @@
1010

1111

1212
# revision identifiers, used by Alembic.
13-
revision = '8ab68295c0d2'
14-
down_revision = '1bbe1d722e6d'
13+
revision = "8ab68295c0d2"
14+
down_revision = "1bbe1d722e6d"
1515
branch_labels = None
1616
depends_on = None
1717

1818

1919
def upgrade():
2020
# ### commands auto generated by Alembic - please adjust! ###
21-
with op.batch_alter_table('items', schema=None) as batch_op:
22-
batch_op.add_column(sa.Column('description', sa.String(), nullable=True))
21+
with op.batch_alter_table("items", schema=None) as batch_op:
22+
batch_op.add_column(sa.Column("description", sa.String(), nullable=True))
2323

2424
# ### end Alembic commands ###
2525

2626

2727
def downgrade():
2828
# ### commands auto generated by Alembic - please adjust! ###
29-
with op.batch_alter_table('items', schema=None) as batch_op:
30-
batch_op.drop_column('description')
29+
with op.batch_alter_table("items", schema=None) as batch_op:
30+
batch_op.drop_column("description")
3131

3232
# ### end Alembic commands ###

models/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
from models.item import ItemModel
33
from models.tag import TagModel
44
from models.item_tags import ItemTags
5-
from models.user import UserModel
5+
from models.user import UserModel

models/item.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
from db import db
22

3+
34
class ItemModel(db.Model):
4-
__tablename__ = 'items'
5+
__tablename__ = "items"
56

67
id = db.Column(db.Integer, primary_key=True)
78
name = db.Column(db.String(80), unique=True, nullable=False)
89
description = db.Column(db.String)
910
price = db.Column(db.Float(precision=2), unique=False, nullable=False)
10-
store_id = db.Column(db.Integer, db.ForeignKey('stores.id'), unique=False, nullable=False)
11-
store = db.relationship('StoreModel', back_populates='items')
12-
tags = db.relationship('TagModel', back_populates='items', secondary='items_tags')
11+
store_id = db.Column(
12+
db.Integer, db.ForeignKey("stores.id"), unique=False, nullable=False
13+
)
14+
store = db.relationship("StoreModel", back_populates="items")
15+
tags = db.relationship("TagModel", back_populates="items", secondary="items_tags")

0 commit comments

Comments
 (0)