-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Configuration is now moved in `config/taxhub_config.toml`. Include new parameter `API_PREFIX` for retro-compatibility for GeoNature-Atlas and Occtax-mobile. Include a marshmallow validation schema. --------- Co-authored-by: Jacques Fize <[email protected]>
- Loading branch information
1 parent
1985fb7
commit da6f9dd
Showing
12 changed files
with
134 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,4 +81,4 @@ target/ | |
|
||
/docs/changelog.html | ||
|
||
/apptax/*_config.py | ||
config/*_config.toml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,8 @@ | ||
taxhub_routes = [ | ||
("apptax.taxonomie.routesbibnoms:adresses", "/api/bibnoms"), | ||
("apptax.taxonomie.routestaxref:adresses", "/api/taxref"), | ||
("apptax.taxonomie.routesbibattributs:adresses", "/api/bibattributs"), | ||
("apptax.taxonomie.routesbiblistes:adresses", "/api/biblistes"), | ||
("apptax.taxonomie.routestmedias:adresses", "/api/tmedias"), | ||
("apptax.taxonomie.routesbdcstatuts:adresses", "/api/bdc_statuts"), | ||
("apptax.admin.admin:adresses", "/"), | ||
taxhub_api_routes = [ | ||
("apptax.taxonomie.routesbibnoms:adresses", "/bibnoms"), | ||
("apptax.taxonomie.routestaxref:adresses", "/taxref"), | ||
("apptax.taxonomie.routesbibattributs:adresses", "/bibattributs"), | ||
("apptax.taxonomie.routesbiblistes:adresses", "/biblistes"), | ||
("apptax.taxonomie.routestmedias:adresses", "/tmedias"), | ||
("apptax.taxonomie.routesbdcstatuts:adresses", "/bdc_statuts"), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
""" | ||
Description des options de configuration | ||
""" | ||
|
||
from marshmallow import Schema, fields, validates_schema, ValidationError, post_load, pre_load | ||
from marshmallow.validate import OneOf, Regexp, Email, Length | ||
|
||
|
||
class TaxhubApiConf(Schema): | ||
API_PREFIX = fields.String( | ||
load_default="", | ||
validate=Regexp( | ||
r"(^\/(.+)$)|(^\s*$)", | ||
error="API_PREFIX must start with a slash.", | ||
), | ||
) | ||
|
||
|
||
class TaxhubSchemaConf(Schema): | ||
SQLALCHEMY_DATABASE_URI = fields.String( | ||
required=True, | ||
validate=Regexp( | ||
r"^(postgres(?:ql)?)((\+psycopg2)?):\/\/(?:([^@\s]+)@)?([^\/\s]+)(?:\/(\w+))?(?:\?(.+))?", | ||
error="PostgreSQL database URL is invalid. Check for authorized URL here : https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING-URIS", | ||
), | ||
) | ||
SQLALCHEMY_TRACK_MODIFICATIONS = fields.Boolean(load_default=True) | ||
SESSION_TYPE = fields.String(load_default="filesystem") | ||
SECRET_KEY = fields.String(required=True, validate=Length(min=20)) | ||
CODE_APPLICATION = fields.String(load_default="TH") | ||
# le cookie expire toute les 7 jours par défaut | ||
COOKIE_EXPIRATION = fields.Integer(load_default=3600 * 24 * 7) | ||
COOKIE_AUTORENEW = fields.Boolean(load_default=True) | ||
TRAP_ALL_EXCEPTIONS = fields.Boolean(load_default=False) | ||
APPLICATION_ROOT = fields.String(load_default="/") | ||
MEDIA_FOLDER = fields.String(load_default="media") | ||
PASS_METHOD = fields.String(load_default="hash") | ||
FLASK_ADMIN_SWATCH = fields.String(load_default="cerulean") | ||
FLASK_ADMIN_FLUID_LAYOUT = fields.Boolean(load_default=True) | ||
API = fields.Nested(TaxhubApiConf, load_default=TaxhubApiConf().load({})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from pathlib import Path | ||
|
||
import toml | ||
from marshmallow import EXCLUDE | ||
from marshmallow.exceptions import ValidationError | ||
|
||
|
||
class ConfigError(Exception): | ||
""" | ||
Configuration error class | ||
Quand un fichier de configuration n'est pas conforme aux attentes | ||
""" | ||
|
||
def __init__(self, file, value): | ||
self.value = value | ||
self.file = file | ||
|
||
def __str__(self): | ||
msg = "Error in the config file '{}'. Fix the following:\n" | ||
msg = msg.format(self.file) | ||
for key, errors in self.value.items(): | ||
msg += "\n\t{}:\n\t\t- {}".format(key, errors) | ||
return msg | ||
|
||
|
||
def load_and_validate_toml(toml_file, config_schema, partial=None): | ||
""" | ||
Fonction qui charge un fichier toml | ||
et le valide avec un Schema marshmallow | ||
""" | ||
if toml_file: | ||
toml_config = load_toml(toml_file) | ||
else: | ||
toml_config = {} | ||
try: | ||
configs_py = config_schema().load(toml_config, unknown=EXCLUDE, partial=partial) | ||
except ValidationError as e: | ||
raise ConfigError(toml_file, e.messages) | ||
return configs_py | ||
|
||
|
||
def load_toml(toml_file): | ||
""" | ||
Fonction qui charge un fichier toml | ||
""" | ||
if not Path(toml_file).is_file(): | ||
raise Exception("Missing file {}".format(toml_file)) | ||
return toml.load(str(toml_file)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
|
||
############################################# | ||
# Taxhub backend global configuration file | ||
############################################# | ||
|
||
# Database | ||
SQLALCHEMY_DATABASE_URI = "postgresql://monuser:monpassachanger@localhost:5432/mabase" | ||
|
||
# Remplacer par une clé alétoire complexe | ||
SECRET_KEY = 'super secret key' | ||
|
||
# Configuration liée à l'api de taxhub | ||
[API] | ||
API_PREFIX = "/api" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,5 @@ psycopg2 | |
python-dotenv | ||
Pillow<10.0.0 | ||
urllib3 | ||
click>=8.1.3 | ||
click>=8.1.3 | ||
toml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters