2022-12-28 11:43:36 +01:00
|
|
|
import os
|
2021-01-02 19:28:03 +01:00
|
|
|
from typing import Dict
|
|
|
|
|
2020-05-02 22:00:11 +02:00
|
|
|
from flask import current_app
|
2021-11-13 19:46:12 +01:00
|
|
|
from sqlalchemy import exc
|
2021-01-02 19:28:03 +01:00
|
|
|
from sqlalchemy.engine.base import Connection
|
2020-05-02 22:00:11 +02:00
|
|
|
from sqlalchemy.event import listens_for
|
2023-10-02 18:57:53 +02:00
|
|
|
from sqlalchemy.orm import mapped_column
|
2021-01-02 19:28:03 +01:00
|
|
|
from sqlalchemy.orm.mapper import Mapper
|
|
|
|
from sqlalchemy.orm.session import Session
|
2023-10-02 18:57:53 +02:00
|
|
|
from sqlalchemy.sql import text
|
2019-11-13 18:40:01 +01:00
|
|
|
|
2021-11-01 10:33:34 +01:00
|
|
|
from fittrackee import VERSION, db
|
2021-01-20 16:47:00 +01:00
|
|
|
from fittrackee.users.models import User
|
2019-11-13 18:40:01 +01:00
|
|
|
|
2021-01-02 19:28:03 +01:00
|
|
|
|
2023-10-02 18:57:53 +02:00
|
|
|
class AppConfig(db.Model): # type: ignore
|
2019-11-13 18:40:01 +01:00
|
|
|
__tablename__ = 'app_config'
|
2023-10-02 18:57:53 +02:00
|
|
|
id = mapped_column(db.Integer, primary_key=True, autoincrement=True)
|
|
|
|
max_users = mapped_column(db.Integer, default=0, nullable=False)
|
|
|
|
gpx_limit_import = mapped_column(db.Integer, default=10, nullable=False)
|
|
|
|
max_single_file_size = mapped_column(
|
2019-11-13 18:40:01 +01:00
|
|
|
db.Integer, default=1048576, nullable=False
|
|
|
|
)
|
2023-10-02 18:57:53 +02:00
|
|
|
max_zip_file_size = mapped_column(
|
|
|
|
db.Integer, default=10485760, nullable=False
|
|
|
|
)
|
|
|
|
admin_contact = mapped_column(db.String(255), nullable=True)
|
|
|
|
privacy_policy_date = mapped_column(db.DateTime, nullable=True)
|
|
|
|
privacy_policy = mapped_column(db.Text, nullable=True)
|
|
|
|
about = mapped_column(db.Text, nullable=True)
|
2019-11-13 18:40:01 +01:00
|
|
|
|
|
|
|
@property
|
2021-01-02 19:28:03 +01:00
|
|
|
def is_registration_enabled(self) -> bool:
|
2021-11-13 19:46:12 +01:00
|
|
|
try:
|
|
|
|
nb_users = User.query.count()
|
|
|
|
except exc.ProgrammingError as e:
|
|
|
|
# workaround for user model related migrations
|
|
|
|
if 'psycopg2.errors.UndefinedColumn' in str(e):
|
2023-10-02 18:57:53 +02:00
|
|
|
query = db.session.execute(text("SELECT COUNT(*) FROM users;"))
|
|
|
|
result = query.fetchone()
|
|
|
|
nb_users = result[0] if result else 0
|
2021-11-13 19:46:12 +01:00
|
|
|
else:
|
|
|
|
raise e
|
2019-11-13 21:46:24 +01:00
|
|
|
return self.max_users == 0 or nb_users < self.max_users
|
2019-11-13 18:40:01 +01:00
|
|
|
|
2020-09-16 13:01:15 +02:00
|
|
|
@property
|
2021-01-02 19:28:03 +01:00
|
|
|
def map_attribution(self) -> str:
|
2020-09-16 13:01:15 +02:00
|
|
|
return current_app.config['TILE_SERVER']['ATTRIBUTION']
|
|
|
|
|
2021-01-02 19:28:03 +01:00
|
|
|
def serialize(self) -> Dict:
|
2022-12-28 11:43:36 +01:00
|
|
|
weather_provider = os.getenv('WEATHER_API_PROVIDER', '').lower()
|
2019-11-13 18:40:01 +01:00
|
|
|
return {
|
2023-02-25 14:06:49 +01:00
|
|
|
'about': self.about,
|
2022-03-23 18:29:49 +01:00
|
|
|
'admin_contact': self.admin_contact,
|
2021-01-02 19:28:03 +01:00
|
|
|
'gpx_limit_import': self.gpx_limit_import,
|
2022-04-23 18:04:20 +02:00
|
|
|
'is_email_sending_enabled': current_app.config['CAN_SEND_EMAILS'],
|
2021-01-02 19:28:03 +01:00
|
|
|
'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,
|
2023-02-25 14:06:49 +01:00
|
|
|
'privacy_policy': self.privacy_policy,
|
|
|
|
'privacy_policy_date': self.privacy_policy_date,
|
2021-11-01 10:33:34 +01:00
|
|
|
'version': VERSION,
|
2022-12-28 11:43:36 +01:00
|
|
|
'weather_provider': (
|
|
|
|
weather_provider
|
|
|
|
if weather_provider in ['darksky', 'visualcrossing']
|
|
|
|
else None
|
|
|
|
),
|
2019-11-13 18:40:01 +01:00
|
|
|
}
|
2020-05-02 22:00:11 +02:00
|
|
|
|
|
|
|
|
2021-01-02 19:28:03 +01:00
|
|
|
def update_app_config() -> None:
|
2020-05-02 22:00:11 +02:00
|
|
|
config = AppConfig.query.first()
|
|
|
|
if config:
|
|
|
|
current_app.config[
|
|
|
|
'is_registration_enabled'
|
|
|
|
] = config.is_registration_enabled
|
|
|
|
|
|
|
|
|
|
|
|
@listens_for(User, 'after_insert')
|
2021-01-02 19:28:03 +01:00
|
|
|
def on_user_insert(mapper: Mapper, connection: Connection, user: User) -> None:
|
2020-05-02 22:00:11 +02:00
|
|
|
@listens_for(db.Session, 'after_flush', once=True)
|
2021-01-02 19:28:03 +01:00
|
|
|
def receive_after_flush(session: Session, context: Connection) -> None:
|
2020-05-02 22:00:11 +02:00
|
|
|
update_app_config()
|
|
|
|
|
|
|
|
|
|
|
|
@listens_for(User, 'after_delete')
|
2021-01-02 19:28:03 +01:00
|
|
|
def on_user_delete(
|
|
|
|
mapper: Mapper, connection: Connection, old_user: User
|
|
|
|
) -> None:
|
2020-05-02 22:00:11 +02:00
|
|
|
@listens_for(db.Session, 'after_flush', once=True)
|
2021-01-02 19:28:03 +01:00
|
|
|
def receive_after_flush(session: Session, context: Connection) -> None:
|
2020-05-02 22:00:11 +02:00
|
|
|
update_app_config()
|