Skip to content

Commit 08841ff

Browse files
committed
Merge branch 'development'
2 parents 2af4425 + c016e2a commit 08841ff

File tree

5 files changed

+32
-21
lines changed

5 files changed

+32
-21
lines changed

webapp/home/utils/create_nodes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from webapp.utils import null_string
2727
from webapp.home.home_utils import RELEASE_NUMBER, log_error, log_info
2828
from webapp.home.metapype_client import VariableType
29-
from webapp.home.utils.node_utils import new_child_node, remove_child, add_node, Optionality
29+
from webapp.home.utils.node_utils import new_child_node, add_node, Optionality
3030

3131
import webapp.home.utils.load_and_save as load_and_save
3232

webapp/home/utils/node_utils.py

+20
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,23 @@ def remove_child(child_node:Node):
8181
parent_node = child_node.parent
8282
if parent_node:
8383
parent_node.remove_child(child_node)
84+
85+
86+
def replace_node(new_node:Node, old_node_id:str):
87+
"""
88+
If the old_node_id is not '1', replace the old node with the new node.
89+
This is used on pages that support Save Changes, where we want the modified node to replace the original node,
90+
using the same node ID so the Back button works as expected.
91+
"""
92+
if old_node_id and old_node_id != '1':
93+
old_node = Node.get_node_instance(old_node_id)
94+
if old_node:
95+
parent_node = old_node.parent
96+
if parent_node:
97+
# Make the replacement in the node tree
98+
parent_node.replace_child(old_node, new_node, delete_old=False)
99+
# Make the replacement in the node instance dictionary
100+
new_node_id = new_node.id
101+
new_node._id = old_node.id
102+
Node.set_node_instance(new_node)
103+
Node.delete_node_instance(id=new_node_id)

webapp/home/views.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1764,7 +1764,7 @@ def open_template():
17641764
filename = copy_template_to_user_data(filename)
17651765

17661766
flash(f'Template "{filename}" has been copied to your user data directory and opened for editing.\n\n'
1767-
f"When you are done editing, use 'Save As Template' to save your changes to the template.", 'success')
1767+
f"When you are done editing, use 'Save As Template' on the 'Manage Templates' page to save your changes to the template.", 'success')
17681768

17691769
# Open the document. Note that open_document takes care of handling locks.
17701770
return open_document(filename)

webapp/views/data_tables/dt.py

+6-11
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from markupsafe import Markup
2424

2525
import webapp.home.utils.create_nodes
26-
import webapp.home.utils.node_utils
26+
# import webapp.home.utils.node_utils
2727
import webapp.views.data_tables.load_data
2828
from webapp.config import Config
2929

@@ -51,7 +51,7 @@
5151
)
5252

5353
from webapp.home.metapype_client import VariableType
54-
from webapp.home.utils.node_utils import remove_child, new_child_node, add_child
54+
from webapp.home.utils.node_utils import remove_child, new_child_node, add_child, replace_node
5555
from webapp.home.utils.hidden_buttons import is_hidden_button, handle_hidden_buttons, check_val_for_hidden_buttons
5656
from webapp.home.utils.node_store import dump_node_store
5757
from webapp.home.utils.load_and_save import load_eml, save_both_formats, handle_custom_unit_additional_metadata
@@ -269,9 +269,7 @@ def compose_atts(att_list: list = []):
269269

