Skip to content

Commit 24d14a8

Browse files
committed
Merge branch 'development'
2 parents b600c2e + 1073267 commit 24d14a8

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

webapp/home/utils/template_management.py

+20
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,24 @@ def init_user_lookup():
4747
user_lookup[user[0]] = sites
4848

4949

50+
def check_overwrite_user_data(template_pathname):
51+
# Check if a file with the given name already exists in the user's data directory and is different from the template
52+
user_base = user_data.get_user_folder_name(current_user_directory_only=True)
53+
filename = os.path.basename(template_pathname)
54+
user_path = os.path.join(user_base, filename)
55+
56+
if os.path.exists(user_path):
57+
with open(template_pathname, 'r') as f:
58+
template_content = f.read()
59+
with open(user_path, 'r') as f:
60+
user_content = f.read()
61+
if template_content == user_content:
62+
return False
63+
return True
64+
65+
return False
66+
67+
5068
def copy_template_to_user_data(template_pathname):
5169
# Copy the template to the user's data directory
5270
user_base = user_data.get_user_folder_name(current_user_directory_only=True)
@@ -66,6 +84,8 @@ def copy_document_to_template_folder(filename, template_folder, save_to_name):
6684
to_path = os.path.join(template_base, template_folder, save_to_name + '.json')
6785

6886
copyfile(from_path, to_path)
87+
# We remove the version in the user's data directory to avoid confusion between the versions
88+
os.remove(from_path)
6989

7090

7191
def is_authorized_to_manage_templates(site):

webapp/home/views.py

+28-1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
init_template_management,
105105
template_folders_for_user,
106106
templates_for_user,
107+
check_overwrite_user_data,
107108
copy_template_to_user_data,
108109
copy_document_to_template_folder,
109110
delete_template_file
@@ -1460,6 +1461,11 @@ def save_as_template():
14601461
copy_document_to_template_folder(current_document, folder, save_to_name)
14611462

14621463
flash(f'Template saved as "{save_to_name}" in template folder {folder}.', 'success')
1464+
1465+
flash(f'Document "{save_to_name}" has been removed from your user folder. '
1466+
f"To edit it in the future, go to 'Manage Templates...' and use the 'Open Template' button, "
1467+
f"which copies it to your user folder and opens it for editing.")
1468+
14631469
return redirect(url_for(PAGE_TITLE, filename=current_document))
14641470
else:
14651471
if len(template_folders) > 1 and not form.folder.data:
@@ -1552,7 +1558,7 @@ def manage_templates():
15521558
@login_required
15531559
@non_saving_hidden_buttons_decorator
15541560
def open_template():
1555-
"""Handle the Open Template... page in EML Documents menu."""
1561+
"""Handle the Open Template... page in Manage Templates... menu."""
15561562

15571563
form = OpenTemplateForm()
15581564
form.filename.choices = list_templates(True)
@@ -1567,9 +1573,30 @@ def open_template():
15671573
if form.validate_on_submit():
15681574
filename = form.filename.data
15691575

1576+
# Check that we are not going to overwrite a modified document in the user's data directory.
1577+
if check_overwrite_user_data(filename):
1578+
doc_name = os.path.basename(filename)
1579+
doc_name = os.path.splitext(doc_name)[0]
1580+
current_filename = current_user.get_filename()
1581+
if doc_name == current_filename:
1582+
flash(f'A different version of document "{doc_name}" is currently open. '
1583+
f"Opening the template will overwrite it. Please delete it before opening this template.\n\n"
1584+
f"If desired, you can use 'Save As...' in the 'EML Documents' menu to save the currently open version "
1585+
f"under a different name before deleting it.\n\n"
1586+
f"If the currently open version is intended to replace the template, just use 'Save As Template' to save it.")
1587+
else:
1588+
flash(f'A different version of document "{doc_name}" already exists in your user folder. '
1589+
f"Opening the template will overwrite it. Please delete it before opening this template.\n\n"
1590+
f"If the version in your user folder is the one you want to keep, use the ordinary 'Open' "
1591+
f"command to open it. You can then save it as a template if you wish.")
1592+
return redirect(url_for(PAGE_MANAGE_TEMPLATES))
1593+
15701594
# Copy the template to the user's data directory.
15711595
filename = copy_template_to_user_data(filename)
15721596

1597+
flash(f'Template "{filename}" has been copied to your user data directory and opened for editing.\n\n'
1598+
f"When you are done editing, use 'Save As Template' to save your changes to the template.", 'success')
1599+
15731600
# Open the document. Note that open_document takes care of handling locks.
15741601
return open_document(filename)
15751602

webapp/static/help.txt

+4
Original file line numberDiff line numberDiff line change
@@ -1390,13 +1390,17 @@ Click <b>Open a Template</b> to view or edit an existing metadata template. You
13901390
To create a new template from scratch, just use <b>New...</b> in the <b>EML Documents</b> menu to create it as you would any other EML document, then save it as a template using <b>Save As Template</b>.
13911391

13921392
With <b>Save As Template</b>, you can save the template under its current name or under a new name.
1393+
1394+
Templates are stored in their own storage area, distinct from your user data directory, so that they will be visible to all users, not just you and your collaborators. When you open a template via <b>Open Template</b>, it is copied into your user data directory and opened for editing. If you then modify the template, you need to save it via <b>Save As Template</b>, which moves it back to the template directory.
13931395
--------------------
13941396
save_as_template
13951397
Save As Template
13961398

13971399
Click <b>Save As Template</b> to save the currently open EML document as a metadata template. You can save the template under the EML document's current name or under a new name.
13981400

13991401
If no EML document is currently open, <b>Save As Template</b> will be disabled.
1402+
1403+
When you save a document as a template, the document is moved out of your user data directory and into the template directory to make it visible to all users, not just to you and your collaborators. It will therefore no longer appear in the list of EML documents that you see when you use the <b>Open</b> command. To view or edit a saved template, use the <b>Open Template</b> command. This copies the template into your user data directory and opens it for editing. Then to save changes to the template, use <b>Save As Template</b>, at which point it is moved back to the template directory.
14001404
--------------------
14011405
delete_template
14021406
Delete Template

0 commit comments

Comments
 (0)