Skip to content

Commit 4b367db

Browse files
committed
add cookiecutter option to choose template, update tests for templates and github workflow, run formatting
1 parent 0ed3ae8 commit 4b367db

File tree

27 files changed

+183
-126
lines changed

27 files changed

+183
-126
lines changed

.github/workflows/tests.yml

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,33 +32,64 @@ jobs:
3232
uses: actions/setup-python@v2
3333
with:
3434
python-version: ${{ matrix.python-version }}
35-
- name: Load cached venv
36-
id: cached-poetry-dependencies
35+
- name: Load cached venv1
36+
id: cached-poetry-dependencies1
3737
uses: actions/cache@v2
3838
with:
39-
path: .venv
40-
key: venv-${{ runner.os }}-${{ matrix.python-version }}-${{ hashFiles('**/poetry.lock') }}
39+
path: .venv1
40+
key: venv-${{ runner.os }}-${{ matrix.python-version }}-${{ hashFiles('{{cookiecutter.project_name}}/template_minimal/poetry.lock') }}
4141
- name: Install dependencies and actiavte virtualenv
42-
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
42+
if: steps.cached-poetry-dependencies1.outputs.cache-hit != 'true'
4343
run: |
44-
python -m venv .venv
45-
source .venv/bin/activate
46-
pip install -r {{cookiecutter.project_name}}/requirements-dev.txt
44+
python -m venv .venv1
45+
source .venv1/bin/activate
46+
pip install -r {{cookiecutter.project_name}}/template_minimal/requirements-dev.txt
4747
pip install cookiecutter
48-
- name: Lint with flake8
48+
- name: Load cached venv2
49+
id: cached-poetry-dependencies2
50+
uses: actions/cache@v2
51+
with:
52+
path: .venv2
53+
key: venv-${{ runner.os }}-${{ matrix.python-version }}-${{ hashFiles('{{cookiecutter.project_name}}/template_fastapi_users/poetry.lock') }}
54+
- name: Install dependencies and actiavte virtualenv
55+
if: steps.cached-poetry-dependencies2.outputs.cache-hit != 'true'
56+
run: |
57+
python -m venv .venv2
58+
source .venv2/bin/activate
59+
pip install -r {{cookiecutter.project_name}}/template_minimal/requirements-dev.txt
60+
pip install cookiecutter
61+
- name: Lint with flake8 minimal project
62+
run: |
63+
source .venv1/bin/activate
64+
# stop the build if there are Python syntax errors or undefined names
65+
cd \{\{cookiecutter.project_name\}\}/template_minimal
66+
flake8 app --count --exit-zero --statistics
67+
- name: Lint with flake8 fastapi_users project
4968
run: |
50-
source .venv/bin/activate
69+
source .venv2/bin/activate
5170
# stop the build if there are Python syntax errors or undefined names
52-
cd \{\{cookiecutter.project_name\}\}/
71+
cd \{\{cookiecutter.project_name\}\}/template_fastapi_users
5372
flake8 app --count --exit-zero --statistics
54-
- name: Test new cookiecuttered project is passing pytest test
73+
- name: Test minimal project is passing pytest test
74+
run: |
75+
source .venv1/bin/activate
76+
python tests/create_minimal_project.py
77+
export TEST_DATABASE_HOSTNAME=localhost
78+
export TEST_DATABASE_USER=test
79+
export TEST_DATABASE_PASSWORD=test
80+
export TEST_DATABASE_PORT=30000
81+
export TEST_DATABASE_DB=test
82+
83+
pytest minimal_project
84+
85+
- name: Test fastapi_users project is passing pytest test
5586
run: |
56-
source .venv/bin/activate
57-
python tests/create_test_project.py
87+
source .venv2/bin/activate
88+
python tests/create_fastapi_users_project.py
5889
export TEST_DATABASE_HOSTNAME=localhost
5990
export TEST_DATABASE_USER=test
6091
export TEST_DATABASE_PASSWORD=test
6192
export TEST_DATABASE_PORT=30000
6293
export TEST_DATABASE_DB=test
6394
64-
pytest generated_project_for_tests
95+
pytest fastapi_users_project

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
.vscode
1+
.vscode
2+
.venv
3+
venv
4+
.venv1
5+
.venv2

cookiecutter.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"project_name": "Base Project"
3-
}
2+
"project_name": "Base Project",
3+
"use_fastapi_users": false
4+
}

