Pamhyr2: Create new generic methode '_data_traversal' for scenario duplicattion.

scenarios
Pierre-Antoine 2025-09-12 10:52:41 +02:00
parent 591cd8950c
commit 8173eb8de9
5 changed files with 57 additions and 24 deletions

View File

@ -502,3 +502,18 @@ class Study(SQLModel):
self.status.set_as_read_only()
else:
self.status.set_as_editable()
def duplicate_current_scenario(self):
source = self.status.scenario_id
new = self.scenarios.new(
self.status.scenario.parent
)
self.river._data_traversal(
predicate=lambda obj: obj._owner_scenario == source,
execute=lambda obj: obj.set_owner_scenario(),
data={}
)
self.status.set_as_editable()
return new

View File

@ -202,7 +202,7 @@ class SQLSubModel(PamhyrID):
self._owner_scenario = self._status.scenario_id
self._status.modified()
def set_owner_scenario_if(self, predicate):
def set_owner_scenario(self):
"""Set study status the object owner_scenario to current
scenario if predicate(scenario_id) is true
@ -215,9 +215,8 @@ class SQLSubModel(PamhyrID):
if self._status is None:
return
if predicate(self._owner_scenario):
self._owner_scenario = self._status.scenario_id
self._status.modified()
self._owner_scenario = self._status.scenario_id
self._status.modified()
def is_deleted(self):
"""This object is deleted?

View File

@ -82,20 +82,27 @@ class PamhyrModelDict(SQLSubModel):
self.set(key, new)
return new
def rec_set_owner_scenario_if(self, predicate):
"""Set study status the object owner_scenario to current
scenario if predicate(scenario_id) is true
def _data_traversal(self,
predicate=lambda obj, data: True,
modifier=lambda obj, data: None,
data={}):
"""Traversal data and execute modifier fonction if predicate
true
Args:
predicate: Function predicate on owner_scenario id
predicate: Function predicate, take current obj and data as input
modifier: Function modifier, take current obj and data as input
Returns:
Nothing
"""
self.set_owner_scenario_if(predicate)
for k in self._lst:
self._dict[key].set_owner_scenario_if(predicate)
"""
if predicate(self, data):
modifier(self, data)
for key in self._lst:
self._dict[key]\
._data_traversal(predicate, modifier, data)
def new(self, key):
raise NotImplementedMethodeError(self, self.new)

View File

@ -85,20 +85,26 @@ class PamhyrModelList(SQLSubModel):
if self._status is not None:
self._status.modified()
def rec_set_owner_scenario_if(self, predicate):
"""Set study status the object owner_scenario to current
scenario if predicate(scenario_id) is true
def _data_traversal(self,
predicate=lambda obj, data: True,
modifier=lambda obj, data: None,
data={}):
"""Traversal data and execute modifier fonction if predicate
true
Args:
predicate: Function predicate on owner_scenario id
predicate: Function predicate, take current obj and data as input
modifier: Function modifier, take current obj and data as input
Returns:
Nothing
"""
self.set_owner_scenario_if(predicate)
if predicate(self, data):
modifier(self, data)
for el in self._lst:
el.set_owner_scenario_if(predicate)
el._data_traversal(predicate, modifier, data)
def new(self, index):
"""Create new elements and add it to list

View File

@ -90,20 +90,26 @@ class PamhyrModelList(SQLSubModel):
if self._status is not None:
self._status.modified()
def rec_set_owner_scenario_if(self, predicate):
"""Set study status the object owner_scenario to current
scenario if predicate(scenario_id) is true
def _data_traversal(self,
predicate=lambda obj, data: True,
modifier=lambda obj, data: None,
data={}):
"""Traversal data and execute modifier fonction if predicate
true
Args:
predicate: Function predicate on owner_scenario id
predicate: Function predicate, take current obj and data as input
modifier: Function modifier, take current obj and data as input
Returns:
Nothing
"""
self.set_owner_scenario_if(predicate)
if predicate(self, data):
modifier(self, data)
for el in self._lst:
el.set_owner_scenario_if(predicate)
el._data_traversal(predicate, modifier, data)
def new(self, index):
"""Create new elements and add it to list