API - add typing
This commit is contained in:
@ -1,5 +1,8 @@
|
||||
from typing import Dict, Union
|
||||
|
||||
from fittrackee import db
|
||||
from fittrackee.responses import (
|
||||
HttpResponse,
|
||||
InvalidPayloadErrorResponse,
|
||||
handle_error_and_return_response,
|
||||
)
|
||||
@ -14,7 +17,7 @@ config_blueprint = Blueprint('config', __name__)
|
||||
|
||||
|
||||
@config_blueprint.route('/config', methods=['GET'])
|
||||
def get_application_config():
|
||||
def get_application_config() -> Union[Dict, HttpResponse]:
|
||||
"""
|
||||
Get Application config
|
||||
|
||||
@ -59,7 +62,7 @@ def get_application_config():
|
||||
|
||||
@config_blueprint.route('/config', methods=['PATCH'])
|
||||
@authenticate_as_admin
|
||||
def update_application_config(auth_user_id):
|
||||
def update_application_config(auth_user_id: int) -> Union[Dict, HttpResponse]:
|
||||
"""
|
||||
Update Application config
|
||||
|
||||
@ -137,7 +140,7 @@ def update_application_config(auth_user_id):
|
||||
|
||||
|
||||
@config_blueprint.route('/ping', methods=['GET'])
|
||||
def health_check():
|
||||
def health_check() -> Union[Dict, HttpResponse]:
|
||||
"""health check endpoint
|
||||
|
||||
**Example request**:
|
||||
|
@ -1,11 +1,19 @@
|
||||
from typing import Dict
|
||||
|
||||
from fittrackee import db
|
||||
from flask import current_app
|
||||
from sqlalchemy.engine.base import Connection
|
||||
from sqlalchemy.event import listens_for
|
||||
from sqlalchemy.ext.declarative import DeclarativeMeta
|
||||
from sqlalchemy.orm.mapper import Mapper
|
||||
from sqlalchemy.orm.session import Session
|
||||
|
||||
from ..users.models import User
|
||||
|
||||
BaseModel: DeclarativeMeta = db.Model
|
||||
|
||||
class AppConfig(db.Model):
|
||||
|
||||
class AppConfig(BaseModel):
|
||||
__tablename__ = 'app_config'
|
||||
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
|
||||
max_users = db.Column(db.Integer, default=0, nullable=False)
|
||||
@ -16,26 +24,26 @@ class AppConfig(db.Model):
|
||||
max_zip_file_size = db.Column(db.Integer, default=10485760, nullable=False)
|
||||
|
||||
@property
|
||||
def is_registration_enabled(self):
|
||||
def is_registration_enabled(self) -> bool:
|
||||
nb_users = User.query.count()
|
||||
return self.max_users == 0 or nb_users < self.max_users
|
||||
|
||||
@property
|
||||
def map_attribution(self):
|
||||
def map_attribution(self) -> str:
|
||||
return current_app.config['TILE_SERVER']['ATTRIBUTION']
|
||||
|
||||
def serialize(self):
|
||||
def serialize(self) -> Dict:
|
||||
return {
|
||||
"gpx_limit_import": self.gpx_limit_import,
|
||||
"is_registration_enabled": self.is_registration_enabled,
|
||||
"max_single_file_size": self.max_single_file_size,
|
||||
"max_zip_file_size": self.max_zip_file_size,
|
||||
"max_users": self.max_users,
|
||||
"map_attribution": self.map_attribution,
|
||||
'gpx_limit_import': self.gpx_limit_import,
|
||||
'is_registration_enabled': self.is_registration_enabled,
|
||||
'max_single_file_size': self.max_single_file_size,
|
||||
'max_zip_file_size': self.max_zip_file_size,
|
||||
'max_users': self.max_users,
|
||||
'map_attribution': self.map_attribution,
|
||||
}
|
||||
|
||||
|
||||
def update_app_config():
|
||||
def update_app_config() -> None:
|
||||
config = AppConfig.query.first()
|
||||
if config:
|
||||
current_app.config[
|
||||
@ -44,14 +52,16 @@ def update_app_config():
|
||||
|
||||
|
||||
@listens_for(User, 'after_insert')
|
||||
def on_user_insert(mapper, connection, user):
|
||||
def on_user_insert(mapper: Mapper, connection: Connection, user: User) -> None:
|
||||
@listens_for(db.Session, 'after_flush', once=True)
|
||||
def receive_after_flush(session, context):
|
||||
def receive_after_flush(session: Session, context: Connection) -> None:
|
||||
update_app_config()
|
||||
|
||||
|
||||
@listens_for(User, 'after_delete')
|
||||
def on_user_delete(mapper, connection, old_user):
|
||||
def on_user_delete(
|
||||
mapper: Mapper, connection: Connection, old_user: User
|
||||
) -> None:
|
||||
@listens_for(db.Session, 'after_flush', once=True)
|
||||
def receive_after_flush(session, context):
|
||||
def receive_after_flush(session: Session, context: Connection) -> None:
|
||||
update_app_config()
|
||||
|
@ -1,14 +1,16 @@
|
||||
import os
|
||||
from typing import Tuple
|
||||
|
||||
from fittrackee import db
|
||||
from fittrackee.users.models import User
|
||||
from flask import Flask
|
||||
|
||||
from .models import AppConfig
|
||||
|
||||
MAX_FILE_SIZE = 1 * 1024 * 1024 # 1MB
|
||||
|
||||
|
||||
def init_config():
|
||||
def init_config() -> Tuple[bool, AppConfig]:
|
||||
"""
|
||||
init application configuration if not existing in database
|
||||
|
||||
@ -36,7 +38,9 @@ def init_config():
|
||||
return False, existing_config
|
||||
|
||||
|
||||
def update_app_config_from_database(current_app, db_config):
|
||||
def update_app_config_from_database(
|
||||
current_app: Flask, db_config: AppConfig
|
||||
) -> None:
|
||||
current_app.config['gpx_limit_import'] = db_config.gpx_limit_import
|
||||
current_app.config['max_single_file_size'] = db_config.max_single_file_size
|
||||
current_app.config['MAX_CONTENT_LENGTH'] = db_config.max_zip_file_size
|
||||
|
Reference in New Issue
Block a user