uploading bpmn files is now working w/ burnettk

This commit is contained in:
jasquat 2022-05-31 14:05:22 -04:00
parent 3e21e6e13b
commit d63dfe1f21
6 changed files with 142 additions and 121 deletions

View File

@ -24,6 +24,8 @@ def create_app() -> flask.app.Flask:
connexion_app = connexion.FlaskApp(__name__, server_args={"instance_path": os.environ.get("FLASK_INSTANCE_PATH")})
app = connexion_app.app
app.config['CONNEXION_APP'] = connexion_app
app.secret_key = 'super secret key'
app.config['SESSION_TYPE'] = 'filesystem'
setup_config(app)
db.init_app(app)

View File

@ -1,9 +1,12 @@
"""APIs for dealing with process groups, process models, and process instances."""
import os
from typing import Any
from flask import Blueprint, render_template, redirect, url_for
from flask import Blueprint, render_template, redirect, url_for, flash
from flask_bpmn.models.db import db
from flask import request
from flask import request, current_app
from werkzeug.utils import secure_filename
from spiff_workflow_webapp.models.user import UserModel
from spiff_workflow_webapp.services.process_instance_processor import ProcessInstanceProcessor
@ -13,6 +16,8 @@ from spiff_workflow_webapp.services.process_model_service import ProcessModelSer
admin_blueprint = Blueprint("admin", __name__, template_folder='templates', static_folder='static')
ALLOWED_BPMN_EXTENSIONS = {'bpmn', 'dmn'}
@admin_blueprint.route("/process-groups", methods=["GET"])
def process_groups_list():
@ -50,34 +55,31 @@ def process_model_show_file(process_model_id, file_name):
@admin_blueprint.route("/process-models/<process_model_id>/upload-file", methods=["POST"])
def process_model_upload_file(process_model_id):
"""Process_model_upload_file."""
process_model = ProcessModelService().get_spec(process_model_id)
bpmn_xml = SpecFileService.get_data(process_model, file_name)
files = SpecFileService.get_files(process_model, extension_filter="bpmn")
process_model_service = ProcessModelService()
process_model = process_model_service.get_spec(process_model_id)
if 'file' not in request.files:
flash('No file part')
return redirect(url_for('admin.process_model_show', process_model_id=process_model.id))
file = request.files['file']
flash('No file part', "error")
request_file = request.files['file']
# If the user does not select a file, the browser submits an
# empty file without a filename.
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect(url_for('download_file', name=filename))
if request_file.filename == '':
flash('No selected file', "error")
if request_file and _allowed_file(request_file.filename):
SpecFileService.add_file(process_model, request_file.filename, request_file.stream.read())
process_model_service.update_spec(process_model)
return render_template('process_model_show.html', process_model=process_model, bpmn_xml=bpmn_xml, files=files, current_file_name=file_name)
return redirect(url_for('admin.process_model_show', process_model_id=process_model.id))
@admin_blueprint.route("/process_models/<process_model_id>/edit", methods=["GET"])
def process_model_edit(process_model_id):
@admin_blueprint.route("/process_models/<process_model_id>/edit/<file_name>", methods=["GET"])
def process_model_edit(process_model_id, file_name):
"""Edit_bpmn."""
process_model = ProcessModelService().get_spec(process_model_id)
bpmn_xml = SpecFileService.get_data(process_model, process_model.primary_file_name)
bpmn_xml = SpecFileService.get_data(process_model, file_name)
return render_template('process_model_edit.html', bpmn_xml=bpmn_xml.decode("utf-8"),
process_model=process_model)
process_model=process_model, file_name=file_name)
@admin_blueprint.route("/process-models/<process_model_id>/save", methods=["POST"])
@ -100,9 +102,11 @@ def process_model_run(process_model_id):
result = processor.get_data()
process_model = ProcessModelService().get_spec(process_model_id)
files = SpecFileService.get_files(process_model, extension_filter="bpmn")
current_file_name = process_model.primary_file_name
bpmn_xml = SpecFileService.get_data(process_model, process_model.primary_file_name)
return render_template('process_model_show.html', process_model=process_model, bpmn_xml=bpmn_xml, result=result)
return render_template('process_model_show.html', process_model=process_model, bpmn_xml=bpmn_xml, result=result, files=files, current_file_name=current_file_name)
def _find_or_create_user(username: str = "test_user1") -> Any:
@ -113,3 +117,8 @@ def _find_or_create_user(username: str = "test_user1") -> Any:
db.session.add(user)
db.session.commit()
return user
def _allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_BPMN_EXTENSIONS

