diff --git a/README.md b/README.md index fbe5655..0e83867 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ **Source Code**: View it on [Github](https://github.com/ycd/manage-fastapi/) - +**Installation**: `pip install manage-fastapi` --- @@ -44,12 +44,19 @@ -## Example folder structure for more check [documentation](https://ycd.github.io/manage-fastapi/) +## Example folder structure with two commands :open_file_folder: + +``` +manage-fastapi startproject fastproject +manage-fastapi startapp v1 ``` -newproject/ + + +``` +fastproject/ ├── __init__.py ├── main.py -├── newproject +├── core │   ├── models │   │   ├── database.py │   │   └── __init__.py @@ -79,6 +86,12 @@ newproject/ ### Latest Changes +### 0.1.4 + +* Changed project architecture +* Increased travis tests + + ### 0.1.3 * Make database optional diff --git a/docs/index.md b/docs/index.md index a612701..6069e48 100644 --- a/docs/index.md +++ b/docs/index.md @@ -40,10 +40,10 @@ ## Example folder structure 📦 ``` -newproject/ +fastproject/ ├── __init__.py ├── main.py -├── newproject +├── core │   ├── models │   │   ├── database.py │   │   └── __init__.py diff --git a/docs/managing_apps/startapp.md b/docs/managing_apps/startapp.md index d90df72..8ac14c2 100644 --- a/docs/managing_apps/startapp.md +++ b/docs/managing_apps/startapp.md @@ -17,10 +17,10 @@ Application v1 created successfully! Let's see what it created. Now we have a new folder called **v1** and another folder called **v1** under our **tests** folder. Let's see what they have. ```python -newproject/ +fastproject/ ├── __init__.py ├── main.py -├── newproject +├── core │   ├── models │   │   ├── database.py │   │   └── __init__.py @@ -41,7 +41,7 @@ newproject/ └── __init__.py ``` -In our **`myproject/myapp`** we have new **1 directory and 4 files**, let's see what they have. +In our **`fastproject/v1`** we have new **1 directory and 4 files**, let's see what they have. In our `endpoints` folder we are going create all the endpoints for this app, also `endpoints.py` comes with a basic Hello world router, diff --git a/docs/managing_projects/startproject.md b/docs/managing_projects/startproject.md index b4e2dc8..252acfb 100644 --- a/docs/managing_projects/startproject.md +++ b/docs/managing_projects/startproject.md @@ -3,21 +3,21 @@ To start a new project with Manage FastAPI, you can use this: * `manage-fastapi startproject [project-name]` - Create a new project. -This will create create **4 directories and 8 files** for you. Let's see what it includes, for instance i'm creating a new project called **newproject** +This will create create **4 directories and 8 files** for you. Let's see what it includes, for instance i'm creating a new project called **fastproject** ```shell -manage-fastapi startproject newproject +manage-fastapi startproject fastproject -Project newproject created successfully! +Project fastproject created successfully! ``` The command we ran above, created a `main.py` that will include all our external app's. A folder called **models** for our database stuff, another folder called **schemas** for our Pydantic models etc and a `settings.py` file. ```shell -newproject/ +fastproject/ ├── __init__.py ├── main.py -├── newproject +├── core │   ├── models │   │   ├── database.py │   │   └── __init__.py @@ -35,8 +35,8 @@ Our **`main.py`** gonna be our controller. It will include all the routers other from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware -from newproject.settings import settings -from newproject.models.database import database +from fastproject.settings import settings +from fastproject.models.database import database app = FastAPI(title=settings.PROJECT_NAME) @@ -68,7 +68,7 @@ from pydantic import BaseSettings, AnyHttpUrl, HttpUrl, validator class Settings(BaseSettings): - PROJECT_NAME: str = "newproject" + PROJECT_NAME: str = "fastproject" BACKEND_CORS_ORIGINS: List[AnyHttpUrl] = [ "http://localhost", @@ -113,7 +113,7 @@ In **`models/database.py`** we create all our database stuff, If you don't need ```python import sqlalchemy -from newproject.settings import settings +from fastproject.settings import settings import databases database = databases.Database(settings.DATABASE_URL) diff --git a/docs/release-notes.md b/docs/release-notes.md index 1a38ec8..84e5d31 100644 --- a/docs/release-notes.md +++ b/docs/release-notes.md @@ -2,6 +2,12 @@ ### Latest Changes +### 0.1.4 + +* Changed project architecture +* Increased travis tests + + ### 0.1.3 * Make database optional diff --git a/manage_fastapi/__init__.py b/manage_fastapi/__init__.py index ae73625..bbab024 100644 --- a/manage_fastapi/__init__.py +++ b/manage_fastapi/__init__.py @@ -1 +1 @@ -__version__ = "0.1.3" +__version__ = "0.1.4" diff --git a/manage_fastapi/templates.py b/manage_fastapi/templates.py index 2ce5066..7a8872d 100644 --- a/manage_fastapi/templates.py +++ b/manage_fastapi/templates.py @@ -131,11 +131,11 @@ def test_read_items(): tortoise_main_template = """from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware -from {project_name}.{project_name}.models.database import Example +from {project_name}.core.models.database import Example from tortoise.contrib.fastapi import HTTPNotFoundError, register_tortoise -from {project_name}.{project_name}.settings import settings +from {project_name}.core.settings import settings app = FastAPI(title=settings.PROJECT_NAME) @@ -182,7 +182,7 @@ class PydanticMeta: empty_main_template = """from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware -from {project_name}.settings import settings +from {project_name}.core.settings import settings if settings.BACKEND_CORS_ORIGINS: app.add_middleware( @@ -199,8 +199,8 @@ class PydanticMeta: async_sql_main_template = """from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware -from {project_name}.{project_name}.settings import settings -from {project_name}.{project_name}.models.database import database +from {project_name}.core.settings import settings +from {project_name}.core.models.database import database app = FastAPI(title=settings.PROJECT_NAME) @@ -224,7 +224,7 @@ async def disconnect_database(): """ async_sql_database_template = """import sqlalchemy -from {project_name}.{project_name}.settings import settings +from {project_name}.core.settings import settings import databases database = databases.Database(settings.DATABASE_URL) @@ -246,8 +246,8 @@ async def disconnect_database(): mongo_main_template = """from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware -from {project_name}.{project_name}.settings import settings -from {project_name}.{project_name}.models.utils import connect_to_mongo, close_mongo_connection +from {project_name}.core.settings import settings +from {project_name}.core.models.utils import connect_to_mongo, close_mongo_connection app = FastAPI() @@ -283,7 +283,7 @@ async def get_database() -> AsyncIOMotorClient: mongo_utils_template = """import logging from motor.motor_asyncio import AsyncIOMotorClient -from {project_name}.{project_name}.models.database import db +from {project_name}.core.models.database import db logger = logging.getLogger(__name__) diff --git a/manage_fastapi/tests/test_utils_startapp.py b/manage_fastapi/tests/test_utils_startapp.py index 6312654..c588182 100644 --- a/manage_fastapi/tests/test_utils_startapp.py +++ b/manage_fastapi/tests/test_utils_startapp.py @@ -1,5 +1,5 @@ from typer.testing import CliRunner - +import os from manage_fastapi.main import app runner = CliRunner() @@ -9,6 +9,9 @@ def test_startapp_single(): result = runner.invoke(app, ["startapp", "myapp"]) assert result.exit_code == 0 assert "Application myapp created successfully!" in result.stdout + os.path.exists("./myapp") + os.path.exists("./tests/myapp") + os.path.exists("./myapp/endpoints") def test_startapp_duplicate(): diff --git a/manage_fastapi/tests/test_utils_startproject.py b/manage_fastapi/tests/test_utils_startproject.py index b3b1de7..5eaf38d 100644 --- a/manage_fastapi/tests/test_utils_startproject.py +++ b/manage_fastapi/tests/test_utils_startproject.py @@ -2,20 +2,38 @@ from manage_fastapi.main import app +import os + runner = CliRunner() def test_startproject_single(): - result = runner.invoke(app, ["startproject", "myproject"], "0") + result = runner.invoke(app, ["startproject", "test_one"], "0") + result_two = runner.invoke(app, ["startproject", "test_two"], "1") assert result.exit_code == 0 assert ( - "Project myproject created successfully!\nWe created requirements file for your project needs." + "Project test_one created successfully!\nWe created requirements file for your project needs." in result.stdout ) + assert result_two.exit_code == 0 + assert ( + "Project test_two created successfully!\nWe created requirements file for your project needs." + in result_two.stdout + ) + assert os.path.isdir("./test_one") + assert os.path.isdir("./test_one/core") + assert os.path.isdir("./test_one/core/models") + assert os.path.isdir("./test_one/core/schemas") + assert os.path.isdir("./test_two") + assert os.path.isdir("./test_two/core") + assert os.path.isdir("./test_two/core/models") + assert os.path.isdir("./test_two/core/schemas") def test_startproject_duplicate(): - result = runner.invoke(app, ["startproject", "myproject"], "2") + result = runner.invoke(app, ["startproject", "test_one"], "2") + result_two = runner.invoke(app, ["startproject", "test_two"], "9") assert result.exit_code == 0 - assert "Project myproject already exists!" in result.stdout - + assert "Project test_one already exists!" in result.stdout + assert result_two.exit_code == 0 + assert "Project test_two already exists!" in result_two.stdout diff --git a/manage_fastapi/tests/test_version.py b/manage_fastapi/tests/test_version.py index 576882e..beed6fe 100644 --- a/manage_fastapi/tests/test_version.py +++ b/manage_fastapi/tests/test_version.py @@ -2,4 +2,4 @@ def test_version(): - assert __version__ == "0.1.3" + assert __version__ == "0.1.4" diff --git a/manage_fastapi/utils.py b/manage_fastapi/utils.py index 328fff9..827d90b 100644 --- a/manage_fastapi/utils.py +++ b/manage_fastapi/utils.py @@ -26,22 +26,18 @@ def start_project( try: Path(f"{current_path}/{project_name}").mkdir(parents=True, exist_ok=False) Path(f"{current_path}/{project_name}/tests").mkdir(parents=True, exist_ok=False) - Path(f"{current_path}/{project_name}/{project_name}").mkdir( - parents=True, exist_ok=False - ) - Path(f"{current_path}/{project_name}/{project_name}/schemas").mkdir( + Path(f"{current_path}/{project_name}/core").mkdir(parents=True, exist_ok=False) + Path(f"{current_path}/{project_name}/core/schemas").mkdir( parents=True, exist_ok=False ) Path(f"{current_path}/{project_name}/__init__.py").touch() Path(f"{current_path}/{project_name}/tests/__init__.py").touch() - Path( - f"{current_path}/{project_name}/{project_name}/schemas/__init__.py" - ).touch() + Path(f"{current_path}/{project_name}/core/schemas/__init__.py").touch() with open( - f"{current_path}/{project_name}/{project_name}/schemas/schema.py", "a+" + f"{current_path}/{project_name}/core/schemas/schema.py", "a+" ) as schema, open( - f"{current_path}/{project_name}/{project_name}/settings.py", "a+" + f"{current_path}/{project_name}/core/settings.py", "a+" ) as settings, open( f"{current_path}/requirements.txt", "a+" ) as requirements: @@ -51,15 +47,13 @@ def start_project( if database_option == "0": - Path(f"{current_path}/{project_name}/{project_name}/models").mkdir( + Path(f"{current_path}/{project_name}/core/models").mkdir( parents=True, exist_ok=False ) - Path( - f"{current_path}/{project_name}/{project_name}/models/__init__.py" - ).touch() + Path(f"{current_path}/{project_name}/core/models/__init__.py").touch() with open( - f"{current_path}/{project_name}/{project_name}/models/database.py", "a+" + f"{current_path}/{project_name}/core/models/database.py", "a+" ) as database, open( f"{current_path}/{project_name}/main.py", "a+" ) as main, open( @@ -75,15 +69,13 @@ def start_project( # Tortoise ORM = 1 elif database_option == "1": - Path(f"{current_path}/{project_name}/{project_name}/models").mkdir( + Path(f"{current_path}/{project_name}/core/models").mkdir( parents=True, exist_ok=False ) - Path( - f"{current_path}/{project_name}/{project_name}/models/__init__.py" - ).touch() + Path(f"{current_path}/{project_name}/core/models/__init__.py").touch() with open( - f"{current_path}/{project_name}/{project_name}/models/database.py", "a+" + f"{current_path}/{project_name}/core/models/database.py", "a+" ) as database, open( f"{current_path}/{project_name}/main.py", "a+" ) as main, open( @@ -97,19 +89,17 @@ def start_project( # MongoDB elif database_option == "2": - Path(f"{current_path}/{project_name}/{project_name}/models").mkdir( + Path(f"{current_path}/{project_name}/core/models").mkdir( parents=True, exist_ok=False ) - Path( - f"{current_path}/{project_name}/{project_name}/models/__init__.py" - ).touch() + Path(f"{current_path}/{project_name}/core/models/__init__.py").touch() with open( - f"{current_path}/{project_name}/{project_name}/models/database.py", "a+" + f"{current_path}/{project_name}/core/models/database.py", "a+" ) as database, open( f"{current_path}/{project_name}/main.py", "a+" ) as main, open( - f"{current_path}/{project_name}/{project_name}/models/utils.py", "a+" + f"{current_path}/{project_name}/core/models/utils.py", "a+" ) as utils, open( f"{current_path}/requirements.txt", "a+" ) as requirements: @@ -126,7 +116,7 @@ def start_project( with open(f"{current_path}/{project_name}/main.py", "a+") as main: main.write(empty_main_template.replace("{project_name}", project_name)) - except FileExistsError as e: + except FileExistsError: print(f"Project {project_name} already exists!") else: @@ -152,7 +142,7 @@ def start_app(app_name: str, current_path: str = Path.cwd()): api.write(api_template.replace("{app_name}", app_name)) test.write(test_template.replace("{app_name}", app_name)) - except FileExistsError as e: + except FileExistsError: print(f"Application {app_name} already exists!") else: @@ -165,6 +155,11 @@ def select_database(): # TODO + + +# TODO + + # def run_server(server: str = ("uvicorn")): # import os # import subprocess diff --git a/pyproject.toml b/pyproject.toml index d2be430..356e0d1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "manage-fastapi" -version = "0.1.3" +version = "0.1.4" description = "Managing FastAPI projects made easy." authors = ["ycd "] readme = "README.md"