added target entity

This commit is contained in:
Gardient
2021-09-21 21:23:14 +03:00
parent 8e67a58630
commit aeb8916744
7 changed files with 128 additions and 2 deletions

View File

@@ -1,5 +1,5 @@
from flask import Flask, Blueprint
from . import commands, login, target_exchange
from . import commands, login, target_exchange, target
from .settings import ProdConfig, Config
from .extensions import db, migrate, jwt, apispec
from .exceptions import ApiException
@@ -40,6 +40,8 @@ def register_blueprints(app: Flask):
login.views.blueprint, url_prefix='/login')
api_blueprint.register_blueprint(
target_exchange.views.blueprint, url_prefix='/target-exchange')
api_blueprint.register_blueprint(
target.views.blueprint, url_prefix='/target')
app.register_blueprint(api_blueprint)
@@ -80,7 +82,8 @@ def register_shellcontext(app: Flask):
"""Shell context objects."""
return {
'db': db,
'TargetExchange': target_exchange.models.TargetExchange
'TargetExchange': target_exchange.models.TargetExchange,
'Target': target.models.Target
}
app.shell_context_processor(shell_context)

3
api/target/__init__.py Normal file
View File

@@ -0,0 +1,3 @@
"""Target view"""
from . import views, models

12
api/target/models.py Normal file
View 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
View 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
View 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__)