Adists: DIF: Update for scenario.

scenarios
Pierre-Antoine 2025-08-14 16:07:01 +02:00
parent 45fddb9459
commit 31af83ba02
2 changed files with 95 additions and 56 deletions

View File

@ -105,43 +105,64 @@ class DIFAdisTS(SQLSubModel):
@classmethod
def _db_load(cls, execute, data=None):
new = []
status = data['status']
scenario = data["scenario"]
loaded = data['loaded_pid']
table = execute(
"SELECT pamhyr_id, name, method, dif, b, c, enabled " +
"FROM dif_adists"
"SELECT pamhyr_id, name, method, dif, b, c, enabled, scenario " +
"FROM dif_adists " +
f"WHERE scenario = {scenario.id} " +
f"AND pamhyr_id NOT IN ({', '.join(map(str, loaded))}) "
)
if table is not None:
for row in table:
dif_id = row[0]
name = row[1]
method = row[2]
dif = row[3]
b = row[4]
c = row[5]
enabled = (row[6] == 1)
it = iter(row)
DIF = cls(
id=dif_id,
pid = next(it)
name = next(it)
method = next(it)
dif = next(it)
b = next(it)
c = next(it)
enabled = (next(it) == 1)
owner_scenario = next(it)
dif = cls(
id=pid,
name=name,
status=data['status']
status=status,
owner_scenario=owner_scenario,
)
DIF.method = method
DIF.dif = dif
DIF.b = b
DIF.c = c
DIF.enabled = enabled
dif.method = method
dif.dif = dif
dif.b = b
dif.c = c
dif.enabled = enabled
data['dif_default_id'] = dif_id
DIF._data = DIFAdisTSSpec._db_load(execute, data)
data['dif_default_id'] = pid
dif._data = difAdisTSSpec._db_load(execute, data)
new.append(DIF)
loaded.add(pid)
new.append(dif)
data["scenario"] = scenario.parent
new += cls._db_load(execute, data)
data["scenario"] = scenario
return new
def _db_save(self, execute, data=None):
execute(f"DELETE FROM dif_adists WHERE id = {self.id}")
if not self.must_be_saved():
return True
execute(
"DELETE FROM dif_adists " +
f"WHERE pamhyr_id = {self.id} " +
f"AND scenario = {self._status.scenario_id} "
)
method = ""
if self.method is not None:
@ -162,12 +183,13 @@ class DIFAdisTS(SQLSubModel):
sql = (
"INSERT INTO " +
"dif_adists(" +
"pamhyr_id, name, method, dif, b, c, enabled" +
"pamhyr_id, name, method, dif, b, c, enabled, scenario" +
") " +
"VALUES (" +
f"{self.id}, '{self._db_format(self._name)}', " +
f"'{self._db_format(self._method)}', " +
f"{dif}, {b}, {c}, {self._enabled}" +
f"{self._status.scenario_id}" +
")"
)
@ -176,7 +198,8 @@ class DIFAdisTS(SQLSubModel):
data['dif_default_id'] = self.id
execute(
"DELETE FROM dif_adists_spec " +
f"WHERE dif_default = {self.id}"
f"WHERE dif_default = {self.id} " +
f"AND scenario = {self._status.scenario_id} "
)
for dif_spec in self._data:
@ -194,7 +217,7 @@ class DIFAdisTS(SQLSubModel):
@name.setter
def name(self, name):
self._name = name
self._status.modified()
self.modified()
@property
def method(self):
@ -203,7 +226,7 @@ class DIFAdisTS(SQLSubModel):
@method.setter
def method(self, method):
self._method = method
self._status.modified()
self.modified()
@property
def types(self):
@ -216,7 +239,7 @@ class DIFAdisTS(SQLSubModel):
@dif.setter
def dif(self, dif):
self._dif = dif
self._status.modified()
self.modified()
@property
def b(self):
@ -225,7 +248,7 @@ class DIFAdisTS(SQLSubModel):
@b.setter
def b(self, b):
self._b = b
self._status.modified()
self.modified()
@property
def c(self):
@ -234,7 +257,7 @@ class DIFAdisTS(SQLSubModel):
@c.setter
def c(self, c):
self._c = c
self._status.modified()
self.modified()
@property
def enabled(self):
@ -243,12 +266,12 @@ class DIFAdisTS(SQLSubModel):
@enabled.setter
def enabled(self, enabled):
self._enabled = enabled
self._status.modified()
self.modified()
def new(self, index):
n = DIFAdisTSSpec(status=self._status)
self._data.insert(index, n)
self._status.modified()
self.modified()
return n
def delete(self, data):
@ -258,13 +281,13 @@ class DIFAdisTS(SQLSubModel):
self._data
)
)
self._status.modified()
self.modified()
def delete_i(self, indexes):
for ind in indexes:
del self._data[ind]
self._status.modified()
self.modified()
def insert(self, index, data):
self._data.insert(index, data)
self._status.modified()
self.modified()

View File

@ -115,30 +115,42 @@ class DIFAdisTSSpec(SQLSubModel):
@classmethod
def _db_load(cls, execute, data=None):
new = []
status = data['status']
scenario = data["scenario"]
loaded = data['loaded_pid']
if scenario is None:
return new
table = execute(
"SELECT id, dif_default, method, reach, start_rk, end_rk, " +
"dif, b, c, enabled " +
"SELECT id, method, reach, start_rk, end_rk, " +
"dif, b, c, enabled, scenario " +
"FROM dif_spec " +
f"WHERE dif_default = {data['dif_default_id']} "
f"WHERE dif_default = {data['dif_default_id']} " +
f"AND scenario = {scenario.id} " +
f"AND pamhyr_id NOT IN ({', '.join(map(str, loaded))}) "
)
if table is not None:
for row in table:
id = row[0]
method = row[2]
reach = row[3]
start_rk = row[4]
end_rk = row[5]
dif = row[6]
b = row[7]
c = row[8]
enabled = (row[9] == 1)
it = iter(row)
id = next(it)
method = next(it)
reach = next(it)
start_rk = next(it)
end_rk = next(it)
dif = next(it)
b = next(it)
c = next(it)
enabled = (next(it) == 1)
owner_scenario = next(it)
new_spec = cls(
id=id,
method=method,
status=data['status']
status=status,
owner_scenario=owner_scenario
)
new_spec.reach = reach
@ -149,31 +161,35 @@ class DIFAdisTSSpec(SQLSubModel):
new_spec.c = c
new_spec.enabled = enabled
loaded.add(pid)
new.append(new_spec)
data["scenario"] = scenario.parent
new += cls._db_load(execute, data)
data["scenario"] = scenario
return new
def _db_save(self, execute, data=None):
if not self.must_be_saved():
return True
dif_default = data['dif_default_id']
sql = (
execute(
"INSERT INTO " +
"dif_spec(id, dif_default, method, reach, " +
"start_rk, end_rk, dif, b, c, enabled) " +
"start_rk, end_rk, dif, b, c, enabled, scenario) " +
"VALUES (" +
f"{self.id}, " +
f"{dif_default}, " +
f"{self.id}, {dif_default}, " +
f"'{self._db_format(self._method)}', " +
f"{self._reach}, " +
f"{self._start_rk}, " +
f"{self._end_rk}, " +
f"{self._dif}, " +
f"{self._b}, " +
f"{self._c}, " +
f"{self._start_rk}, {self._end_rk}, " +
f"{self._dif}, {self._b}, {self._c}, " +
f"{self._enabled}" +
f"{self._status.scenario_id}" +
")"
)
execute(sql)
return True