added target entity
This commit is contained in:
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__)
|
||||
Reference in New Issue
Block a user