View File

@ -8,6 +8,15 @@
</head>
<body>
<h1>{{ self.title() }}</h1>
{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
<ul class=flashes>
{% for category, message in messages %}
<li class="{{ category }}">{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
{% block content %}{% endblock %}
</body>
</html>

View File

@ -1,27 +1,27 @@
{% extends "layout.html" %}
{% block title %}Process Model Edit: {{ process_model.id }}{% endblock %}
{% block head %}
<meta charset="UTF-8" />
<meta charset="UTF-8" />
<!-- example styles -->
<!-- required modeler styles -->
<link rel="stylesheet" href="https://unpkg.com/bpmn-js@9.1.0/dist/assets/bpmn-js.css">
<link rel="stylesheet" href="https://unpkg.com/bpmn-js@9.1.0/dist/assets/diagram-js.css">
<link rel="stylesheet" href="https://unpkg.com/bpmn-js@9.1.0/dist/assets/bpmn-font/css/bpmn.css">
<!-- example styles -->
<!-- required modeler styles -->
<link rel="stylesheet" href="https://unpkg.com/bpmn-js@9.1.0/dist/assets/bpmn-js.css">
<link rel="stylesheet" href="https://unpkg.com/bpmn-js@9.1.0/dist/assets/diagram-js.css">
<link rel="stylesheet" href="https://unpkg.com/bpmn-js@9.1.0/dist/assets/bpmn-font/css/bpmn.css">
<!-- modeler distro -->
<script src="https://unpkg.com/bpmn-js@9.1.0/dist/bpmn-modeler.development.js"></script>
<!-- modeler distro -->
<script src="https://unpkg.com/bpmn-js@9.1.0/dist/bpmn-modeler.development.js"></script>
<!-- needed for this example only -->
<script src="https://unpkg.com/jquery@3.3.1/dist/jquery.js"></script>
<!-- needed for this example only -->
<script src="https://unpkg.com/jquery@3.3.1/dist/jquery.js"></script>
<!-- example styles -->
<style>
html, body, #canvas {
height: 100%;
padding: 0;
margin: 0;
}
<!-- example styles -->
<style>
html, body, #canvas {
height: 100%;
padding: 0;
margin: 0;
}
.diagram-note {
background-color: rgba(66, 180, 21, 0.7);
@ -44,101 +44,101 @@
bottom: 20px;
left: 20px;
}
</style>
{% endblock %}
{% block content %}
<div id="result">{{ result }}</div>
<button type="button" onclick="window.location.href='{{ url_for( 'admin.process_model_show', process_model_id=process_model.id ) }}';">Back</button>
<button type="button" onclick="exportDiagram()" >Save</button>
<div id="canvas"></div>
</style>
{% endblock %}
{% block content %}
<div id="result">{{ result }}</div>
<button type="button" onclick="window.location.href='{{ url_for( 'admin.process_model_show_file', process_model_id=process_model.id, file_name=file_name ) }}';">Back</button>
<button type="button" onclick="exportDiagram()" >Save</button>
<div id="canvas"></div>
<meta id="bpmn_xml" data-name="{{bpmn_xml}}">
<meta id="process_model_id" data-name="{{process_model.id}}">
<script>
<meta id="bpmn_xml" data-name="{{bpmn_xml}}">
<meta id="process_model_id" data-name="{{process_model.id}}">
<script>
// modeler instance
var bpmnModeler = new BpmnJS({
container: '#canvas',
keyboard: {
bindTo: window
}
});
// modeler instance
var bpmnModeler = new BpmnJS({
container: '#canvas',
keyboard: {
bindTo: window
}
});
/**
* Save diagram contents and print them to the console.
*/
async function exportDiagram() {
/**
* Save diagram contents and print them to the console.
*/
async function exportDiagram() {
try {
try {
var data = await bpmnModeler.saveXML({ format: true });
var process_model_id = $('#process_model_id').data().name;
console.log("The data is", data)
//POST request with body equal on data in JSON format
fetch('/admin/process-models/' + process_model_id + '/save' , {
method: 'POST',
headers: {
'Content-Type': 'text/xml',
},
body: data.xml
})
.then((response) => response.json())
//Then with the data from the response in JSON...
.then((data) => {
console.log('Success:', data);
})
//Then with the error genereted...
.catch((error) => {
console.error('Error:', error);
});
var data = await bpmnModeler.saveXML({ format: true });
var process_model_id = $('#process_model_id').data().name;
console.log("The data is", data)
//POST request with body equal on data in JSON format
fetch('/admin/process-models/' + process_model_id + '/save' , {
method: 'POST',
headers: {
'Content-Type': 'text/xml',
},
body: data.xml
})
.then((response) => response.json())
//Then with the data from the response in JSON...
.then((data) => {
console.log('Success:', data);
})
//Then with the error genereted...
.catch((error) => {
console.error('Error:', error);
});
alert('Diagram exported. Check the developer tools!');
} catch (err) {
console.error('could not save BPMN 2.0 diagram', err);
}
}
alert('Diagram exported. Check the developer tools!');
} catch (err) {
console.error('could not save BPMN 2.0 diagram', err);
}
}
/**
* Open diagram in our modeler instance.
*
* @param {String} bpmnXML diagram to display
*/
async function openDiagram(bpmnXML) {
/**
* Open diagram in our modeler instance.
*
* @param {String} bpmnXML diagram to display
*/
async function openDiagram(bpmnXML) {
// import diagram
try {
// import diagram
try {
await bpmnModeler.importXML(bpmnXML);
await bpmnModeler.importXML(bpmnXML);
// access modeler components
var canvas = bpmnModeler.get('canvas');
var overlays = bpmnModeler.get('overlays');
// access modeler components
var canvas = bpmnModeler.get('canvas');
var overlays = bpmnModeler.get('overlays');
// zoom to fit full viewport
canvas.zoom('fit-viewport');
// zoom to fit full viewport
canvas.zoom('fit-viewport');
// attach an overlay to a node
overlays.add('SCAN_OK', 'note', {
position: {
bottom: 0,
right: 0
},
html: '<div class="diagram-note">Mixed up the labels?</div>'
});
// attach an overlay to a node
overlays.add('SCAN_OK', 'note', {
position: {
bottom: 0,
right: 0
},
html: '<div class="diagram-note">Mixed up the labels?</div>'
});
// add marker
canvas.addMarker('SCAN_OK', 'needs-discussion');
} catch (err) {
// add marker
canvas.addMarker('SCAN_OK', 'needs-discussion');
} catch (err) {
console.error('could not import BPMN 2.0 diagram', err);
}
}
console.error('could not import BPMN 2.0 diagram', err);
}
}
var bpmn_xml = $('#bpmn_xml').data();
openDiagram(bpmn_xml.name)
var bpmn_xml = $('#bpmn_xml').data();
openDiagram(bpmn_xml.name)
// wire save button
$('#save-button').click(exportDiagram);
</script>
{% endblock %}
// wire save button
$('#save-button').click(exportDiagram);
</script>
{% endblock %}

