diff --git a/.gitignore b/.gitignore
index e5e0451e..c5ba5117 100644
--- a/.gitignore
+++ b/.gitignore
@@ -96,3 +96,6 @@ ENV/
# Rope project settings
.ropeproject
+
+#vscode settings
+.vscode/
\ No newline at end of file
diff --git a/TEKDB/TEKDB/static/admin/css/itk_base.css b/TEKDB/TEKDB/static/admin/css/itk_base.css
index 90162d8f..1821db17 100644
--- a/TEKDB/TEKDB/static/admin/css/itk_base.css
+++ b/TEKDB/TEKDB/static/admin/css/itk_base.css
@@ -68,19 +68,8 @@ span.select2-results {
background-color: var(--body-bg);
}
-/* EXPORT/IMPORT MODULES */
-
-div#export-module a,
-button#import-button {
- margin: 1rem;
-}
-
button.btn-info#export-info,
button.btn-info#import-info {
margin: -0.5rem 0 1rem 1rem;
float: right;
}
-
-a.btn-primary#export-button {
- color: var(--bs-white);
-}
diff --git a/TEKDB/TEKDB/static/admin/js/admin_index.js b/TEKDB/TEKDB/static/admin/js/admin_index.js
index 6b2ee42f..89205d1a 100644
--- a/TEKDB/TEKDB/static/admin/js/admin_index.js
+++ b/TEKDB/TEKDB/static/admin/js/admin_index.js
@@ -1,108 +1,210 @@
-$(function() {
- $('button#import-button').click(function(e) {
- e.preventDefault();
- if (
- window.confirm(
- "This process will remove all data and files from your current " +
- "database and replace it with data from the provided zip file. " +
- "\n\n" +
- "This process CANNOT be undone. \n\n" +
- "It is recommended that you use the button above to export " +
- "your current database status so that you may restore to your " +
- "present state, AND that you COORDINATE WITH IT professionals " +
- "prior to attempting this. \n\n" +
- "You will need to log in again using administrator credentials " +
- "as defined by the new data: your current account will cease to " +
- "exist.\n\n" +
- "Are you sure you understand and are prepared to take this risk?"
- )
- ) {
- form = $('#import-database-form');
- $.ajax({
- url: '/import_database/',
- data: new FormData(form[0]),
- type: "POST",
- processData: false,
- contentType: false,
- success: function(data, status) {
- if (data.hasOwnProperty('status_code') && data.hasOwnProperty('status_message')) {
- if (data.status_code == 200) {
- window.alert(data.status_message + "\n\nYou may now be logged out.");
- window.location.reload();
- } else {
- window.alert("Error Code: " + data.status_code + "\n\n" + data.status_message);
- }
- } else {
- window.alert("Unexpected error occurred: " + status);
- }
- },
- error: function(xhr, desc, err) {
- window.alert("Unexpected error occurred: " + err);
- }
- })
- }
+const importText = `
+ This process will remove all data and files from your current
+ database and replace it with data from the provided zip file.
+
+ This process CANNOT be undone.
+ It is recommended that you use the 'Export to .zip' button above to export
+ your current database status so that you may restore to your
+ present state, AND that you COORDINATE WITH IT professionals
+ prior to attempting this.
+ You will need to log in again using administrator credentials
+ as defined by the new data: your current account will cease to
+ exist.
+ Are you sure you understand and are prepared to take this risk?
+`;
+
+const importInfoText = `The Import Database Tool is designed to support restoring your Traditional Knowledge Database back to a prior state. This is best used in conjunction with the 'Export Database' tool above.
+ To use the Import Database tool is simple: select a properly formatted zip file to upload and then click 'Import'. All of your data will be reverted back to the state it was when that zipfile was created.
+ This is VERY DANGEROUS! For this to work, all of the data currently
+ in your database, including your users, your records, and your page
+ contents will be removed, and then replaced. Also, any files
+ associated with your media records (images, audio, video, PDFs,
+ etc...) will be overwritten by any files of the same name included
+ in the zipped up backup file that you import.
+ If you wish to use this, it is recommended that you work with IT
+ professionals prior to undertaking this task. While IT cannot create
+ one of these import files for a prior state, they can help you
+ securely preserve any new zipped export files, and should be able
+ to create a system backup of your database to restore in the event
+ of complications while using this tool.
+ Note that a zipped backup file is easily created for your current
+ state with the 'Export to .zip' button above, but cannot be
+ created by hand. If you have not created any of these files, you
+ should not use this tool.`;
+
+const exportInfoText = `The Export Database Tool is designed to support saving the current
+ state of your Traditional Knowledge Database. This is best
+ used in conjunction with the 'Import Database' tool below.
+
+ To use the Export Database tool is simple: just press 'Export to
+ .zip' button and all of your data and media files will be collected
+ into a single zipfile and downloaded to your current computer.
+
+ This is VERY DANGEROUS! While keeping regular backups
+ of your database is crucial to preventing data loss from future
+ incidents, the exported file will contain ALL of your data in an
+ unencrypted format. These exported files
+ must be kept in a secure place and only shared via secure channels:
+ NOT BY EMAIL.
+
+ Please work with IT professionals to design
+ a safe and secure practice for regular backups, which may or may
+ not involve this tool.`;
+
+$(function () {
+ const unexpectedError = (statusCode, statusMessage = "") => {
+ return `
Unexpected error occurred: ${statusCode}
${statusMessage}
`; + }; + + const showNextStepsSection = () => { + $("#modalNextSteps").removeClass("hidden"); + }; + + const showModalFooter = () => { + $(".modal-footer").show(); + }; + + const hideModalFooter = () => { + $(".modal-footer").hide(); + }; + + const resetModal = () => { + $("#continueImport").html("Close"); + $("#closeModalButton").prop("disabled", false); + $("#continueImport").click(function () { + $("#exportImportModal").modal("hide"); }); + }; - $('button#export-info').click(function(e) { - window.alert( - "Export Database Tool:\n\n" + - - "The Export Database Tool is designed to support saving the current " + - "state of your Traditional Knowledge Database. This is best " + - "used in conjunction with the 'Import Database' tool below. \n\n" + - - "To use the Export Database tool is simple: just press 'Export to " + - ".zip' button and all of your data and media files will be collected " + - "into a single zipfile and downloaded to your current computer." + - "\n\n" + - - "This is VERY DANGEROUS! While keeping regular backups " + - "of your database is crucial to preventing data loss from future " + - "incidents, the exported file will contain ALL of your data in an " + - "unencrypted format. These exported files " + - "must be kept in a secure place and only shared via secure channels: " + - "NOT BY EMAIL.\n\n" + - - "Please work with IT professionals to design " + - "a safe and secure practice for regular backups, which may or may " + - "not involve this tool." - ); - }) - - $('button#import-info').click(function(e) { - window.alert( - "Import Database Tool:\n\n" + - - "The Import Database Tool is designed to support restoring your " + - "Traditional Knowledge Database back to a prior state. This is best " + - "used in conjunction with the 'Export Database' tool above. \n\n" + - - "To use the Import Database tool is simple: select a properly formatted " + - "zip file to upload and then click 'Import'. All of your data will " + - "be reverted back to the state it was when that zipfile was created." + - "\n\n" + - - "This is VERY DANGEROUS! For this to work, all of the data currently " + - "in your database, including your users, your records, and your page " + - "contents will be removed, and then replaced. Also, any files " + - "associated with your media records (images, audio, video, PDFs, " + - "etc...) will be overwritten by any files of the same name included " + - "in the zipped up backup file that you import.\n\n" + - - "If you wish to use this, it is recommended that you work with IT " + - "professionals prior to undertaking this task. While IT cannot create " + - "one of these import files for a prior state, they can help you " + - "securely preserve any new zipped export files, and should be able " + - "to create a system backup of your database to restore in the event " + - "of complications while using this tool. \n\n" + - - "Note that a zipped backup file is easily created for your current " + - "state with the 'Export to .zip' button above, but cannot be " + - "created by hand. If you have not created any of these files, you " + - "should not use this tool." + const resetExportButton = (iframe, clearInterval) => { + $("#export-button").prop("disabled", false); + $("#export-button").html("Export to .zip"); + iframe.remove(); + clearInterval(); + }; + + $("button#import-button").click(function (e) { + e.preventDefault(); + showModalFooter(); + + $("#modalTitle").text("Import Database"); + $("#modalBody").html(importText); + + $("#continueImport").click(function () { + // prevent closing the modal after verifying import + $("#exportImportModal").modal({ + backdrop: "static", + keyboard: false, + }); + + // add spinner to the button + // change button text to "Importing..." + $("#continueImport").html( + ` Importing...` ); - }) + $("#continueImport").prop("disabled", true); + $("#closeModalButton").prop("disabled", true); + + const form = $("#import-database-form"); + $.ajax({ + url: "/import_database/", + data: new FormData(form[0]), + type: "POST", + processData: false, + contentType: false, + success: function (data, status) { + if ( + data.hasOwnProperty("status_code") && + data.hasOwnProperty("status_message") + ) { + showNextStepsSection(); + $("#continueImport").prop("disabled", false); + + if (data.status_code == 200) { + $("#modalNextSteps").html( + `Import successful. You may now be logged out.
` + ); + $("#continueImport").html("Log out"); + $("#continueImport").click(function () { + location.reload(true); + }); + } else { + $("#modalNextSteps").html( + `Import failed with error code ${data.status_code}.
${data.status_message}
` + ); + resetModal(); + } + } else { + $("#modalNextSteps").html(unexpectedError(status)); + resetModal(); + } + }, + error: function (xhr) { + $("#modalNextSteps").html( + unexpectedError(xhr.status, xhr.statusText) + ); + resetModal(); + }, + }); + }); + }); + + $("button#import-info").click(function (e) { + hideModalFooter(); + $("#modalTitle").text("Import Database Tool"); + $("#modalBody").html(importInfoText); + }); + $("button#export-info").click(function (e) { + hideModalFooter(); + $("#modalTitle").text("Export Database Tool"); + $("#modalBody").html(exportInfoText); + }); + + $("button#export-button").click(function (e) { + e.preventDefault(); + + // remove error message if present + $("#exportStatus").addClass("hidden"); + + // disable button and show spinner + $("#export-button").prop("disabled", true); + $("#export-button").html( + ` Exporting...` + ); + + // set cookie to pending + document.cookie = "export_status=pending"; + + // create hidden iframe to trigger download + const iframe = $("") + .hide() + .attr("src", "/export_database/") + .appendTo("body"); + + // poll cookie for export_status + const checkStatus = setInterval(function () { + const cookies = document.cookie.split(";").map((c) => c.trim()); + const exportStatus = cookies.filter((c) => + c.startsWith("export_status=") + ); + if (exportStatus && exportStatus.includes("export_status=done")) { + resetExportButton(iframe, clearInterval(checkStatus)); + } else if (exportStatus && exportStatus.includes("export_status=error")) { + $("#exportStatus").removeClass("hidden"); + resetExportButton(iframe, clearInterval(checkStatus)); + } + }, 1000); + }); + // Clear modal content on close; reset to default state + $("#exportImportModal").on("hidden.bs.modal", function () { + $("#modalTitle").text(""); + $("#modalBody").html(""); + $("#modalNextSteps").html(""); + $("#modalNextSteps").addClass("hidden"); + $("#continueImport").html("Continue"); + $("#continueImport").off("click"); + }); }); diff --git a/TEKDB/TEKDB/templates/admin/index.html b/TEKDB/TEKDB/templates/admin/index.html index 1ac0b221..0e17b69d 100644 --- a/TEKDB/TEKDB/templates/admin/index.html +++ b/TEKDB/TEKDB/templates/admin/index.html @@ -25,20 +25,21 @@ {% if user.is_superuser %}