Skip to content

Commit

Permalink
add saving terms to /config if there is no such in the /config directory
Browse files Browse the repository at this point in the history
  • Loading branch information
FuHsinyu committed Aug 1, 2024
1 parent 38f7510 commit 0a9f535
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
11 changes: 10 additions & 1 deletion admin/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,16 @@ def get_publication_terms():
# Fallback to API if the file does not exist or an error occurred
try:
response = api.call('vault_get_publication_terms', {})
return response["data"]
publication_terms_html = response["data"]

# Save the data to a local file if it was fetched from the API
try:
with open(publication_terms_path, 'w') as file:
file.write(html.escape(publication_terms_html)) # Optionally escape HTML to preserve integrity
except Exception as e:
flash(f"Failed to save publication terms to file: {str(e)}", "error")

return publication_terms_html
except Exception as e:
flash(f"Failed to load publication terms from API: {str(e)}", "error")

Expand Down
26 changes: 26 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
__copyright__ = 'Copyright (c) 2021-2023, Utrecht University'
__license__ = 'GPLv3, see LICENSE'


import json
from os import path
from typing import Any, Dict, Optional
Expand Down Expand Up @@ -87,6 +88,10 @@ def load_admin_setting() -> Dict[str, Any]:
# Load theme templates
set_theme_loader(app)

import api



# Setup values for the navigation bar used in
# general/templates/general/base.html
app.config['modules'] = []
Expand Down Expand Up @@ -135,6 +140,26 @@ def load_admin_setting() -> Dict[str, Any]:
monitor_thread = Monitor(app.config)
monitor_thread.start()

def retrive_publication_terms_from_irods():
publication_terms_path = path.join(app.config['YODA_CONFIG_PATH'], 'publication_terms.html')

# Check if the publication terms file already exists
if not path.exists(publication_terms_path):
# If the file does not exist, fetch the terms from iRODS
try:
response = api.call('vault_get_publication_terms', {})
publication_terms_html = response['data']

# Save the terms to portal config directory
with open(publication_terms_path, 'w') as file:
file.write(publication_terms_html)
print("Publication terms loaded from iRODS and saved locally.")
except Exception as e:
print(f"Failed to load publication terms from iRODS: {str(e)}")
else:
print("Publication terms file already exists.")


# Register blueprints
with app.app_context():
app.register_blueprint(general_bp)
Expand All @@ -155,6 +180,7 @@ def load_admin_setting() -> Dict[str, Any]:
app.register_blueprint(intake_bp, url_prefix='/intake')
if app.config.get('DATAREQUEST_ENABLED'):
app.register_blueprint(datarequest_bp, url_prefix='/datarequest/')
#retrive_publication_terms_from_irods()

# CSRF protection.
csrf = CSRFProtect(app)
Expand Down

0 comments on commit 0a9f535

Please sign in to comment.