270270
# If we're modifying an existing data table, we need to replace the old data table node with the
271271
# new one.
272-
dataset_parent_node = old_dt_node.parent
273-
dataset_parent_node.replace_child(old_dt_node, dt_node)
274-
dt_node_id = dt_node.id
272+
replace_node(dt_node, old_dt_node.id)
275273
else:
276274
msg = f"No node found in the node store with node id {dt_node_id}"
277275
dump_node_store(eml_node, 'data_table')
@@ -1300,8 +1298,7 @@ def attribute_dateTime(filename=None, dt_node_id=None, node_id=None):
13001298
if node_id and len(node_id) != 1:
13011299
old_att_node = Node.get_node_instance(att_node_id)
13021300
if old_att_node:
1303-
att_parent_node = old_att_node.parent
1304-
att_parent_node.replace_child(old_att_node, att_node)
1301+
replace_node(att_node, old_att_node.id)
13051302
else:
13061303
msg = f"No node found in the node store with node id {node_id}"
13071304
dump_node_store(eml_node, 'attribute_dateTime')
@@ -1547,8 +1544,7 @@ def attribute_numerical(filename=None, dt_node_id=None, node_id=None, mscale=Non
15471544
if node_id and len(node_id) != 1:
15481545
old_att_node = Node.get_node_instance(att_node_id)
15491546
if old_att_node:
1550-
att_parent_node = old_att_node.parent
1551-
att_parent_node.replace_child(old_att_node, att_node)
1547+
replace_node(att_node, old_att_node.id)
15521548
else:
15531549
msg = f"No node found in the node store with node id {node_id}"
15541550
dump_node_store(eml_node, 'attribute_numerical')
@@ -1858,8 +1854,7 @@ def code_definition_from_attribute(att_node: Node = None):
18581854
if node_id and len(node_id) != 1:
18591855
old_att_node = Node.get_node_instance(att_node_id)
18601856
if old_att_node:
1861-
att_parent_node = old_att_node.parent
1862-
att_parent_node.replace_child(old_att_node, att_node)
1857+
replace_node(att_node, old_att_node.id)
18631858
else:
18641859
msg = f"No node found in the node store with node id {node_id}"
18651860
dump_node_store(eml_node, 'attribute_categorical')

webapp/views/responsible_parties/rp.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from webapp.home.utils.load_and_save import load_eml, save_both_formats
2020
from webapp.home.utils.lists import list_responsible_parties
2121
from webapp.home.utils.create_nodes import create_responsible_party
22-
from webapp.home.utils.node_utils import add_child, new_child_node
22+
from webapp.home.utils.node_utils import add_child, new_child_node, replace_node
2323

2424
from metapype.eml import names
2525
from metapype.model.node import Node
@@ -266,9 +266,6 @@ def responsible_party(filename=None, rp_node_id=None,
266266
child nodes, and it is easier to create a new one from scratch than to try to modify an existing one.
267267
The responsibleParty node has a number of child nodes that have cardinality 0..infinity, which makes
268268
it a lot more complicated to find and modify the appropriate children to modify.
269-
270-
A complication, though, is that the node ID will change, which we need to allow for if Save Changes is
271-
clicked, bringing us back to the same page. I.e., we want the generated URL to use the new node ID.
272269
"""
273270
rp_node = Node(node_name, parent=parent_node)
274271

@@ -299,16 +296,15 @@ def responsible_party(filename=None, rp_node_id=None,
299296
if rp_node_id and len(rp_node_id) != 1:
300297
old_rp_node = Node.get_node_instance(rp_node_id)
301298
if old_rp_node:
302-
old_rp_parent_node = old_rp_node.parent
303-
old_rp_parent_node.replace_child(old_rp_node, rp_node)
299+
replace_node(rp_node, old_rp_node.id)
304300
else:
305301
msg = f"No node found in the node store with node id {rp_node_id}"
306302
raise Exception(msg)
307303
else:
308304
add_child(parent_node, rp_node)
309305

310-
# We want generated URLs, below, to point to the new responsibleParty node
311-
rp_node_id = rp_node.id
306+
# We want generated URLs, below, to point to the new responsibleParty node, but we have
307+
# given the new node the same node ID as the old node. So, we just leave the node ID as is.
312308

313309
save_both_formats(filename=filename, eml_node=eml_node)
314310
# flash(f"Changes to the '{node_name}' element have been saved.")

0 commit comments

Comments
 (0)