diff --git a/proxstar/__init__.py b/proxstar/__init__.py index 65baffd..69d407d 100644 --- a/proxstar/__init__.py +++ b/proxstar/__init__.py @@ -511,6 +511,20 @@ def template_disk(template_id): return get_template_disk(db, template_id) +@app.route('/template//edit', methods=['POST']) +@auth.oidc_auth +def template_edit(template_id): + if 'rtp' in session['userinfo']['groups']: + name = request.form['name'] + username = request.form['username'] + password = request.form['password'] + disk = request.form['disk'] + set_template_info(db, template_id, name, username, password, disk) + return '', 200 + else: + return '', 403 + + @app.route("/logout") @auth.oidc_logout def logout(): diff --git a/proxstar/db.py b/proxstar/db.py index eeb9fd5..582639d 100644 --- a/proxstar/db.py +++ b/proxstar/db.py @@ -201,3 +201,14 @@ def delete_allowed_user(db, user): Allowed_Users.id == user).one() db.delete(allowed_user) db.commit() + + +def set_template_info(db, template_id, name, username, password, disk): + if db.query(exists().where(Template.id == template_id,)).scalar(): + template = db.query(Template).filter(Template.id == template_id,).one() + template.name = name + template.username = username + if password: + template.password = password + template.disk = disk + db.commit() diff --git a/proxstar/static/js/script.js b/proxstar/static/js/script.js index ec68690..b029bd5 100644 --- a/proxstar/static/js/script.js +++ b/proxstar/static/js/script.js @@ -903,3 +903,87 @@ $(".resize-disk").click(function(){ } }); }); + +$(".edit-template").click(function(){ + const template_id = $(this).data('template_id'); + const template_name = $(this).data('template_name'); + const template_username = $(this).data('template_username'); + const template_disk = $(this).data('template_disk'); + var options = document.createElement('div'); + name_text = document.createElement('p'); + name_text.innerHTML = 'Name'; + options.append(name_text); + var name = document.createElement('input'); + name.defaultValue = template_name; + options.append(name); + username_text = document.createElement('p'); + username_text.innerHTML = 'Username'; + options.append(username_text); + var username = document.createElement('input'); + username.defaultValue = template_username; + options.append(username); + password_text = document.createElement('p'); + password_text.innerHTML = 'Password'; + options.append(password_text); + var password = document.createElement('input'); + password.type = 'password'; + options.append(password); + disk_text = document.createElement('p'); + disk_text.innerHTML = 'Disk Size (GB)'; + options.append(disk_text); + var disk = document.createElement('input'); + disk.type = 'number'; + disk.defaultValue = template_disk; + options.append(disk); + swal({ + title: `Template ${template_id}:`, + content: options, + buttons: { + cancel: { + text: "Cancel", + visible: true, + closeModal: true, + className: "", + }, + select: { + text: "Submit", + closeModal: false, + className: "swal-button", + } + }, + }) + .then((willChange) => { + if (willChange) { + var data = new FormData(); + data.append('name', $(name).val()); + data.append('username', $(username).val()); + data.append('password', $(password).val()); + data.append('disk', $(disk).val()); + fetch(`/template/${template_id}/edit`, { + credentials: 'same-origin', + method: 'post', + body: data + }).then((response) => { + return swal(`Template info changed!`, { + icon: "success", + buttons: { + ok: { + text: "OK", + closeModal: true, + className: "", + } + } + }); + }).then(() => { + location.reload(); + }); + } + }).catch(err => { + if (err) { + swal("Uh oh...", `Unable to change the template info. Please try again later.`, "error"); + } else { + swal.stopLoading(); + swal.close(); + } + }); +}); diff --git a/proxstar/templates/base.html b/proxstar/templates/base.html index d8d67c1..d4e3f09 100644 --- a/proxstar/templates/base.html +++ b/proxstar/templates/base.html @@ -60,13 +60,6 @@