Skip to content

Commit

Permalink
Clarify template management to guard against accidental overwriting o…
Browse files Browse the repository at this point in the history
…f changes.
  • Loading branch information
jon-ide committed Jun 25, 2024
1 parent d5ae6ac commit 1073267
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
20 changes: 20 additions & 0 deletions webapp/home/utils/template_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,24 @@ def init_user_lookup():
user_lookup[user[0]] = sites


def check_overwrite_user_data(template_pathname):
# Check if a file with the given name already exists in the user's data directory and is different from the template
user_base = user_data.get_user_folder_name(current_user_directory_only=True)
filename = os.path.basename(template_pathname)
user_path = os.path.join(user_base, filename)

if os.path.exists(user_path):
with open(template_pathname, 'r') as f:
template_content = f.read()
with open(user_path, 'r') as f:
user_content = f.read()
if template_content == user_content:
return False
return True

return False


def copy_template_to_user_data(template_pathname):
# Copy the template to the user's data directory
user_base = user_data.get_user_folder_name(current_user_directory_only=True)
Expand All @@ -66,6 +84,8 @@ def copy_document_to_template_folder(filename, template_folder, save_to_name):
to_path = os.path.join(template_base, template_folder, save_to_name + '.json')

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


def is_authorized_to_manage_templates(site):
Expand Down
29 changes: 28 additions & 1 deletion webapp/home/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
init_template_management,
template_folders_for_user,
templates_for_user,
check_overwrite_user_data,
copy_template_to_user_data,
copy_document_to_template_folder,
delete_template_file
Expand Down Expand Up @@ -1460,6 +1461,11 @@ def save_as_template():
copy_document_to_template_folder(current_document, folder, save_to_name)

flash(f'Template saved as "{save_to_name}" in template folder {folder}.', 'success')

flash(f'Document "{save_to_name}" has been removed from your user folder. '
f"To edit it in the future, go to 'Manage Templates...' and use the 'Open Template' button, "
f"which copies it to your user folder and opens it for editing.")

return redirect(url_for(PAGE_TITLE, filename=current_document))
else:
if len(template_folders) > 1 and not form.folder.data:
Expand Down Expand Up @@ -1552,7 +1558,7 @@ def manage_templates():
@login_required
@non_saving_hidden_buttons_decorator
def open_template():
"""Handle the Open Template... page in EML Documents menu."""
"""Handle the Open Template... page in Manage Templates... menu."""

form = OpenTemplateForm()
form.filename.choices = list_templates(True)
Expand All @@ -1567,9 +1573,30 @@ def open_template():
if form.validate_on_submit():
filename = form.filename.data

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

# Copy the template to the user's data directory.
filename = copy_template_to_user_data(filename)

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

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

Expand Down
4 changes: 4 additions & 0 deletions webapp/static/help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1390,13 +1390,17 @@ Click <b>Open a Template</b> to view or edit an existing metadata template. You
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>.

With <b>Save As Template</b>, you can save the template under its current name or under a new name.

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.
--------------------
save_as_template
Save As Template

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.

If no EML document is currently open, <b>Save As Template</b> will be disabled.

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.
--------------------
delete_template
Delete Template
Expand Down

0 comments on commit 1073267

Please sign in to comment.