Skip to content

Commit 1cd7819

Browse files
committed
added ruff
1 parent e255b11 commit 1cd7819

File tree

10 files changed

+375
-204
lines changed

10 files changed

+375
-204
lines changed

.coveragerc

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[run]
2+
omit =
3+
*/tests/*
4+
*/__init__.py
5+
*/migrations/*
6+
*/config/*
7+
*/middleware/*
8+
*/celery.py
9+
*/tasks/*

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ ENV/
3131
.pytest_cache/
3232
.coverage
3333
htmlcov/
34+
coverage.xml
35+
3436

3537
# IDE specific files
3638
.idea/
@@ -65,3 +67,6 @@ docker-compose.override.yml
6567
docs/_build/
6668

6769
app/tests/
70+
71+
fastapi-pundra/
72+
fastapi-pundra1/

.pre-commit-config.yaml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
repos:
2+
- repo: https://github.com/astral-sh/ruff-pre-commit
3+
# Ruff version.
4+
rev: v0.9.6
5+
hooks:
6+
# Run the linter.
7+
- id: ruff
8+
args: [ --fix ]
9+
# Run the formatter.
10+
- id: ruff-format

Pipfile

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pytest-cov = "*"
2626
factory-boy = "*"
2727
httpx = "*"
2828
ruff = "*"
29+
pre-commit = "*"
2930

3031
[requires]
3132
python_version = "3.12"

Pipfile.lock

+290-176
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+20-1
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,23 @@ alembic upgrade head
105105
106106
## postman collection documentation
107107

108-
* [postman collection documentation](https://documenter.getpostman.com/view/9920489/2sAYQZGBNJ)
108+
* [postman collection documentation](https://documenter.getpostman.com/view/9920489/2sAYQZGBNJ)
109+
110+
## Linting and formatting
111+
112+
### with script
113+
114+
```bash
115+
./scripts/lint.sh
116+
./scripts/format.sh
117+
```
118+
119+
120+
### with pre-commit
121+
122+
```bash
123+
pre-commit install
124+
```
125+
126+
> [!NOTE]
127+
> pre-commit will run automatically when you commit your changes.

app/api/v1/user.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from fastapi import APIRouter, BackgroundTasks, Request, status
3+
from fastapi import APIRouter, Request, status
44
from fastapi.encoders import jsonable_encoder
55
from fastapi.responses import JSONResponse
66
from fastapi_pundra.rest.helpers import the_query
@@ -19,13 +19,13 @@
1919
# Registration route
2020
@router.post("/users/registration")
2121
@dto(UserCreateSchema)
22-
async def registration(request: Request, background_tasks: BackgroundTasks) -> JSONResponse:
22+
async def registration(request: Request) -> JSONResponse:
2323
"""Register a new user."""
2424
# Retrieve data from the request
2525
request_data = await the_query(request)
2626
data = UserCreateSchema(**request_data)
2727

28-
output = await user_service.s_registration(request, data, background_tasks)
28+
output = await user_service.s_registration(request, data)
2929
return JSONResponse(content=output, status_code=status.HTTP_201_CREATED)
3030

3131

app/services/user_service.py

+19-24
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
from fastapi import BackgroundTasks, Request
1+
from fastapi import Request
22
from fastapi_pundra.common.jwt_utils import create_access_token, create_refresh_token
3-
from fastapi_pundra.common.mailer.mail import send_mail_background
43
from fastapi_pundra.common.password import compare_hashed_password, generate_password_hash
54
from fastapi_pundra.rest.exceptions import (
65
BaseAPIException,
@@ -9,6 +8,7 @@
98
)
109
from fastapi_pundra.rest.helpers import the_query, the_sorting
1110
from fastapi_pundra.rest.paginate import paginate
11+
from sqlalchemy.orm import Session
1212

1313
from app.database import get_db
1414
from app.models.users import User
@@ -19,16 +19,11 @@
1919
class UserService:
2020
"""User service."""
2121

22-
def __init__(self) -> None:
22+
def __init__(self, db: Session | None = None) -> None:
2323
"""Initialize the user service."""
24-
self.db = get_db()
25-
26-
async def s_registration(
27-
self,
28-
request: Request,
29-
data: UserCreateSchema,
30-
background_tasks: BackgroundTasks,
31-
) -> dict:
24+
self.db = db or get_db()
25+
26+
async def s_registration(self, request: Request, data: UserCreateSchema) -> dict:
3227
"""Register a new user."""
3328
db_user = self.db.query(User).filter(User.email == data.email).first()
3429

@@ -48,19 +43,19 @@ async def s_registration(
4843
user_data = UserSerializer(**new_user.as_dict())
4944

5045
# Send welcome email in background
51-
template_name = "welcome_email.html"
52-
context = {
53-
"name": new_user.name or new_user.email,
54-
"activation_link": f"{request.base_url}api/v1/users/activate",
55-
}
56-
57-
await send_mail_background(
58-
background_tasks=background_tasks,
59-
subject=f"Welcome, {new_user.name or new_user.email}!",
60-
to=[new_user.email],
61-
template_name=template_name,
62-
context=context,
63-
)
46+
# template_name = "welcome_email.html"
47+
# context = {
48+
# "name": new_user.name or new_user.email,
49+
# "activation_link": f"{request.base_url}api/v1/users/activate",
50+
# }
51+
52+
# await send_mail_background(
53+
# background_tasks=background_tasks,
54+
# subject=f"Welcome, {new_user.name or new_user.email}!",
55+
# to=[new_user.email],
56+
# template_name=template_name,
57+
# context=context,
58+
# )
6459

6560
return {"message": "Registration successful", "user": user_data.model_dump()}
6661

ruff.toml

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ ignore = [
5252
"FIX002", # Line contains TODO, consider resolving the issue
5353
"COM812", # Missing trailing comma in Python 3.6+
5454
"N805", # Instance method first argument name should be 'self'
55+
"ERA001",
5556
]
5657

5758
# Allow unused variables when underscore-prefixed

scripts/test.sh

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
3+
set -e
4+
set -x
5+
6+
7+
# Run pytest with coverage
8+
pytest \
9+
--cov=app \
10+
--cov-report=term-missing \
11+
--cov-report=html \
12+
--cov-report=xml \
13+
--cov-fail-under=80 \
14+
--cov-config=.coveragerc \
15+
-v \
16+
app/tests/ \
17+
"${@}"

0 commit comments

Comments
 (0)