2022-05-15 21:20:05 +02:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
from click import argument, command
|
2022-05-15 21:20:05 +02:00
|
|
|
from click.exceptions import ClickException
|
2023-01-14 17:38:03 +01:00
|
|
|
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):
|
2023-01-14 17:38:03 +01:00
|
|
|
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:
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
from marshmallow import Schema
|
|
|
|
|
2022-05-15 21:20:05 +02:00
|
|
|
class ExampleSchema(ma.SQLAlchemyAutoSchema):
|
|
|
|
class Meta:
|
|
|
|
model = MODEL_NAME
|
|
|
|
\'\'\'
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
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)
|
2023-01-14 17:38:03 +01:00
|
|
|
\'\'\'
|
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)
|