From 98a8747821997ad9c8cec9170b6297b0ab3c1df6 Mon Sep 17 00:00:00 2001 From: Gardient Date: Wed, 22 Sep 2021 19:24:48 +0300 Subject: [PATCH] formatting --- api/__init__.py | 6 ++++-- api/commands.py | 6 ++++-- api/constants.py | 4 ++-- api/database.py | 5 +++-- api/exceptions.py | 3 +++ api/extensions.py | 4 +++- 6 files changed, 19 insertions(+), 9 deletions(-) diff --git a/api/__init__.py b/api/__init__.py index b8f272f..5da89db 100644 --- a/api/__init__.py +++ b/api/__init__.py @@ -1,8 +1,9 @@ from flask import Flask, Blueprint + from . import commands, login, target_exchange, target -from .settings import ProdConfig, Config -from .extensions import db, migrate, jwt, apispec from .exceptions import ApiException +from .extensions import db, migrate, jwt, apispec +from .settings import ProdConfig, Config def create_app(config: Config = ProdConfig) -> Flask: @@ -78,6 +79,7 @@ def register_errorhandlers(app: Flask): def register_shellcontext(app: Flask): """Register shell context objects.""" + def shell_context(): """Shell context objects.""" return { diff --git a/api/commands.py b/api/commands.py index 15a9797..67ec59f 100644 --- a/api/commands.py +++ b/api/commands.py @@ -1,12 +1,13 @@ import os -import click +import click from flask import current_app from flask.cli import with_appcontext from werkzeug.exceptions import MethodNotAllowed, NotFound from .target_exchange.models import TargetExchange + @click.command() def clean(): """Remove *.pyc and *.pyo files recursively starting at current directory. @@ -39,7 +40,7 @@ def urls(url, order): try: rule, arguments = ( current_app.url_map.bind('localhost') - .match(url, return_rule=True)) + .match(url, return_rule=True)) rows.append((rule.rule, rule.endpoint, arguments)) column_length = 3 except (NotFound, MethodNotAllowed) as e: @@ -82,6 +83,7 @@ def urls(url, order): for row in rows: click.echo(str_template.format(*row[:column_length])) + @click.command() @with_appcontext def seed(): diff --git a/api/constants.py b/api/constants.py index 30b4582..2e37b3c 100644 --- a/api/constants.py +++ b/api/constants.py @@ -1,2 +1,2 @@ -API_PASS='API_PASS' -API_USER='super' +API_PASS = 'API_PASS' +API_USER = 'super' diff --git a/api/database.py b/api/database.py index 49dbb31..8ff9061 100644 --- a/api/database.py +++ b/api/database.py @@ -9,6 +9,7 @@ Column = db.Column relationship = relationship Model = db.Model + # From Mike Bayer's "Building the app" talk # https://speakerdeck.com/zzzeek/building-the-app class SurrogatePK(object): @@ -24,8 +25,8 @@ class SurrogatePK(object): def get_by_id(cls, record_id): """Get record by ID.""" if any( - (isinstance(record_id, (str, bytes)) and record_id.isdigit(), - isinstance(record_id, (int, float))), + (isinstance(record_id, (str, bytes)) and record_id.isdigit(), + isinstance(record_id, (int, float))), ): return cls.query.get(int(record_id)) diff --git a/api/exceptions.py b/api/exceptions.py index 21d9e11..8835ceb 100644 --- a/api/exceptions.py +++ b/api/exceptions.py @@ -1,5 +1,6 @@ from flask import jsonify + class ApiException(Exception): status_code = 500 @@ -13,10 +14,12 @@ class ApiException(Exception): rv.status_code = self.status_code return rv + class NotFoundException(ApiException): def __init__(self, entity_name) -> None: super().__init__(404, f'{entity_name} could not be found') + class BadRequestException(ApiException): def __init__(self, message) -> None: super().__init__(400, message) diff --git a/api/extensions.py b/api/extensions.py index e07de38..9392df1 100644 --- a/api/extensions.py +++ b/api/extensions.py @@ -1,7 +1,8 @@ +from flask_apispec import FlaskApiSpec from flask_jwt_extended import JWTManager from flask_migrate import Migrate from flask_sqlalchemy import SQLAlchemy, Model -from flask_apispec import FlaskApiSpec + class CRUDMixin(Model): """Mixin that adds convenience methods for CRUD (create, read, update, delete) operations.""" @@ -30,6 +31,7 @@ class CRUDMixin(Model): db.session.delete(self) return commit and db.session.commit() + db = SQLAlchemy(model_class=CRUDMixin) migrate = Migrate() jwt = JWTManager()