From d63dfe1f219a3a2743b5689da050c087c7c8e548 Mon Sep 17 00:00:00 2001 From: jasquat Date: Tue, 31 May 2022 14:05:22 -0400 Subject: [PATCH] uploading bpmn files is now working w/ burnettk --- src/spiff_workflow_webapp/__init__.py | 2 + .../routes/admin_blueprint/admin_blueprint.py | 51 +++-- .../admin_blueprint/templates/layout.html | 9 + .../templates/process_model_edit.html | 196 +++++++++--------- .../templates/process_model_show.html | 1 + .../routes/process_api_blueprint.py | 4 +- 6 files changed, 142 insertions(+), 121 deletions(-) diff --git a/src/spiff_workflow_webapp/__init__.py b/src/spiff_workflow_webapp/__init__.py index 577cecb6..35b1d8d9 100644 --- a/src/spiff_workflow_webapp/__init__.py +++ b/src/spiff_workflow_webapp/__init__.py @@ -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) diff --git a/src/spiff_workflow_webapp/routes/admin_blueprint/admin_blueprint.py b/src/spiff_workflow_webapp/routes/admin_blueprint/admin_blueprint.py index 3684f3ad..76e2413d 100644 --- a/src/spiff_workflow_webapp/routes/admin_blueprint/admin_blueprint.py +++ b/src/spiff_workflow_webapp/routes/admin_blueprint/admin_blueprint.py @@ -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//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//edit", methods=["GET"]) -def process_model_edit(process_model_id): + +@admin_blueprint.route("/process_models//edit/", 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//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 diff --git a/src/spiff_workflow_webapp/routes/admin_blueprint/templates/layout.html b/src/spiff_workflow_webapp/routes/admin_blueprint/templates/layout.html index 61a9ca50..2dbcea26 100644 --- a/src/spiff_workflow_webapp/routes/admin_blueprint/templates/layout.html +++ b/src/spiff_workflow_webapp/routes/admin_blueprint/templates/layout.html @@ -8,6 +8,15 @@

{{ self.title() }}

+ {% with messages = get_flashed_messages(with_categories=true) %} + {% if messages %} +
    + {% for category, message in messages %} +
  • {{ message }}
  • + {% endfor %} +
+ {% endif %} + {% endwith %} {% block content %}{% endblock %} diff --git a/src/spiff_workflow_webapp/routes/admin_blueprint/templates/process_model_edit.html b/src/spiff_workflow_webapp/routes/admin_blueprint/templates/process_model_edit.html index 52d9c8dd..5c99dad8 100644 --- a/src/spiff_workflow_webapp/routes/admin_blueprint/templates/process_model_edit.html +++ b/src/spiff_workflow_webapp/routes/admin_blueprint/templates/process_model_edit.html @@ -1,27 +1,27 @@ {% extends "layout.html" %} {% block title %}Process Model Edit: {{ process_model.id }}{% endblock %} {% block head %} - + - - - - - + + + + + - - + + - - + + - - - {% endblock %} - {% block content %} -
{{ result }}
- - -
+ +{% endblock %} +{% block content %} +
{{ result }}
+ + +
- - - - {% endblock %} + // wire save button + $('#save-button').click(exportDiagram); + +{% endblock %} diff --git a/src/spiff_workflow_webapp/routes/admin_blueprint/templates/process_model_show.html b/src/spiff_workflow_webapp/routes/admin_blueprint/templates/process_model_show.html index 316cf50e..eb62037a 100644 --- a/src/spiff_workflow_webapp/routes/admin_blueprint/templates/process_model_show.html +++ b/src/spiff_workflow_webapp/routes/admin_blueprint/templates/process_model_show.html @@ -49,6 +49,7 @@ html, body, #canvas {
{{ result }}
+ {% if files %}

BPMN Files

diff --git a/src/spiff_workflow_webapp/routes/process_api_blueprint.py b/src/spiff_workflow_webapp/routes/process_api_blueprint.py index 70d49d1e..933de8fa 100644 --- a/src/spiff_workflow_webapp/routes/process_api_blueprint.py +++ b/src/spiff_workflow_webapp/routes/process_api_blueprint.py @@ -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)