FitTrackee/fittrackee/application/app_config.py

180 lines
4.9 KiB
Python
Raw Normal View History

2021-01-02 19:28:03 +01:00
from typing import Dict, Union
2021-01-20 16:47:00 +01:00
from flask import Blueprint, current_app, request
from sqlalchemy.orm.exc import MultipleResultsFound, NoResultFound
2021-01-01 16:39:25 +01:00
from fittrackee import db
from fittrackee.responses import (
2021-01-02 19:28:03 +01:00
HttpResponse,
2021-01-01 16:39:25 +01:00
InvalidPayloadErrorResponse,
handle_error_and_return_response,
)
2021-01-20 16:47:00 +01:00
from fittrackee.users.decorators import authenticate_as_admin
from .models import AppConfig
from .utils import update_app_config_from_database, verify_app_config
config_blueprint = Blueprint('config', __name__)
@config_blueprint.route('/config', methods=['GET'])
2021-01-02 19:28:03 +01:00
def get_application_config() -> Union[Dict, HttpResponse]:
"""
Get Application config
**Example request**:
.. sourcecode:: http
GET /api/config HTTP/1.1
Content-Type: application/json
**Example response**:
.. sourcecode:: http
HTTP/1.1 200 OK
Content-Type: application/json
{
"data": {
"gpx_limit_import": 10,
"is_registration_enabled": false,
"max_single_file_size": 1048576,
"max_zip_file_size": 10485760,
2020-09-19 13:56:14 +02:00
"max_users": 0,
"map_attribution": "&copy; <a href=http://www.openstreetmap.org/copyright>OpenStreetMap</a> contributors"
2021-11-30 09:42:09 +01:00
"version": "0.5.1"
},
"status": "success"
}
:statuscode 200: success
:statuscode 500: error on getting configuration
"""
try:
config = AppConfig.query.one()
2021-01-01 16:39:25 +01:00
return {'status': 'success', 'data': config.serialize()}
except (MultipleResultsFound, NoResultFound) as e:
2021-01-01 16:39:25 +01:00
return handle_error_and_return_response(
e, message='error on getting configuration'
2021-01-01 16:39:25 +01:00
)
@config_blueprint.route('/config', methods=['PATCH'])
@authenticate_as_admin
2021-01-02 19:28:03 +01:00
def update_application_config(auth_user_id: int) -> Union[Dict, HttpResponse]:
"""
Update Application config
Authenticated user must be an admin
**Example request**:
.. sourcecode:: http
GET /api/config HTTP/1.1
Content-Type: application/json
**Example response**:
.. sourcecode:: http
HTTP/1.1 200 OK
Content-Type: application/json
{
"data": {
"gpx_limit_import": 10,
"is_registration_enabled": true,
"max_single_file_size": 1048576,
"max_zip_file_size": 10485760,
"max_users": 10
},
"status": "success"
}
:param integer auth_user_id: authenticate user id (from JSON Web Token)
:<json integer gpx_limit_import: max number of files in zip archive
:<json boolean is_registration_enabled: is registration enabled ?
:<json integer max_single_file_size: max size of a single file
:<json integer max_zip_file_size: max size of a zip archive
:<json integer max_users: max users allowed to register on instance
:reqheader Authorization: OAuth 2.0 Bearer Token
:statuscode 200: success
:statuscode 400: invalid payload
:statuscode 401:
- provide a valid auth token
- signature expired, please log in again
- invalid token, please log in again
:statuscode 403: you do not have permissions
:statuscode 500: error when updating configuration
"""
config_data = request.get_json()
if not config_data:
2021-01-01 16:39:25 +01:00
return InvalidPayloadErrorResponse()
ret = verify_app_config(config_data)
if ret:
return InvalidPayloadErrorResponse(message=ret)
try:
config = AppConfig.query.one()
if 'gpx_limit_import' in config_data:
config.gpx_limit_import = config_data.get('gpx_limit_import')
if 'max_single_file_size' in config_data:
config.max_single_file_size = config_data.get(
'max_single_file_size'
)
if 'max_zip_file_size' in config_data:
config.max_zip_file_size = config_data.get('max_zip_file_size')
if 'max_users' in config_data:
config.max_users = config_data.get('max_users')
if config.max_zip_file_size < config.max_single_file_size:
return InvalidPayloadErrorResponse(
'Max. size of zip archive must be equal or greater than '
'max. size of uploaded files'
)
db.session.commit()
update_app_config_from_database(current_app, config)
2021-01-01 16:39:25 +01:00
return {'status': 'success', 'data': config.serialize()}
except Exception as e:
2021-01-01 16:39:25 +01:00
return handle_error_and_return_response(
e, message='error when updating configuration'
2021-01-01 16:39:25 +01:00
)
2020-05-01 18:10:34 +02:00
@config_blueprint.route('/ping', methods=['GET'])
2021-01-02 19:28:03 +01:00
def health_check() -> Union[Dict, HttpResponse]:
2020-09-16 11:09:32 +02:00
"""health check endpoint
2020-05-01 18:10:34 +02:00
**Example request**:
.. sourcecode:: http
GET /api/ping HTTP/1.1
Content-Type: application/json
**Example response**:
.. sourcecode:: http
HTTP/1.1 200 OK
Content-Type: application/json
{
"message": "pong!",
"status": "success"
}
:statuscode 200: success
"""
2021-01-01 16:39:25 +01:00
return {'status': 'success', 'message': 'pong!'}