From a81b14c173ef34fe4a0c08c6f8fc2c9edc7bca67 Mon Sep 17 00:00:00 2001 From: ycd Date: Sat, 15 Aug 2020 17:37:52 +0300 Subject: [PATCH 01/10] change inner project name --- manage_fastapi/utils.py | 60 ++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 34 deletions(-) diff --git a/manage_fastapi/utils.py b/manage_fastapi/utils.py index 328fff9..af9c1a2 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: @@ -165,13 +155,15 @@ def select_database(): # TODO -# def run_server(server: str = ("uvicorn")): -# import os -# import subprocess - -# project_name = os.getenv("PROJECT_NAME") -# print(project_name) -# subprocess.run( -# [server, f"{Path.cwd()}/{project_name}/main:app".replace("/", "."), "--reload"] -# ) + + +def run_server(server: str = ("uvicorn")): + import subprocess + + subprocess.run( + [server, f"{Path.cwd()}/{project_name}/main:app".replace("/", "."), "--reload"] + ) + + +# From 583469df8b6d1c7aa234cd4df3a32a3159b30b69 Mon Sep 17 00:00:00 2001 From: ycd Date: Sat, 15 Aug 2020 17:38:03 +0300 Subject: [PATCH 02/10] change inner project name --- manage_fastapi/templates.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) 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__) From 26431732c614c5495c2badf6e1e385e1a73e4106 Mon Sep 17 00:00:00 2001 From: ycd Date: Sat, 15 Aug 2020 17:39:44 +0300 Subject: [PATCH 03/10] fix comments --- manage_fastapi/utils.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/manage_fastapi/utils.py b/manage_fastapi/utils.py index af9c1a2..953a8ba 100644 --- a/manage_fastapi/utils.py +++ b/manage_fastapi/utils.py @@ -157,13 +157,13 @@ def select_database(): # TODO -def run_server(server: str = ("uvicorn")): - import subprocess - - subprocess.run( - [server, f"{Path.cwd()}/{project_name}/main:app".replace("/", "."), "--reload"] - ) - - -# +# TODO +# def run_server(server: str = ("uvicorn")): +# import os +# import subprocess +# project_name = os.getenv("PROJECT_NAME") +# print(project_name) +# subprocess.run( +# [server, f"{Path.cwd()}/{project_name}/main:app".replace("/", "."), "--reload"] +# ) From 50703549812675232c4bfee0e9556d649637743e Mon Sep 17 00:00:00 2001 From: ycd Date: Sun, 16 Aug 2020 04:16:54 +0300 Subject: [PATCH 04/10] increase tests --- manage_fastapi/tests/test_utils_startapp.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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(): From c11e04b2e1f6d9edaba37da59c9f84c280be21d7 Mon Sep 17 00:00:00 2001 From: ycd Date: Sun, 16 Aug 2020 04:17:06 +0300 Subject: [PATCH 05/10] increase tests --- .../tests/test_utils_startproject.py | 28 +++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) 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 From fbe3469e47c6b8f57366f4cf773d128ba59f0ac1 Mon Sep 17 00:00:00 2001 From: ycd Date: Sun, 16 Aug 2020 04:21:16 +0300 Subject: [PATCH 06/10] delete error capture --- manage_fastapi/utils.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/manage_fastapi/utils.py b/manage_fastapi/utils.py index 953a8ba..92a209e 100644 --- a/manage_fastapi/utils.py +++ b/manage_fastapi/utils.py @@ -116,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: @@ -142,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: @@ -158,12 +158,15 @@ def select_database(): # TODO -# def run_server(server: str = ("uvicorn")): -# import os -# import subprocess -# project_name = os.getenv("PROJECT_NAME") -# print(project_name) -# subprocess.run( -# [server, f"{Path.cwd()}/{project_name}/main:app".replace("/", "."), "--reload"] -# ) + + +def run_server(server: str = ("uvicorn")): + import os + import subprocess + + project_name = os.getenv("PROJECT_NAME") + print(project_name) + subprocess.run( + [server, f"{Path.cwd()}/{project_name}/main:app".replace("/", "."), "--reload"] + ) From 79e8af14377c93be8486668f69eae32916fe169a Mon Sep 17 00:00:00 2001 From: ycd Date: Sun, 16 Aug 2020 04:40:38 +0300 Subject: [PATCH 07/10] update version --- manage_fastapi/__init__.py | 2 +- manage_fastapi/tests/test_version.py | 2 +- manage_fastapi/utils.py | 18 +++++++++--------- 3 files changed, 11 insertions(+), 11 deletions(-) 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/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 92a209e..827d90b 100644 --- a/manage_fastapi/utils.py +++ b/manage_fastapi/utils.py @@ -160,13 +160,13 @@ def select_database(): # TODO -def run_server(server: str = ("uvicorn")): - import os - import subprocess - - project_name = os.getenv("PROJECT_NAME") - print(project_name) - subprocess.run( - [server, f"{Path.cwd()}/{project_name}/main:app".replace("/", "."), "--reload"] - ) +# def run_server(server: str = ("uvicorn")): +# import os +# import subprocess + +# project_name = os.getenv("PROJECT_NAME") +# print(project_name) +# subprocess.run( +# [server, f"{Path.cwd()}/{project_name}/main:app".replace("/", "."), "--reload"] +# ) From 18fe0f540065f12b5b95e4e9bcc9b262c394ac90 Mon Sep 17 00:00:00 2001 From: ycd Date: Sun, 16 Aug 2020 04:41:11 +0300 Subject: [PATCH 08/10] Update README --- README.md | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) 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 From aae92340802f378d617f896d82d4a49ceb38ba61 Mon Sep 17 00:00:00 2001 From: ycd Date: Sun, 16 Aug 2020 04:42:24 +0300 Subject: [PATCH 09/10] update docs --- docs/index.md | 4 ++-- docs/managing_apps/startapp.md | 6 +++--- docs/managing_projects/startproject.md | 18 +++++++++--------- docs/release-notes.md | 6 ++++++ 4 files changed, 20 insertions(+), 14 deletions(-) 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 From 6e873338112d6615eafe7c372dcefc7575dab29f Mon Sep 17 00:00:00 2001 From: ycd Date: Sun, 16 Aug 2020 04:43:48 +0300 Subject: [PATCH 10/10] update project version 0.1.4 --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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"