Skip to content

Commit

Permalink
add: file formats fnctionality
Browse files Browse the repository at this point in the history
  • Loading branch information
leonidastri committed Oct 3, 2024
1 parent 53e894b commit 4654fd8
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 1 deletion.
49 changes: 49 additions & 0 deletions admin/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
__copyright__ = 'Copyright (c) 2024, Utrecht University'
__license__ = 'GPLv3, see LICENSE'

import os
import html
import json
from functools import wraps
Expand All @@ -16,6 +17,7 @@
Flask,
g,
jsonify,
make_response,
redirect,
render_template,
request,
Expand All @@ -29,6 +31,8 @@

import api
from util import get_theme_directories, length_check
from werkzeug.utils import secure_filename
from irods.message import iRODSMessage

# Blueprint configuration
admin_bp = Blueprint("admin_bp", __name__,
Expand Down Expand Up @@ -271,3 +275,48 @@ def get_publication_terms() -> Optional[str]:
flash("Failed to load publication terms from API", "error")

return "Error: failed to read publication terms"


@admin_bp.route('/upload_file_formats', methods=['POST'])
@admin_required
def upload_file_formats():
filename = secure_filename(request.files['file'].filename)
if not filename.endswith('.json'):
flash("Uploaded file for file formats is not a JSON file.", "danger")
return redirect(url_for("admin_bp.index"))

file_path = os.path.join("/" + g.irods.zone, 'yoda', 'file_formats', filename)

# Get the chunk data.
data = request.files['file']
encode_unicode_content = iRODSMessage.encode_unicode(data.stream.read())

try:
with g.irods.data_objects.open(file_path, 'w') as obj_desc:
obj_desc.write(encode_unicode_content)
obj_desc.close()
flash("File Formats uploaded successfully.", "success")
except Exception:
flash("Failed to upload File Formats", "error")

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


@admin_bp.route('/delete_file_formats', methods=['POST'])
@admin_required
def delete_file_formats():
filename = request.form.get('filename')

if not filename:
flash("No File Formats specified for deletion.", "danger")
return redirect(url_for("admin_bp.index"))

file_path = os.path.join("/" + g.irods.zone, 'yoda', 'file_formats', filename + '.json')
print(file_path)
try:
g.irods.data_objects.unlink(file_path, force=True)
flash(f"File '{filename}' deleted successfully.", "success")
except Exception as e:
flash("Failed to delete File Formats.", "danger")

return redirect(url_for("admin_bp.index"))
30 changes: 30 additions & 0 deletions admin/static/admin/js/file_formats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
$(function () {
Yoda.call('vault_preservable_formats_lists').then((data) => {
preservableFormatsLists = data
$('#file-formats-list').html("<option value='' disabled selected>Select a file format list</option>")
for (const list in data) {
if (Object.prototype.hasOwnProperty.call(data, list)) {
$('#file-formats-list').append(new Option(data[list].name, list))
}
}
})

document.getElementById('upload-button').addEventListener('click', function() {
document.getElementById('file').click()
})

document.getElementById('file').addEventListener('change', function() {
if (this.files.length > 0) {
this.form.submit()
}
})

$('#file-formats-list').on('change', function() {
let selectedValue = $(this).val();
if (selectedValue) {
$('#delete-format-button').prop('disabled', false)
} else {
$('#delete-format-button').prop('disabled', true)
}
})
})
27 changes: 26 additions & 1 deletion admin/templates/admin/admin.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

{% block scripts %}
<script src="{{ url_for('static', filename='lib/purify-3.1.6/js/purify.min.js') }}"></script>
<script src="{{ url_for('static', filename='lib/jquery-3.7.1/jquery-3.7.1.min.js') }}"></script>
<script src="{{ url_for('admin_bp.static', filename='js/create_preview.js') }}"></script>
<script src="{{ url_for('admin_bp.static', filename='js/file_formats.js') }}"></script>
{% endblock scripts %}

{% block content %}
Expand Down Expand Up @@ -63,7 +65,7 @@ <h2 class="card-title">Portal Theme</h2>
</div>

<!-- Publication Terms Section -->
<div>
<div class="mb-4">
<h2 class="card-title">Publication Terms</h2>
<div class="d-flex justify-content-start align-items-end">
<form action="{{ url_for('admin_bp.set_publication_terms') }}" method="post"
Expand All @@ -80,6 +82,29 @@ <h2 class="card-title">Publication Terms</h2>
</form>
</div>
</div>

<!-- File Formats Section -->
<div class="mb-4">
<h2 class="card-title">File Formats</h2>
<div class="mb-3">
<label>List of File Formats:</label>
<form action="{{ url_for('admin_bp.delete_file_formats') }}" method="post" class="flex-fill me-2 needs-validation" novalidate>
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
<select class="form-control" id="file-formats-list" name="filename" required>
<option value="" disabled selected>Select a file format to delete</option>
</select>
<button type="submit" class="btn btn-danger" id="delete-format-button" disabled>Delete File Formats</button>
</form>
</div>
<div class="mb-3">
<label>Upload new File Formats:</label>
<form action="{{ url_for('admin_bp.upload_file_formats') }}" method="post" enctype="multipart/form-data" class="flex-fill me-2 needs-validation" novalidate>
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
<input type="file" name="file" id="file" class="d-none" required>
<button type="button" class="btn btn-primary" id="upload-button">Upload File Formats</button>
</form>
</div>
</div>
</div>
</section>
</main>
Expand Down

0 comments on commit 4654fd8

Please sign in to comment.