Coverage for src/app/portal/bp_portal.py: 63%
35 statements
« prev ^ index » next coverage.py v7.6.10, created at 2025-03-27 07:10 -0400
« prev ^ index » next coverage.py v7.6.10, created at 2025-03-27 07:10 -0400
1"""Blueprint for the CueCode Dev Portal"""
3import uuid
5from flask import Blueprint, flash, redirect, render_template, request, url_for
6from flask_wtf import FlaskForm
7from werkzeug.utils import secure_filename
8from wtforms import FileField, SubmitField
10from actors import actor_config_algo_openapi_spec
11from common.models import CuecodeConfig, OpenAPISpec
12from common.models.base import db
15class OpenAPISpecUploadForm(FlaskForm):
16 """The form for receiving OpenAPI specification files"""
18 spec_file = FileField("Upload OpenAPI Specification")
19 submit = SubmitField("Upload")
22def create_blueprint():
23 """Build the CueCode developer portal blueprint.
24 For the prototype, this is a bare-bones web app used only to upload
25 the API spec. Other requirements like login may apply per instruction from
26 our professor."""
28 portal_bp = Blueprint(
29 "portal", __name__, static_folder="static", template_folder="templates"
30 )
32 @portal_bp.route("/", methods=["GET"])
33 def index():
35 # pylint: disable-next=fixme
36 # TODO: will need to add a decorator and add "context" param once
37 # auth is added. The same goes for all handlers.
39 return render_template("index.html")
41 @portal_bp.route("/upload-spec", methods=["GET", "POST"])
42 def upload_spec():
43 # Design note: spec upload diagram found at the following link.
44 # https://app.diagrams.net/?src=about#G1pe-I-vJEF1rdLu7zXhRyQhJHBGLgtCJX#%7B%22pageId%22%3A%22VpygQNfUVgPvtDY-Q96l%22%7D
45 form = OpenAPISpecUploadForm()
47 if request.method == "POST" and form.validate_on_submit():
48 spec_file = form.spec_file.data
50 if spec_file:
51 filename = secure_filename(spec_file.filename)
52 spec_content = spec_file.read().decode(
53 "utf-8"
54 ) # Read the content as text
56 # Create DB records
57 cuecode_config = CuecodeConfig(
58 cuecode_config_id=uuid.uuid4(),
59 config_is_finished=False,
60 is_live=False,
61 )
62 db.session.add(cuecode_config)
64 spec_id = uuid.uuid4()
65 openapi_spec = OpenAPISpec(
66 openapi_spec_id=spec_id,
67 spec_text=spec_content,
68 file_name=filename,
69 cuecode_config_id=cuecode_config.cuecode_config_id,
70 )
71 db.session.add(openapi_spec)
72 db.session.commit()
74 # Publish to queue
75 actor_config_algo_openapi_spec.send(str(spec_id))
77 flash("OpenAPI spec uploaded successfully!", "success")
78 return redirect(url_for("portal.upload_spec"))
80 return render_template("upload_spec.html", form=form)
82 return portal_bp