Skip to content

Commit 9853049

Browse files
jpm-cbnaTheoLechemia
authored andcommitted
fix: build altitudes entity dynamicly
1 parent dae0b46 commit 9853049

File tree

3 files changed

+57
-21
lines changed

3 files changed

+57
-21
lines changed

atlas/configuration/config_schema.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ class Meta:
163163
AFFICHAGE_GRAPH_PHENOLOGIE = fields.Boolean(load_default=True)
164164
TYPE_TERRITOIRE_SHEET = fields.List(fields.String(), load_default=["COM"])
165165
AFFICHAGE_GRAPH_PHENOLOGIE = fields.Boolean(load_default=False)
166+
ALTITUDE_RANGES = fields.List(
167+
fields.Integer(),
168+
load_default=[0, 500, 1000, 1500, 2000, 2500, 3000, 3500, 4000],
169+
)
166170
AFFICHAGE_TOUT_TERRITOIRE_GRAPH = fields.Boolean(load_default=False)
167171
AFFICHAGE_GRAPH_PROVENANCE_DONNEE = fields.Boolean(load_default=False)
168172
COLOR_STACKED_BAR_CHARTS = fields.List(
Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,32 @@
1-
# coding: utf-8
1+
from itertools import pairwise
2+
from flask import current_app
23
from sqlalchemy.orm import Mapped, mapped_column
4+
from sqlalchemy import Integer
35
from atlas.env import db
46

57

6-
class VmAltitudes(db.Model):
7-
__tablename__ = "vm_altitudes"
8-
__table_args__ = {"schema": "atlas"}
8+
def create_vm_altitudes_class():
9+
"""
10+
Dynamically creates and returns the VmAltitudes model class
11+
based on the altitude ranges defined in the configuration.
12+
"""
13+
altitude_ranges = current_app.config["ALTITUDE_RANGES"]
14+
print("LAAA", altitude_ranges)
915

10-
cd_ref: Mapped[int] = mapped_column(primary_key=True)
11-
_0_500: Mapped[int] = mapped_column()
12-
_500_1000: Mapped[int] = mapped_column()
13-
_1000_1500: Mapped[int] = mapped_column()
14-
_1500_2000: Mapped[int] = mapped_column()
15-
_2000_2500: Mapped[int] = mapped_column()
16-
_2500_3000: Mapped[int] = mapped_column()
17-
_3000_3500: Mapped[int] = mapped_column()
18-
_3500_4000: Mapped[int] = mapped_column()
16+
class_attributes = {
17+
"__tablename__": "vm_altitudes",
18+
"__table_args__": {"schema": "atlas"},
19+
# Fixed column
20+
"cd_ref": mapped_column(Integer, primary_key=True),
21+
}
22+
23+
for alt_min, alt_max in pairwise(altitude_ranges):
24+
# The '_' prefix is necessary because a variable name cannot start with a number.
25+
column_name = f"_{alt_min}_{alt_max}"
26+
class_attributes[column_name] = mapped_column(Integer)
27+
28+
VmAltitudesClass = type("VmAltitudes", (db.Model,), class_attributes)
29+
return VmAltitudesClass
30+
31+
32+
VmAltitudes = create_vm_altitudes_class()

install_app.sh

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -214,15 +214,34 @@ function updatePythonConfigFile() {
214214
local conf_file="config.py"
215215
local conf_path="${__conf_dir__}/${conf_file}"
216216

217-
local pg_uri="postgresql:\/\/${user_pg}:${user_pg_pass}@${db_host}:${db_port}\/${db_name}"
218-
sed -i "s/SQLALCHEMY_DATABASE_URI = .*$/SQLALCHEMY_DATABASE_URI = \"${pg_uri}\"/" "${conf_path}"
219-
if [[ $? -eq 0 ]]; then
220-
printVerbose ">SQLALCHEMY_DATABASE_URI ${Gre}updated${RCol} in ${conf_file}"
217+
local pg_uri="postgresql://${user_pg}:${user_pg_pass}@${db_host}:${db_port}/${db_name}"
218+
if grep -q "SQLALCHEMY_DATABASE_URI" "${conf_path}"; then
219+
sed -i "s#SQLALCHEMY_DATABASE_URI = .*\$#SQLALCHEMY_DATABASE_URI = \"${pg_uri}\"#" "${conf_path}"
220+
if [[ $? -eq 0 ]]; then
221+
printVerbose ">SQLALCHEMY_DATABASE_URI ${Gre}updated${Gra} in ${Std}${conf_file}${Gra}"
222+
fi
223+
else
224+
echo "SQLALCHEMY_DATABASE_URI = \"${pg_uri}\"" >> "${conf_path}"
225+
local msg=">SQLALCHEMY_DATABASE_URI not found in ${Std}${conf_file}${Gra}, "
226+
msg+="${Gre}we added it to the end of the file${Gra}."
227+
printVerbose "${msg}"
221228
fi
222229

223-
sed -i "s/GUNICORN_PORT = .*$/GUNICORN_PORT = \"${gun_port}\"/" "${conf_path}"
224-
if [[ $? -eq 0 ]]; then
225-
printVerbose ">GUNICORN_PORT ${Gre}updated${RCol} in ${conf_file}"
230+
# Convert the bash array 'altitudes' into a comma-separated string
231+
local old_ifs=$IFS
232+
IFS=','
233+
local altitudes_str="${altitudes[*]}"
234+
IFS=$old_ifs
235+
if grep -q "ALTITUDE_RANGES" "${conf_path}"; then
236+
sed -i "s/ALTITUDE_RANGES = .*$/ALTITUDE_RANGES = [${altitudes_str}]/" "${conf_path}"
237+
if [[ $? -eq 0 ]]; then
238+
printVerbose ">ALTITUDE_RANGES ${Gre}updated${Gra} in ${Std}${conf_file}${Gra}"
239+
fi
240+
else
241+
echo "ALTITUDE_RANGES = [${altitudes_str}]" >> "${conf_path}"
242+
local msg=">ALTITUDE_RANGES not found in ${Std}${conf_file}${Gra}, "
243+
msg+="${Gre}we added it to the end of the file${Gra}."
244+
printVerbose "${msg}"
226245
fi
227246
}
228247

@@ -317,4 +336,3 @@ function startAtlasService() {
317336
}
318337

319338
main "${@}"
320-

0 commit comments

Comments
 (0)