Skip to content

Commit 2666634

Browse files
committed
YDA-6118: publication terms not saving correctly in admin page
1 parent ae659a3 commit 2666634

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

admin/admin.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,20 @@ def set_publication_terms() -> Response:
220220
# Save new terms to local file
221221
try:
222222
publication_terms_path = path.join(app.config['YODA_CONFIG_PATH'], 'publication_terms.html')
223-
with open(publication_terms_path, 'w') as file:
223+
224+
# Read current content first, in case writing fails
225+
with open(publication_terms_path, 'r', encoding='utf-8') as file:
226+
old_terms = file.read()
227+
with open(publication_terms_path, 'w', encoding='utf-8') as file:
224228
file.write(sanitized_terms)
229+
225230
flash("Publication terms updated successfully.", "success")
226231
return redirect(url_for("admin_bp.index"))
227232
except Exception:
228-
flash("Failed to update publication terms", "error")
233+
with open(publication_terms_path, 'w', encoding='utf-8') as file:
234+
file.write(old_terms)
235+
236+
flash("Failed to update publication terms.", "error")
229237

230238
return redirect(url_for("admin_bp.index"))
231239

@@ -250,7 +258,7 @@ def get_publication_terms() -> Optional[str]:
250258
# Attempt to read from local file
251259
if path.exists(publication_terms_path):
252260
try:
253-
with open(publication_terms_path, 'r') as file:
261+
with open(publication_terms_path, 'r', encoding='utf-8') as file:
254262
publication_terms_html = file.read()
255263
return html.unescape(publication_terms_html) # Convert escaped terms to html
256264
except Exception:
@@ -263,16 +271,16 @@ def get_publication_terms() -> Optional[str]:
263271

264272
# Save the data to a local file if it was fetched from the API
265273
try:
266-
with open(publication_terms_path, 'w') as file:
274+
with open(publication_terms_path, 'w', encoding='utf-8') as file:
267275
file.write(html.escape(publication_terms_html))
268276
except Exception:
269-
flash("Failed to save publication terms to file", "error")
277+
flash("Failed to save publication terms to file.", "error")
270278

271279
return publication_terms_html
272280
except Exception:
273-
flash("Failed to load publication terms from API", "error")
281+
flash("Failed to load publication terms from API.", "error")
274282

275-
return "Error: failed to read publication terms"
283+
return "Error: failed to read publication terms."
276284

277285

278286
@admin_bp.route('/upload_file_formats', methods=['POST'])

0 commit comments

Comments
 (0)