View File

@ -49,6 +49,7 @@ html, body, #canvas {
<div id="result">{{ result }}</div>
<button type="button" onclick="window.location.href='{{ url_for( 'admin.process_group_show', process_group_id=process_model.process_group_id ) }}';">Back</button>
<button type="button" onclick="window.location.href='{{ url_for( 'admin.process_model_run' , process_model_id=process_model.id ) }}';">Run</button>
<button type="button" onclick="window.location.href='{{ url_for( 'admin.process_model_edit' , process_model_id=process_model.id, file_name=current_file_name ) }}';">Edit</button>
{% if files %}
<h3>BPMN Files</h3>

View File

@ -49,8 +49,8 @@ def add_file(spec_id):
"""Add_file."""
workflow_spec_service = ProcessModelService()
workflow_spec = workflow_spec_service.get_spec(spec_id)
file = connexion.request.files['file']
file = SpecFileService.add_file(workflow_spec, file.filename, file.stream.read())
request_file = connexion.request.files['file']
file = SpecFileService.add_file(workflow_spec, request_file.filename, request_file.stream.read())
if not workflow_spec.primary_process_id and file.type == FileType.bpmn.value:
SpecFileService.set_primary_bpmn(workflow_spec, file.name)
workflow_spec_service.update_spec(workflow_spec)