web app updates to support save/open session xml files
This commit is contained in:
parent
979dc05a9f
commit
053c9789bc
2 changed files with 71 additions and 5 deletions
|
@ -1,4 +1,5 @@
|
|||
import os
|
||||
import tempfile
|
||||
from functools import wraps
|
||||
from threading import Lock
|
||||
|
||||
|
@ -6,6 +7,7 @@ from flask import Flask
|
|||
from flask import jsonify
|
||||
from flask import render_template
|
||||
from flask import request
|
||||
from flask import send_file
|
||||
from flask_socketio import SocketIO
|
||||
from flask_socketio import emit
|
||||
|
||||
|
@ -92,6 +94,38 @@ def get_ips():
|
|||
)
|
||||
|
||||
|
||||
@app.route("/sessions/<int:session_id>/xml")
|
||||
def save_xml(session_id):
|
||||
session = coreemu.sessions.get(session_id)
|
||||
if not session:
|
||||
return jsonify(error="session does not exist"), 404
|
||||
|
||||
_, temp_path = tempfile.mkstemp()
|
||||
session.save_xml(temp_path, "1.0")
|
||||
return send_file(
|
||||
temp_path,
|
||||
as_attachment=True,
|
||||
attachment_filename="session-%s.xml" % session_id
|
||||
)
|
||||
|
||||
|
||||
@app.route("/sessions/xml", methods=["POST"])
|
||||
def open_xml():
|
||||
session = coreemu.create_session()
|
||||
logger.info("open xml: %s", request.files)
|
||||
_, temp_path = tempfile.mkstemp()
|
||||
session_file = request.files['session']
|
||||
session_file.save(temp_path)
|
||||
|
||||
try:
|
||||
session.open_xml(temp_path, start=True)
|
||||
return jsonify(id=session.session_id)
|
||||
except:
|
||||
logger.exception("error opening session file")
|
||||
coreemu.delete_session(session.session_id)
|
||||
return jsonify(error="error opening session file"), 404
|
||||
|
||||
|
||||
@app.route("/sessions")
|
||||
def get_sessions():
|
||||
sessions = []
|
||||
|
|
|
@ -28,8 +28,10 @@
|
|||
File
|
||||
</a>
|
||||
<div class="dropdown-menu" aria-labelledby="nb-file">
|
||||
<a class="dropdown-item" href="#">Open File</a>
|
||||
<a class="dropdown-item" href="#">Save File</a>
|
||||
<label class="dropdown-item btn btn-default">
|
||||
Open Session <input id="session-open" type="file" hidden>
|
||||
</label>
|
||||
<a id="session-save-button" class="dropdown-item" href="#">Save Session</a>
|
||||
</div>
|
||||
</li>
|
||||
|
||||
|
@ -118,11 +120,14 @@
|
|||
<div id="node-context" class="list-group context d-none">
|
||||
<button type="button" class="list-group-item list-group-item-action" href="#" data-option="edit">Edit Node</button>
|
||||
<button id="node-linkrf-button" type="button" class="list-group-item list-group-item-action d-none" href="#"
|
||||
data-option="linkrf">Link All Routers</button>
|
||||
data-option="linkrf">Link All Routers
|
||||
</button>
|
||||
<button type="button" class="list-group-item list-group-item-action" href="#"
|
||||
data-option="services">Services</button>
|
||||
data-option="services">Services
|
||||
</button>
|
||||
<button id="node-delete-button" type="button" class="list-group-item list-group-item-action" href="#"
|
||||
data-option="delete">Delete</button>
|
||||
data-option="delete">Delete
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div id="edge-context" class="list-group context d-none">
|
||||
|
@ -148,6 +153,8 @@
|
|||
const $runButton = $('#run-button');
|
||||
const $nodeSelect = $('#node-select');
|
||||
const $newSessionButton = $('#new-session-button');
|
||||
const $sessionSaveButton = $('#session-save-button');
|
||||
const $sessionOpen = $('#session-open');
|
||||
|
||||
function formToJson($form) {
|
||||
const formData = {};
|
||||
|
@ -203,6 +210,8 @@
|
|||
$sessionId.text(`Session: ${session.id}`);
|
||||
const isStartEnabled = session.state !== SessionStates.runtime;
|
||||
setRunButton(isStartEnabled);
|
||||
$sessionSaveButton.attr('href', `/sessions/${session.id}/xml`);
|
||||
$sessionSaveButton.attr('download', true);
|
||||
}
|
||||
|
||||
// handle network clicks
|
||||
|
@ -299,6 +308,29 @@
|
|||
$nodeSelect.attr('src', icon);
|
||||
});
|
||||
|
||||
$sessionOpen.change(function (event) {
|
||||
const file = event.target.files[0];
|
||||
console.log('session open: ', file);
|
||||
const uri = `/sessions/xml`;
|
||||
const request = new XMLHttpRequest();
|
||||
const formData = new FormData();
|
||||
request.open('POST', uri, true);
|
||||
request.onreadystatechange = function () {
|
||||
if (request.readyState == 4 && request.status == 200) {
|
||||
const response = JSON.parse(request.responseText);
|
||||
coreNetwork.joinSession(response.id)
|
||||
.then(function (session) {
|
||||
joinSession(session);
|
||||
})
|
||||
.catch(function (err) {
|
||||
console.log('join open session error: ', err);
|
||||
});
|
||||
}
|
||||
};
|
||||
formData.append('session', file);
|
||||
request.send(formData);
|
||||
});
|
||||
|
||||
console.log('connecting to ws');
|
||||
const ws = io.connect();
|
||||
ws.on('connection', function () {
|
||||
|
|
Loading…
Reference in a new issue