hooks/post_gen_project.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,40 @@
11
"""
2+
Copy template choosen from TEMPLATES to main folder.
3+
24
Copy content from env template (autogenerated passwords etc)
3-
to .env which is gitignored, then remove template env file as it is redundant
5+
to .env which is gitignored, then remove template env file as it is redundant.
46
"""
5-
67
from pathlib import Path
8+
from shutil import copytree, rmtree
9+
10+
PROJECT_NAME = "{{ cookiecutter.module_name }}"
11+
USE_FASTAPI_USERS = bool("{{ cookiecutter.use_fastapi_users }}")
12+
TEMPLATES = ["template_fastapi_users", "template_minimal"]
13+
14+
15+
def copy_choosen_template_to_main_dir(used_template: str):
16+
if used_template not in TEMPLATES:
17+
raise ValueError(f"{used_template} not in {TEMPLATES}")
18+
19+
copytree(used_template, "./")
20+
21+
for template in TEMPLATES:
22+
rmtree(Path(template))
23+
24+
25+
def create_env_file_and_remove_env_template():
26+
env_template_file = Path(".env.template")
27+
env_file = Path(".env")
28+
29+
env_file.write_text(env_template_file.read_text())
30+
env_template_file.unlink()
31+
732

8-
env_template_file = Path("./.env.template")
9-
env_file = Path("./.env")
33+
if __name__ == "__main__":
34+
if USE_FASTAPI_USERS:
35+
used_template = "template_fastapi_users"
36+
else:
37+
used_template = "template_minimal"
1038

11-
env_file.write_text(env_template_file.read_text())
12-
env_template_file.unlink()
39+
copy_choosen_template_to_main_dir(used_template=used_template)
40+
create_env_file_and_remove_env_template()
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
Creates fastapi_users template project in root folder with default values
3+
"""
4+
5+
from pathlib import Path
6+
7+
from cookiecutter.main import cookiecutter
8+
9+
ROOT_FOLDER = Path(__file__).parent.parent
10+
11+
12+
def main():
13+
cookiecutter(
14+
template=str(ROOT_FOLDER),
15+
no_input=True,
16+
extra_context={
17+
"project_name": "fastapi_users_project",
18+
"use_fastapi_users": True,
19+
},
20+
)
21+
22+
23+
if __name__ == "__main__":
24+
main()
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Creates template project in root folder with default values
2+
Creates minimal template project in root folder with default values
33
"""
44

55
from pathlib import Path
@@ -14,7 +14,8 @@ def main():
1414
template=str(ROOT_FOLDER),
1515
no_input=True,
1616
extra_context={
17-
"project_name": "generated_project_for_tests",
17+
"project_name": "minimal_project",
18+
"use_fastapi_users": False,
1819
},
1920
)
2021

{{cookiecutter.project_name}}/template_fastapi_users/.env.example

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
SECRET_KEY="DVnFmhwvjEhJZpuhndxjhlezxQPJmBIIkMDEmFREWQADPcUnrG"
22
ENVIRONMENT="DEV"
33
ACCESS_TOKEN_EXPIRE_MINUTES="11520"
4-
REFRESH_TOKEN_EXPIRE_MINUTES="40320"
54
BACKEND_CORS_ORIGINS="http://localhost:3000,http://localhost:8001"
65

76
DEFAULT_DATABASE_HOSTNAME="localhost"

{{cookiecutter.project_name}}/template_fastapi_users/.env.template

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
SECRET_KEY="{{ random_ascii_string(50) }}"
22
ENVIRONMENT="DEV"
33
ACCESS_TOKEN_EXPIRE_MINUTES="11520"
4-
REFRESH_TOKEN_EXPIRE_MINUTES="40320"
54
BACKEND_CORS_ORIGINS="http://localhost:3000,http://localhost:8001"
65

76
DEFAULT_DATABASE_HOSTNAME="localhost"

{{cookiecutter.project_name}}/template_fastapi_users/app/core/config.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ class Settings(BaseSettings):
3434
SECRET_KEY: str
3535
ENVIRONMENT: Literal["DEV", "PYTEST", "STAGE", "PRODUCTION"]
3636
ACCESS_TOKEN_EXPIRE_MINUTES: int
37-
REFRESH_TOKEN_EXPIRE_MINUTES: int
3837
BACKEND_CORS_ORIGINS: Union[str, list[AnyHttpUrl]]
3938

4039
# PROJECT NAME, VERSION AND DESCRIPTION

{{cookiecutter.project_name}}/template_fastapi_users/app/core/security.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
"""
2-
You can have several authentication methods, e.g. a cookie
2+
You can have several authentication methods, e.g. a cookie
33
authentication for browser-based queries and a JWT token authentication for pure API queries.
44
55
In this template, token will be sent through Bearer header
66
{"Authorization": "Bearer xyz"}
77
using JWT tokens.
88
99
There are more option to consider, refer to
10-
https://fastapi-users.github.io/fastapi-users/configuration/authentication/
10+
https://fastapi-users.github.io/fastapi-users/configuration/authentication/
1111
1212
UserManager class is core fastapi users class with customizable attrs and methods
1313
https://fastapi-users.github.io/fastapi-users/configuration/user-manager/

0 commit comments

Comments
 (0)