mirror of
https://gitlab.com/pamhyr/pamhyr2.git
synced 2026-10-30 03:20:41 +01:00
HS: Add to scenarios.
This commit is contained in:
1 parent
9ec21d815d
commit
60ee889794
10 files changed
+296
-165
No files matched your search
@@ -37,13 +37,13 @@ class HydraulicStructure(SQLSubModel):
|
||||
_sub_classes = [
|
||||
BasicHS,
|
||||
]
|
||||
_id_cnt = 0
|
||||
|
||||
def __init__(self, id: int = -1, name: str = "",
|
||||
status=None):
|
||||
super(HydraulicStructure, self).__init__(id)
|
||||
|
||||
self._status = status
|
||||
status=None, owner_scenario=-1):
|
||||
super(HydraulicStructure, self).__init__(
|
||||
id=id, status=status,
|
||||
owner_scenario=owner_scenario
|
||||
)
|
||||
|
||||
self._name = name
|
||||
self._input_section = None
|
||||
@@ -58,6 +58,7 @@ class HydraulicStructure(SQLSubModel):
|
||||
execute(f"""
|
||||
CREATE TABLE hydraulic_structures{ext} (
|
||||
{cls.create_db_add_pamhyr_id()},
|
||||
deleted BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
name TEXT NOT NULL,
|
||||
enabled BOOLEAN NOT NULL,
|
||||
input_reach INTEGER,
|
||||
@@ -84,9 +85,9 @@ class HydraulicStructure(SQLSubModel):
|
||||
@classmethod
|
||||
def _db_update(cls, execute, version, data=None):
|
||||
major, minor, release = version.strip().split(".")
|
||||
if major == minor == "0":
|
||||
rl = int(release)
|
||||
rl = int(release)
|
||||
|
||||
if major == minor == "0":
|
||||
if rl < 6:
|
||||
cls._db_create(execute)
|
||||
return True
|
||||
@@ -103,9 +104,16 @@ class HydraulicStructure(SQLSubModel):
|
||||
cls._db_update_to_0_1_0(execute, data)
|
||||
|
||||
if major == "0" and minor == "1":
|
||||
if int(release) < 1:
|
||||
if rl < 1:
|
||||
cls._db_update_to_0_1_1(execute, data)
|
||||
|
||||
if rl < 2:
|
||||
execute(
|
||||
"ALTER TABLE hydraulic_structures " +
|
||||
"ADD COLUMN deleted BOOLEAN NOT NULL DEFAULT FALSE"
|
||||
)
|
||||
|
||||
|
||||
return cls._update_submodel(execute, version, data)
|
||||
|
||||
@classmethod
|
||||
@@ -181,70 +189,97 @@ class HydraulicStructure(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 []
|
||||
|
||||
table = execute(
|
||||
"SELECT pamhyr_id, name, enabled, " +
|
||||
"SELECT pamhyr_id, deleted, name, enabled, " +
|
||||
"input_section, output_section, " +
|
||||
"input_reach, output_reach " +
|
||||
"FROM hydraulic_structures"
|
||||
"input_reach, output_reach, scenario " +
|
||||
"FROM hydraulic_structures " +
|
||||
f"WHERE scenario = {scenario.id} " +
|
||||
f"AND pamhyr_id NOT IN ({', '.join(map(str, loaded))})"
|
||||
)
|
||||
|
||||
for row in table:
|
||||
it = iter(row)
|
||||
new.append(cls._db_load_new(execute, data, row))
|
||||
|
||||
hs_id = next(it)
|
||||
name = next(it)
|
||||
enabled = (next(it) == 1)
|
||||
input_section_id = next(it)
|
||||
input_section_id = (
|
||||
-1 if input_section_id is None else input_section_id
|
||||
)
|
||||
output_section_id = next(it)
|
||||
output_section_id = (
|
||||
-1 if output_section_id is None else output_section_id
|
||||
)
|
||||
input_reach_id = next(it)
|
||||
output_reach_id = next(it)
|
||||
|
||||
hs = cls(
|
||||
id=hs_id, name=name, status=data['status']
|
||||
)
|
||||
|
||||
hs.enabled = enabled
|
||||
hs.input_reach, hs.output_reach = reduce(
|
||||
lambda acc, n: (
|
||||
n if n.pamhyr_id == input_reach_id else acc[0],
|
||||
n if n.pamhyr_id == output_reach_id else acc[1]
|
||||
),
|
||||
data["edges"],
|
||||
[None, None]
|
||||
)
|
||||
sections = []
|
||||
if hs.input_reach is not None:
|
||||
sections += hs.input_reach.reach.profiles
|
||||
if hs.output_reach is not None:
|
||||
sections += hs.output_reach.reach.profiles
|
||||
|
||||
hs.input_section, hs.output_section = reduce(
|
||||
lambda acc, s: (
|
||||
s if s.pamhyr_id == input_section_id else acc[0],
|
||||
s if s.pamhyr_id == output_section_id else acc[1]
|
||||
),
|
||||
sections,
|
||||
[None, None]
|
||||
)
|
||||
|
||||
data['hs_id'] = hs_id
|
||||
hs._data = BasicHS._db_load(execute, data)
|
||||
|
||||
new.append(hs)
|
||||
data["scenario"] = scenario.parent
|
||||
new += cls._db_load(execute, data)
|
||||
data["scenario"] = scenario
|
||||
|
||||
return new
|
||||
|
||||
@classmethod
|
||||
def _db_load_new(cls, execute, data, row):
|
||||
it = iter(row)
|
||||
status = data["status"]
|
||||
scenario = data["scenario"]
|
||||
loaded = data['loaded_pid']
|
||||
|
||||
hs_id = next(it)
|
||||
deleted = (next(it) == 1)
|
||||
name = next(it)
|
||||
enabled = (next(it) == 1)
|
||||
input_section_id = next(it)
|
||||
input_section_id = (
|
||||
-1 if input_section_id is None else input_section_id
|
||||
)
|
||||
output_section_id = next(it)
|
||||
output_section_id = (
|
||||
-1 if output_section_id is None else output_section_id
|
||||
)
|
||||
input_reach_id = next(it)
|
||||
output_reach_id = next(it)
|
||||
owner_scenario = next(it)
|
||||
|
||||
hs = cls(
|
||||
id=hs_id, name=name, status=status,
|
||||
owner_scenario=owner_scenario
|
||||
)
|
||||
if deleted:
|
||||
new.set_as_deleted()
|
||||
|
||||
hs.enabled = enabled
|
||||
hs.input_reach, hs.output_reach = reduce(
|
||||
lambda acc, n: (
|
||||
n if n.pamhyr_id == input_reach_id else acc[0],
|
||||
n if n.pamhyr_id == output_reach_id else acc[1]
|
||||
),
|
||||
data["edges"],
|
||||
[None, None]
|
||||
)
|
||||
sections = []
|
||||
if hs.input_reach is not None:
|
||||
sections += hs.input_reach.reach.profiles
|
||||
if hs.output_reach is not None:
|
||||
sections += hs.output_reach.reach.profiles
|
||||
|
||||
hs.input_section, hs.output_section = reduce(
|
||||
lambda acc, s: (
|
||||
s if s.pamhyr_id == input_section_id else acc[0],
|
||||
s if s.pamhyr_id == output_section_id else acc[1]
|
||||
),
|
||||
sections,
|
||||
[None, None]
|
||||
)
|
||||
|
||||
loaded.add(hs_id)
|
||||
|
||||
data['hs_id'] = hs_id
|
||||
hs._data = BasicHS._db_load(execute, data)
|
||||
|
||||
return hs
|
||||
|
||||
def _db_save(self, execute, data=None):
|
||||
execute(
|
||||
"DELETE FROM hydraulic_structures " +
|
||||
f"WHERE pamhyr_id = {self.pamhyr_id}"
|
||||
f"WHERE pamhyr_id = {self.pamhyr_id} " +
|
||||
f"AND scenario = {self._status.scenario_id}"
|
||||
)
|
||||
|
||||
input_reach_id = -1
|
||||
@@ -266,21 +301,27 @@ class HydraulicStructure(SQLSubModel):
|
||||
execute(
|
||||
"INSERT INTO " +
|
||||
"hydraulic_structures(" +
|
||||
" pamhyr_id, name, enabled, input_section, output_section, " +
|
||||
" input_reach, output_reach" +
|
||||
" pamhyr_id, deleted, name, enabled, " +
|
||||
" input_section, output_section, " +
|
||||
" input_reach, output_reach, " +
|
||||
" scenario" +
|
||||
") " +
|
||||
"VALUES (" +
|
||||
f"{self.pamhyr_id}, '{self._db_format(self._name)}', " +
|
||||
f"{self.pamhyr_id}, " +
|
||||
f"{self._db_format(self.is_deleted())}, " +
|
||||
f"'{self._db_format(self._name)}', " +
|
||||
f"{self._db_format(self.enabled)}, " +
|
||||
f"{input_section}, {output_section}, " +
|
||||
f"{input_reach_id}, {output_reach_id}" +
|
||||
f"{input_reach_id}, {output_reach_id}, " +
|
||||
f"{self._status.scenario_id}"
|
||||
")"
|
||||
)
|
||||
|
||||
data['hs_id'] = self.pamhyr_id
|
||||
execute(
|
||||
"DELETE FROM hydraulic_structures_basic " +
|
||||
f"WHERE hs = {self.pamhyr_id}"
|
||||
f"WHERE hs = {self.pamhyr_id} " +
|
||||
f"AND scenario = {self._status.scenario_id}"
|
||||
)
|
||||
|
||||
for basic in self._data:
|
||||
@@ -289,7 +330,16 @@ class HydraulicStructure(SQLSubModel):
|
||||
return True
|
||||
|
||||
def __len__(self):
|
||||
return len(self._data)
|
||||
return len(self.lst)
|
||||
|
||||
@property
|
||||
def lst(self):
|
||||
return list(
|
||||
filter(
|
||||
lambda bhs: not bhs.is_deleted(),
|
||||
self._data,
|
||||
)
|
||||
)
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
@@ -301,7 +351,7 @@ class HydraulicStructure(SQLSubModel):
|
||||
@name.setter
|
||||
def name(self, name):
|
||||
self._name = name
|
||||
self._status.modified()
|
||||
self.modified()
|
||||
|
||||
@property
|
||||
def input_rk(self):
|
||||
@@ -322,7 +372,7 @@ class HydraulicStructure(SQLSubModel):
|
||||
@input_section.setter
|
||||
def input_section(self, input_section):
|
||||
self._input_section = input_section
|
||||
self._status.modified()
|
||||
self.modified()
|
||||
|
||||
@property
|
||||
def output_section(self):
|
||||
@@ -331,7 +381,7 @@ class HydraulicStructure(SQLSubModel):
|
||||
@output_section.setter
|
||||
def output_section(self, output_section):
|
||||
self._output_section = output_section
|
||||
self._status.modified()
|
||||
self.modified()
|
||||
|
||||
@property
|
||||
def enabled(self):
|
||||
@@ -340,7 +390,7 @@ class HydraulicStructure(SQLSubModel):
|
||||
@enabled.setter
|
||||
def enabled(self, enabled):
|
||||
self._enabled = enabled
|
||||
self._status.modified()
|
||||
self.modified()
|
||||
|
||||
@property
|
||||
def input_reach(self):
|
||||
@@ -349,7 +399,7 @@ class HydraulicStructure(SQLSubModel):
|
||||
@input_reach.setter
|
||||
def input_reach(self, input_reach):
|
||||
self._input_reach = input_reach
|
||||
self._status.modified()
|
||||
self.modified()
|
||||
|
||||
@property
|
||||
def output_reach(self):
|
||||
@@ -358,26 +408,31 @@ class HydraulicStructure(SQLSubModel):
|
||||
@output_reach.setter
|
||||
def output_reach(self, output_reach):
|
||||
self._output_reach = output_reach
|
||||
self._status.modified()
|
||||
self.modified()
|
||||
|
||||
@property
|
||||
def basic_structures(self):
|
||||
return self._data.copy()
|
||||
return self.lst.copy()
|
||||
|
||||
def basic_structure(self, index: int):
|
||||
return self._data[index]
|
||||
return self.lst[index]
|
||||
|
||||
def add(self, index: int):
|
||||
value = NotDefined(status=self._status)
|
||||
self._data.insert(index, value)
|
||||
self._status.modified()
|
||||
|
||||
self.modified()
|
||||
return value
|
||||
|
||||
def insert(self, index: int, value: BasicHS):
|
||||
self._data.insert(index, value)
|
||||
self._status.modified()
|
||||
if value in self._data:
|
||||
value.set_as_not_deleted()
|
||||
else:
|
||||
self._data.insert(index, value)
|
||||
|
||||
def delete_i(self, indexes):
|
||||
self.modified()
|
||||
|
||||
def hard_delete_i(self, indexes):
|
||||
self._data = list(
|
||||
map(
|
||||
lambda e: e[1],
|
||||
@@ -387,20 +442,30 @@ class HydraulicStructure(SQLSubModel):
|
||||
)
|
||||
)
|
||||
)
|
||||
self._status.modified()
|
||||
self.modified()
|
||||
|
||||
def delete(self, els):
|
||||
self._data = list(
|
||||
filter(
|
||||
lambda e: e not in els,
|
||||
self._data
|
||||
def delete_i(self, indexes):
|
||||
list(
|
||||
map(
|
||||
lambda e: e[1].set_as_deleted(),
|
||||
filter(
|
||||
lambda e: e[0] in indexes,
|
||||
enumerate(self.lst)
|
||||
)
|
||||
)
|
||||
)
|
||||
self._status.modified()
|
||||
self.modified()
|
||||
|
||||
def delete(self, els):
|
||||
for el in els:
|
||||
el.set_as_deleted()
|
||||
|
||||
self.modified()
|
||||
|
||||
def sort(self, _reverse=False, key=None):
|
||||
if key is None:
|
||||
self._data.sort(reverse=_reverse)
|
||||
else:
|
||||
self._data.sort(reverse=_reverse, key=key)
|
||||
self._status.modified()
|
||||
|
||||
self.modified()
|
||||
Reference in new issue
Block a user