diff --git a/blueprints/upload_photo/upload_photo.html b/blueprints/upload_photo/upload_photo.html index 9352ac2..890bc5c 100644 --- a/blueprints/upload_photo/upload_photo.html +++ b/blueprints/upload_photo/upload_photo.html @@ -6,10 +6,26 @@ +

Upload a Static Photo

+
+ +
+
+ + +
+
+ + +
+ +
\ No newline at end of file diff --git a/static/script.js b/static/script.js new file mode 100644 index 0000000..06a7d5e --- /dev/null +++ b/static/script.js @@ -0,0 +1,45 @@ +function uploadFiles(submitButtonID, fileInputID, progressBarID, targetEndpoint, otherData) { + const submitButton = document.getElementById(submitButtonID) + submitButton.disabled = true + + const files = document.getElementById(fileInputID).files + + if (!files[0]) { + alert('Please select at least one file') + submitButton.disabled = false + return + } + + const formData = new FormData() + + for(var i = 0; i < files.length; i++) { + formData.append("files[]", files[i]) + } + + for(const key in otherData) { + formData.append(key, otherData[key]) + } + + var xhr = new XMLHttpRequest() + xhr.open("POST", targetEndpoint) + xhr.onload = function() { + if (xhr.status === 200) { + window.location.href = "/" + } else { + alert("Something went wrong during the upload, please try again later") + submitButton.disabled = false + } + } + xhr.upload.onprogress = function(event) { + if(event.lengthComputable) { + const percentComplete = (event.loaded / event.total) * 100 + document.getElementById(progressBarID).value = percentComplete + } + } + xhr.onerror = function() { + alert("An error occurred, please try again later") + submitButton.disabled = false + } + + xhr.send(formData) +} \ No newline at end of file diff --git a/utilities/database/tools.py b/utilities/database/tools.py deleted file mode 100644 index 5234edc..0000000 --- a/utilities/database/tools.py +++ /dev/null @@ -1,15 +0,0 @@ -from flask import g -from sqlite import SQLite -import json - -def get_db(): - db = getattr(g, "_database", None) - - if db is None: - with open('../../config.json') as config_file: - config_json = json.load(config_file.read()) - - if str(config_json["database"]["type"]).casefold() == "SQLite".casefold(): - db = g._database = SQLite(config_json["database"]["name"]) - - return db \ No newline at end of file diff --git a/utilities/tools.py b/utilities/tools.py new file mode 100644 index 0000000..d33675f --- /dev/null +++ b/utilities/tools.py @@ -0,0 +1,21 @@ +from flask import g +from database.sqlite import SQLite +import json + +def get_config(): + config = getattr(g, "_config", None) + + if config is None: + with open("../config.json") as config_file: + config = g._config = json.loads(config_file.read()) + + return config + +def get_db(): + db = getattr(g, "_database", None) + + if db is None: + if str(get_config()["database"]["type"]).casefold() == "SQLite".casefold(): + db = g._database = SQLite(get_config()["database"]["name"]) + + return db \ No newline at end of file