@@ -220,12 +220,20 @@ def set_publication_terms() -> Response:
220
220
# Save new terms to local file
221
221
try :
222
222
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 :
224
228
file .write (sanitized_terms )
229
+
225
230
flash ("Publication terms updated successfully." , "success" )
226
231
return redirect (url_for ("admin_bp.index" ))
227
232
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" )
229
237
230
238
return redirect (url_for ("admin_bp.index" ))
231
239
@@ -250,7 +258,7 @@ def get_publication_terms() -> Optional[str]:
250
258
# Attempt to read from local file
251
259
if path .exists (publication_terms_path ):
252
260
try :
253
- with open (publication_terms_path , 'r' ) as file :
261
+ with open (publication_terms_path , 'r' , encoding = 'utf-8' ) as file :
254
262
publication_terms_html = file .read ()
255
263
return html .unescape (publication_terms_html ) # Convert escaped terms to html
256
264
except Exception :
@@ -263,16 +271,16 @@ def get_publication_terms() -> Optional[str]:
263
271
264
272
# Save the data to a local file if it was fetched from the API
265
273
try :
266
- with open (publication_terms_path , 'w' ) as file :
274
+ with open (publication_terms_path , 'w' , encoding = 'utf-8' ) as file :
267
275
file .write (html .escape (publication_terms_html ))
268
276
except Exception :
269
- flash ("Failed to save publication terms to file" , "error" )
277
+ flash ("Failed to save publication terms to file. " , "error" )
270
278
271
279
return publication_terms_html
272
280
except Exception :
273
- flash ("Failed to load publication terms from API" , "error" )
281
+ flash ("Failed to load publication terms from API. " , "error" )
274
282
275
- return "Error: failed to read publication terms"
283
+ return "Error: failed to read publication terms. "
276
284
277
285
278
286
@admin_bp .route ('/upload_file_formats' , methods = ['POST' ])
0 commit comments