added target entity
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -145,3 +145,4 @@ cython_debug/
|
|||||||
# End of https://www.toptal.com/developers/gitignore/api/python
|
# End of https://www.toptal.com/developers/gitignore/api/python
|
||||||
|
|
||||||
*.db
|
*.db
|
||||||
|
!api/target
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
from flask import Flask, Blueprint
|
from flask import Flask, Blueprint
|
||||||
from . import commands, login, target_exchange
|
from . import commands, login, target_exchange, target
|
||||||
from .settings import ProdConfig, Config
|
from .settings import ProdConfig, Config
|
||||||
from .extensions import db, migrate, jwt, apispec
|
from .extensions import db, migrate, jwt, apispec
|
||||||
from .exceptions import ApiException
|
from .exceptions import ApiException
|
||||||
@@ -40,6 +40,8 @@ def register_blueprints(app: Flask):
|
|||||||
login.views.blueprint, url_prefix='/login')
|
login.views.blueprint, url_prefix='/login')
|
||||||
api_blueprint.register_blueprint(
|
api_blueprint.register_blueprint(
|
||||||
target_exchange.views.blueprint, url_prefix='/target-exchange')
|
target_exchange.views.blueprint, url_prefix='/target-exchange')
|
||||||
|
api_blueprint.register_blueprint(
|
||||||
|
target.views.blueprint, url_prefix='/target')
|
||||||
|
|
||||||
app.register_blueprint(api_blueprint)
|
app.register_blueprint(api_blueprint)
|
||||||
|
|
||||||
@@ -80,7 +82,8 @@ def register_shellcontext(app: Flask):
|
|||||||
"""Shell context objects."""
|
"""Shell context objects."""
|
||||||
return {
|
return {
|
||||||
'db': db,
|
'db': db,
|
||||||
'TargetExchange': target_exchange.models.TargetExchange
|
'TargetExchange': target_exchange.models.TargetExchange,
|
||||||
|
'Target': target.models.Target
|
||||||
}
|
}
|
||||||
|
|
||||||
app.shell_context_processor(shell_context)
|
app.shell_context_processor(shell_context)
|
||||||
|
|||||||
3
api/target/__init__.py
Normal file
3
api/target/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
"""Target view"""
|
||||||
|
|
||||||
|
from . import views, models
|
||||||
12
api/target/models.py
Normal file
12
api/target/models.py
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
from api.database import (Model, SurrogatePK, db,
|
||||||
|
Column, reference_col, relationship)
|
||||||
|
from sqlalchemy import String
|
||||||
|
|
||||||
|
from api.target_exchange.models import TargetExchange
|
||||||
|
|
||||||
|
class Target(SurrogatePK, Model):
|
||||||
|
__tablename__ = "target"
|
||||||
|
name = Column(String(255), unique=True, nullable=False)
|
||||||
|
routing_key = Column(String(255), nullable=False)
|
||||||
|
target_exchange_id = reference_col(TargetExchange.__tablename__, nullable=False)
|
||||||
|
exchange = relationship(TargetExchange.__name__, backref=db.backref('targets'))
|
||||||
11
api/target/serializers.py
Normal file
11
api/target/serializers.py
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
from marshmallow import Schema, fields
|
||||||
|
from api.target_exchange.serializers import TargetExchangeSchema
|
||||||
|
|
||||||
|
class TargetSchema(Schema):
|
||||||
|
id = fields.Int()
|
||||||
|
name = fields.Str()
|
||||||
|
routing_key = fields.Str(required=True)
|
||||||
|
exchange = fields.Nested(TargetExchangeSchema)
|
||||||
|
|
||||||
|
target_schema = TargetSchema()
|
||||||
|
targets_schema = TargetSchema(many=True)
|
||||||
60
api/target/views.py
Normal file
60
api/target/views.py
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
from flask import Blueprint
|
||||||
|
from flask_jwt_extended import jwt_required
|
||||||
|
from flask_apispec import use_kwargs, marshal_with, doc
|
||||||
|
from marshmallow import fields
|
||||||
|
|
||||||
|
from api.exceptions import NotFoundException
|
||||||
|
from api.target_exchange.models import TargetExchange
|
||||||
|
|
||||||
|
from .models import Target
|
||||||
|
from .serializers import target_schema, targets_schema
|
||||||
|
|
||||||
|
blueprint = Blueprint('target', __name__)
|
||||||
|
|
||||||
|
|
||||||
|
@doc(tags=['Target'])
|
||||||
|
@blueprint.route('', methods=['GET'])
|
||||||
|
@jwt_required()
|
||||||
|
@use_kwargs({'exchange': fields.Str()})
|
||||||
|
@marshal_with(targets_schema)
|
||||||
|
def get_list(exchange=None):
|
||||||
|
res = Target.query
|
||||||
|
if exchange is not None:
|
||||||
|
res = res.join(Target.exchange).filter(TargetExchange.name == exchange)
|
||||||
|
return res.all()
|
||||||
|
|
||||||
|
|
||||||
|
@doc(tags=['Target'])
|
||||||
|
@blueprint.route('', methods=['POST'])
|
||||||
|
@jwt_required()
|
||||||
|
@use_kwargs(target_schema)
|
||||||
|
@marshal_with(target_schema)
|
||||||
|
def create(name):
|
||||||
|
target = Target(name=name)
|
||||||
|
target.save()
|
||||||
|
return target
|
||||||
|
|
||||||
|
|
||||||
|
@doc(tags=['Target'])
|
||||||
|
@blueprint.route('/<id>', methods=['GET'])
|
||||||
|
@jwt_required()
|
||||||
|
@marshal_with(target_schema)
|
||||||
|
def get_by_id(id):
|
||||||
|
target_exchange = Target.get_by_id(id)
|
||||||
|
if target_exchange is not None:
|
||||||
|
return target_exchange
|
||||||
|
else:
|
||||||
|
return NotFoundException(Target.__name__)
|
||||||
|
|
||||||
|
|
||||||
|
@doc(tags=['Target'])
|
||||||
|
@blueprint.route('/<id>', methods=['PUT'])
|
||||||
|
@jwt_required()
|
||||||
|
@use_kwargs(target_schema)
|
||||||
|
@marshal_with(target_schema)
|
||||||
|
def update(id, **kwargs):
|
||||||
|
target_exchange = Target.get_by_id(id)
|
||||||
|
if target_exchange is not None:
|
||||||
|
return target_exchange.update(**kwargs)
|
||||||
|
else:
|
||||||
|
return NotFoundException(Target.__name__)
|
||||||
36
migrations/versions/294a2085f840_added_targets.py
Normal file
36
migrations/versions/294a2085f840_added_targets.py
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
"""added targets
|
||||||
|
|
||||||
|
Revision ID: 294a2085f840
|
||||||
|
Revises: 14ceaaaa6e85
|
||||||
|
Create Date: 2021-09-21 21:20:27.226540
|
||||||
|
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = '294a2085f840'
|
||||||
|
down_revision = '14ceaaaa6e85'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.create_table('target',
|
||||||
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('name', sa.String(length=255), nullable=False),
|
||||||
|
sa.Column('routing_key', sa.String(length=255), nullable=False),
|
||||||
|
sa.Column('target_exchange_id', sa.Integer(), nullable=False),
|
||||||
|
sa.ForeignKeyConstraint(['target_exchange_id'], ['target-exchange.id'], ),
|
||||||
|
sa.PrimaryKeyConstraint('id'),
|
||||||
|
sa.UniqueConstraint('name')
|
||||||
|
)
|
||||||
|
# ### end Alembic commands ###
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.drop_table('target')
|
||||||
|
# ### end Alembic commands ###
|
||||||
Reference in New Issue
Block a user