2023-02-25 14:06:49 +01:00
|
|
|
from datetime import datetime
|
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
|
2022-05-27 15:51:40 +02:00
|
|
|
from fittrackee.oauth2.server import require_auth
|
2021-01-01 16:39:25 +01:00
|
|
|
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-12-01 19:22:47 +01:00
|
|
|
from fittrackee.users.models import User
|
2022-03-23 18:29:49 +01:00
|
|
|
from fittrackee.users.utils.controls import is_valid_email
|
2019-11-13 18:40:01 +01:00
|
|
|
|
|
|
|
from .models import AppConfig
|
2021-02-20 16:59:31 +01:00
|
|
|
from .utils import update_app_config_from_database, verify_app_config
|
2019-11-13 18:40:01 +01:00
|
|
|
|
|
|
|
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]:
|
2019-11-13 18:40:01 +01:00
|
|
|
"""
|
2022-07-14 18:36:19 +02:00
|
|
|
Get Application configuration.
|
2019-11-13 18:40:01 +01:00
|
|
|
|
|
|
|
**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": {
|
2023-02-25 14:06:49 +01:00
|
|
|
"about": null,
|
2022-04-03 19:47:40 +02:00
|
|
|
"admin_contact": "admin@example.com",
|
2019-11-13 18:40:01 +01:00
|
|
|
"gpx_limit_import": 10,
|
2022-04-23 18:04:20 +02:00
|
|
|
"is_email_sending_enabled": true,
|
2019-11-13 18:40:01 +01:00
|
|
|
"is_registration_enabled": false,
|
|
|
|
"max_single_file_size": 1048576,
|
2020-09-19 13:56:14 +02:00
|
|
|
"max_users": 0,
|
2022-04-03 19:47:40 +02:00
|
|
|
"max_zip_file_size": 10485760,
|
2022-12-28 11:43:36 +01:00
|
|
|
"map_attribution": "© <a href=http://www.openstreetmap.org/copyright>OpenStreetMap</a> contributors",
|
2023-02-25 14:06:49 +01:00
|
|
|
"privacy_policy": null,
|
|
|
|
"privacy_policy_date": null,
|
2023-07-22 11:36:39 +02:00
|
|
|
"version": "0.7.20",
|
2022-12-28 14:41:33 +01:00
|
|
|
"weather_provider": null
|
2019-11-13 18:40:01 +01:00
|
|
|
},
|
|
|
|
"status": "success"
|
|
|
|
}
|
|
|
|
|
2023-06-18 20:45:39 +02:00
|
|
|
:statuscode 200: ``success``
|
|
|
|
:statuscode 500: ``error on getting configuration``
|
2019-11-13 18:40:01 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
|
|
|
config = AppConfig.query.one()
|
2021-01-01 16:39:25 +01:00
|
|
|
return {'status': 'success', 'data': config.serialize()}
|
2019-11-13 18:40:01 +01:00
|
|
|
except (MultipleResultsFound, NoResultFound) as e:
|
2021-01-01 16:39:25 +01:00
|
|
|
return handle_error_and_return_response(
|
2021-11-01 09:44:10 +01:00
|
|
|
e, message='error on getting configuration'
|
2021-01-01 16:39:25 +01:00
|
|
|
)
|
2019-11-13 18:40:01 +01:00
|
|
|
|
|
|
|
|
|
|
|
@config_blueprint.route('/config', methods=['PATCH'])
|
2022-06-15 19:16:14 +02:00
|
|
|
@require_auth(scopes=['application:write'], as_admin=True)
|
2021-12-01 19:22:47 +01:00
|
|
|
def update_application_config(auth_user: User) -> Union[Dict, HttpResponse]:
|
2019-11-13 18:40:01 +01:00
|
|
|
"""
|
2022-07-14 18:36:19 +02:00
|
|
|
Update Application configuration.
|
2019-11-13 18:40:01 +01:00
|
|
|
|
2022-07-14 18:36:19 +02:00
|
|
|
Authenticated user must be an admin.
|
|
|
|
|
|
|
|
**Scope**: ``application:write``
|
2019-11-13 18:40:01 +01:00
|
|
|
|
|
|
|
**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": {
|
2023-02-25 14:06:49 +01:00
|
|
|
"about": null,
|
2022-03-23 18:29:49 +01:00
|
|
|
"admin_contact": "admin@example.com",
|
2019-11-13 18:40:01 +01:00
|
|
|
"gpx_limit_import": 10,
|
2022-04-23 18:04:20 +02:00
|
|
|
"is_email_sending_enabled": true,
|
2022-04-03 19:47:40 +02:00
|
|
|
"is_registration_enabled": false,
|
2019-11-13 18:40:01 +01:00
|
|
|
"max_single_file_size": 1048576,
|
2022-04-03 19:47:40 +02:00
|
|
|
"max_users": 10,
|
2019-11-13 18:40:01 +01:00
|
|
|
"max_zip_file_size": 10485760,
|
2022-12-28 11:43:36 +01:00
|
|
|
"map_attribution": "© <a href=http://www.openstreetmap.org/copyright>OpenStreetMap</a> contributors",
|
2023-02-25 14:06:49 +01:00
|
|
|
"privacy_policy": null,
|
|
|
|
"privacy_policy_date": null,
|
2023-07-22 11:36:39 +02:00
|
|
|
"version": "0.7.20",
|
2022-12-28 14:41:33 +01:00
|
|
|
"weather_provider": null
|
2019-11-13 18:40:01 +01:00
|
|
|
},
|
|
|
|
"status": "success"
|
|
|
|
}
|
|
|
|
|
2023-02-25 14:06:49 +01:00
|
|
|
:<json string about: instance information
|
2022-03-23 18:29:49 +01:00
|
|
|
:<json string admin_contact: email to contact the administrator
|
2021-02-20 16:59:31 +01:00
|
|
|
:<json integer gpx_limit_import: max number of files in zip archive
|
2022-07-14 18:36:19 +02:00
|
|
|
:<json boolean is_registration_enabled: is registration enabled?
|
2021-02-20 16:59:31 +01:00
|
|
|
:<json integer max_single_file_size: max size of a single file
|
|
|
|
:<json integer max_users: max users allowed to register on instance
|
2022-04-03 19:47:40 +02:00
|
|
|
:<json integer max_zip_file_size: max size of a zip archive
|
2023-02-25 14:06:49 +01:00
|
|
|
:<json string privacy_policy: instance privacy policy
|
2019-11-13 18:40:01 +01:00
|
|
|
|
|
|
|
:reqheader Authorization: OAuth 2.0 Bearer Token
|
|
|
|
|
2023-06-18 20:45:39 +02:00
|
|
|
:statuscode 200: ``success``
|
|
|
|
:statuscode 400: ``invalid payload``
|
2019-11-13 18:40:01 +01:00
|
|
|
:statuscode 401:
|
2023-06-18 20:45:39 +02:00
|
|
|
- ``provide a valid auth token``
|
|
|
|
- ``signature expired, please log in again``
|
|
|
|
- ``invalid token, please log in again``
|
|
|
|
- ``valid email must be provided for admin contact``
|
|
|
|
:statuscode 403: ``you do not have permissions``
|
|
|
|
:statuscode 500: ``error when updating configuration``
|
2019-11-13 18:40:01 +01:00
|
|
|
"""
|
|
|
|
config_data = request.get_json()
|
|
|
|
if not config_data:
|
2021-01-01 16:39:25 +01:00
|
|
|
return InvalidPayloadErrorResponse()
|
2019-11-13 18:40:01 +01:00
|
|
|
|
2021-02-20 16:59:31 +01:00
|
|
|
ret = verify_app_config(config_data)
|
2022-03-23 18:29:49 +01:00
|
|
|
admin_contact = config_data.get('admin_contact')
|
|
|
|
if admin_contact and not is_valid_email(admin_contact):
|
|
|
|
ret.append('valid email must be provided for admin contact')
|
2021-02-20 16:59:31 +01:00
|
|
|
if ret:
|
|
|
|
return InvalidPayloadErrorResponse(message=ret)
|
|
|
|
|
2019-11-13 18:40:01 +01:00
|
|
|
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')
|
2022-03-23 18:29:49 +01:00
|
|
|
if 'admin_contact' in config_data:
|
|
|
|
config.admin_contact = admin_contact if admin_contact else None
|
2023-02-25 14:06:49 +01:00
|
|
|
if 'about' in config_data:
|
2023-02-26 09:02:18 +01:00
|
|
|
config.about = (
|
|
|
|
config_data.get('about') if config_data.get('about') else None
|
|
|
|
)
|
2023-02-25 14:06:49 +01:00
|
|
|
if 'privacy_policy' in config_data:
|
2023-02-26 09:02:18 +01:00
|
|
|
privacy_policy = config_data.get('privacy_policy')
|
|
|
|
config.privacy_policy = privacy_policy if privacy_policy else None
|
|
|
|
config.privacy_policy_date = (
|
|
|
|
datetime.utcnow() if privacy_policy else None
|
|
|
|
)
|
2019-11-13 18:40:01 +01:00
|
|
|
|
2021-02-20 14:14:26 +01:00
|
|
|
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'
|
|
|
|
)
|
2019-11-13 18:40:01 +01:00
|
|
|
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()}
|
2019-11-13 18:40:01 +01:00
|
|
|
|
|
|
|
except Exception as e:
|
2021-01-01 16:39:25 +01:00
|
|
|
return handle_error_and_return_response(
|
2021-11-01 09:44:10 +01:00
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
2023-06-18 20:45:39 +02:00
|
|
|
:statuscode 200: ``success``
|
2020-05-01 18:10:34 +02:00
|
|
|
"""
|
2021-01-01 16:39:25 +01:00
|
|
|
return {'status': 'success', 'message': 'pong!'}
|