system-pri/backend/app/commands/startapp.py

70 lines
1.6 KiB
Python
Raw Normal View History

2022-05-15 21:20:05 +02:00
import os
import re
from click import argument, command
2022-05-15 21:20:05 +02:00
from click.exceptions import ClickException
from flask import current_app
2022-05-15 21:20:05 +02:00
from flask.cli import with_appcontext
@command("startapp")
@argument("name")
@with_appcontext
def startapp(name: str) -> None:
"""Create the application structure"""
if not re.match("^[a-zA-Z].*$", name):
raise ClickException("The name argument must be type of string!")
2022-05-15 21:20:05 +02:00
app_dir = current_app.config["SRC_DIR"] / name
if os.path.exists(app_dir):
raise ClickException("Directory {name} has already exists!")
os.mkdir(app_dir)
schema_content = """\'\'\'
Below you write a schema of model
Example:
from marshmallow import Schema
2022-05-15 21:20:05 +02:00
class ExampleSchema(ma.SQLAlchemyAutoSchema):
class Meta:
model = MODEL_NAME
\'\'\'
from marshmallow import Schema
2022-05-15 21:20:05 +02:00
"""
model_content = """\'\'\'
Below you write a model.
Example:
class Example(db.Model):
__tablename__ = "examples"
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
title = db.Column(db.String(255), unique=True)
\'\'\'
2022-05-15 21:20:05 +02:00
from ..dependencies import db
"""
route_content = f"""from flask import Blueprint, make_response, jsonify, Response
bp = Blueprint("{name}", __name__, url_prefix="/{name}")
@bp.route("/", methods=["GET"])
def index() -> Response:
return make_response(jsonify({{"hello": "{name}"}}), 200)
"""
filenames = ["__init__.py", "schemas.py", "models.py", "routes.py"]
contents = ["", schema_content, model_content, route_content]
for filename, content in zip(filenames, contents):
with open(app_dir / filename, "w") as f:
f.write(content)