adds target-exchange and initial migrations

This commit is contained in:
Gardient
2021-09-19 23:55:36 +03:00
parent cc44c4056e
commit 472f682c58
11 changed files with 273 additions and 3 deletions

View File

@@ -1,5 +1,5 @@
from flask import Flask, Blueprint
from . import commands, login
from . import commands, login, target_exchange
from .settings import ProdConfig, Config
from .extensions import db, migrate, jwt
from .exceptions import ApiException
@@ -35,6 +35,7 @@ def register_blueprints(app: Flask):
api_blueprint = Blueprint('api', __name__, url_prefix='/api')
api_blueprint.register_blueprint(login.views.blueprint, url_prefix='/login')
api_blueprint.register_blueprint(target_exchange.views.blueprint, url_prefix='/target-exchange')
app.register_blueprint(api_blueprint)

View File

@@ -14,8 +14,8 @@ class ApiException(Exception):
return rv
class NotFoundException(ApiException):
def __init__(self, message) -> None:
super().__init__(404, message)
def __init__(self, entity_name) -> None:
super().__init__(404, f'{entity_name} could not be found')
class BadRequestException(ApiException):
def __init__(self, message) -> None:

View File

@@ -0,0 +1,3 @@
"""exchange management"""
from . import views

View File

@@ -0,0 +1,10 @@
from api.database import (Model, SurrogatePK, db,
Column, reference_col, relationship)
class TargetExchange(SurrogatePK, Model):
__tablename__ = "target-exchange"
name = Column(db.String(255), unique=True, nullable=False)
def __init__(self, name, **kwargs) -> None:
Model.__init__(self, name=name, **kwargs)

View File

@@ -0,0 +1,8 @@
from marshmallow import Schema, fields
class TargetExchangeSchema(Schema):
id = fields.Int()
name = fields.Str()
target_exchange_schema = TargetExchangeSchema()
target_exchanges_schema = TargetExchangeSchema(many=True)

View File

@@ -0,0 +1,49 @@
from flask import Blueprint
from flask_jwt_extended import jwt_required
from flask_apispec import use_kwargs, marshal_with
from api.exceptions import NotFoundException
from .models import TargetExchange
from .serializers import target_exchange_schema, target_exchanges_schema
blueprint = Blueprint('target_exchange', __name__)
@blueprint.route('', methods=['GET'])
@jwt_required()
@marshal_with(target_exchanges_schema)
def get_list():
return TargetExchange.query.all()
@blueprint.route('', methods=['POST'])
@jwt_required()
@use_kwargs(target_exchange_schema)
@marshal_with(target_exchange_schema)
def create(name):
target_exchange = TargetExchange(name=name)
target_exchange.save()
return target_exchange
@blueprint.route('/<id>', methods=['GET'])
@jwt_required()
@marshal_with(target_exchange_schema)
def get_by_id(id):
target_exchange = TargetExchange.get_by_id(id)
if target_exchange is not None:
return target_exchange
else:
return NotFoundException(__name__)
@blueprint.route('/<id>', methods=['PUT'])
@jwt_required()
@use_kwargs(target_exchange_schema)
@marshal_with(target_exchange_schema)
def update(id, **kwargs):
target_exchange = TargetExchange.get_by_id(id)
if target_exchange is not None:
return target_exchange.update(**kwargs)
else:
return NotFoundException(__name__)