from typing import Dict, Union
from flask import Blueprint, current_app, request
from sqlalchemy.orm.exc import MultipleResultsFound, NoResultFound
from fittrackee import db
from fittrackee.responses import (
HttpResponse,
InvalidPayloadErrorResponse,
handle_error_and_return_response,
)
from fittrackee.users.decorators import authenticate_as_admin
from fittrackee.users.models import User
from fittrackee.users.utils.controls import is_valid_email
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'])
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": {
"admin_contact": "admin@example.com",
"gpx_limit_import": 10,
"is_email_sending_enabled": true,
"is_registration_enabled": false,
"max_single_file_size": 1048576,
"max_users": 0,
"max_zip_file_size": 10485760,
"map_attribution": "© OpenStreetMap contributors"
"version": "0.6.12"
},
"status": "success"
}
:statuscode 200: success
:statuscode 500: error on getting configuration
"""
try:
config = AppConfig.query.one()
return {'status': 'success', 'data': config.serialize()}
except (MultipleResultsFound, NoResultFound) as e:
return handle_error_and_return_response(
e, message='error on getting configuration'
)
@config_blueprint.route('/config', methods=['PATCH'])
@authenticate_as_admin
def update_application_config(auth_user: User) -> 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": {
"admin_contact": "admin@example.com",
"gpx_limit_import": 10,
"is_email_sending_enabled": true,
"is_registration_enabled": false,
"max_single_file_size": 1048576,
"max_users": 10,
"max_zip_file_size": 10485760,
"map_attribution": "© OpenStreetMap contributors"
"version": "0.6.12"
},
"status": "success"
}
: Union[Dict, HttpResponse]:
"""health check endpoint
**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
"""
return {'status': 'success', 'message': 'pong!'}