2023-04-20 13:10:16 +00:00
|
|
|
import os
|
2023-05-22 06:54:20 +00:00
|
|
|
import re
|
2023-04-20 13:10:16 +00:00
|
|
|
from functools import lru_cache
|
|
|
|
from pydantic import BaseSettings
|
|
|
|
from flask import Flask
|
|
|
|
|
|
|
|
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
APP_ENV = os.environ.get("APP_ENV", "development")
|
|
|
|
|
|
|
|
|
|
|
|
class BaseConfig(BaseSettings):
|
|
|
|
"""Base configuration."""
|
|
|
|
|
|
|
|
ENV: str = "base"
|
2023-04-20 15:07:54 +00:00
|
|
|
APP_NAME: str = "Open Law"
|
2023-06-21 12:22:54 +00:00
|
|
|
SERVER_NAME: str = "open-law.net"
|
2023-04-20 13:10:16 +00:00
|
|
|
SECRET_KEY: str
|
|
|
|
SQLALCHEMY_TRACK_MODIFICATIONS: bool = False
|
|
|
|
WTF_CSRF_ENABLED: bool = False
|
|
|
|
|
|
|
|
# Super admin
|
|
|
|
ADMIN_USERNAME: str
|
|
|
|
ADMIN_PASSWORD: str
|
|
|
|
|
|
|
|
# Pagination
|
|
|
|
DEFAULT_PAGE_SIZE: int
|
|
|
|
PAGE_LINKS_NUMBER: int
|
2023-04-24 13:10:16 +00:00
|
|
|
MAX_SEARCH_RESULTS: int
|
2023-04-20 13:10:16 +00:00
|
|
|
|
2023-05-04 11:18:49 +00:00
|
|
|
# HTTPProvider for SIWE
|
|
|
|
HTTP_PROVIDER_URL: str
|
|
|
|
|
2023-05-22 06:54:20 +00:00
|
|
|
# regex
|
2023-06-01 11:39:42 +00:00
|
|
|
TAG_REGEX = re.compile(r"(#+[a-zA-Z0-9(_)]{1,})") # #tag
|
|
|
|
USER_MENTION_REGEX = re.compile(r"(?<!\w)@\w+") # @word
|
2023-05-22 06:54:20 +00:00
|
|
|
|
2023-04-20 13:10:16 +00:00
|
|
|
@staticmethod
|
|
|
|
def configure(app: Flask):
|
|
|
|
# Implement this method to do further configuration on your app.
|
|
|
|
pass
|
|
|
|
|
|
|
|
class Config:
|
|
|
|
# `.env` takes priority over `project.env`
|
|
|
|
env_file = "project.env", ".env"
|
|
|
|
|
|
|
|
|
|
|
|
class DevelopmentConfig(BaseConfig):
|
|
|
|
"""Development configuration."""
|
|
|
|
|
|
|
|
DEBUG: bool = True
|
|
|
|
SQLALCHEMY_DATABASE_URI: str = "sqlite:///" + os.path.join(
|
|
|
|
BASE_DIR, "database-dev.sqlite3"
|
|
|
|
)
|
|
|
|
|
|
|
|
class Config:
|
|
|
|
fields = {
|
|
|
|
"SQLALCHEMY_DATABASE_URI": {
|
|
|
|
"env": "DEVEL_DATABASE_URL",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class TestingConfig(BaseConfig):
|
|
|
|
"""Testing configuration."""
|
|
|
|
|
|
|
|
TESTING: bool = True
|
|
|
|
PRESERVE_CONTEXT_ON_EXCEPTION: bool = False
|
|
|
|
SQLALCHEMY_DATABASE_URI: str = "sqlite:///" + os.path.join(
|
|
|
|
BASE_DIR, "database-test.sqlite3"
|
|
|
|
)
|
|
|
|
|
|
|
|
class Config:
|
|
|
|
fields = {
|
|
|
|
"SQLALCHEMY_DATABASE_URI": {
|
|
|
|
"env": "TEST_DATABASE_URL",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class ProductionConfig(BaseConfig):
|
|
|
|
"""Production configuration."""
|
|
|
|
|
|
|
|
SQLALCHEMY_DATABASE_URI: str = os.environ.get(
|
|
|
|
"DATABASE_URL", "sqlite:///" + os.path.join(BASE_DIR, "database.sqlite3")
|
|
|
|
)
|
|
|
|
WTF_CSRF_ENABLED = True
|
|
|
|
|
|
|
|
class Config:
|
|
|
|
fields = {
|
|
|
|
"SQLALCHEMY_DATABASE_URI": {
|
|
|
|
"env": "DATABASE_URL",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@lru_cache
|
|
|
|
def config(name=APP_ENV) -> DevelopmentConfig | TestingConfig | ProductionConfig:
|
|
|
|
CONF_MAP = dict(
|
|
|
|
development=DevelopmentConfig(),
|
|
|
|
testing=TestingConfig(),
|
|
|
|
production=ProductionConfig(),
|
|
|
|
)
|
|
|
|
configuration = CONF_MAP[name]
|
|
|
|
configuration.ENV = name
|
|
|
|
return configuration
|