Skip to content

Commit 51d64de

Browse files
committed
Add community role atribute. Comunity orm fucntions and Admin route.
1 parent e4ea178 commit 51d64de

7 files changed

Lines changed: 667 additions & 470 deletions

File tree

app/main.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from fastapi import FastAPI
55
from slowapi import _rate_limit_exceeded_handler
66

7+
from app.routers.admin.admin import create_community_admin
78
from app.routers.router import setup_router as setup_router_v2
89
from app.services.database.database import AsyncSessionLocal, init_db
910
from app.services.limiter import limiter
@@ -16,6 +17,7 @@ async def lifespan(app: FastAPI):
1617
# add check db file and create if not found
1718
await init_db()
1819
app.db_session_factory = AsyncSessionLocal()
20+
await create_community_admin(app.db_session_factory)
1921
try:
2022
yield
2123
finally:

app/routers/admin/__init__.py

Whitespace-only changes.

app/routers/admin/admin.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import os
2+
from typing import Annotated
3+
4+
from fastapi import APIRouter, Depends, Request, status
5+
from pydantic import BaseModel
6+
from sqlmodel.ext.asyncio.session import AsyncSession
7+
8+
from app.routers.authentication import get_current_active_community
9+
from app.services import auth
10+
from app.services.database.models import Community as DBCommunity # Precisa?
11+
from app.services.database.orm.community import create_community
12+
13+
# ADMIN_USER = os.getenv("ADMIN_USER")
14+
# ADMIN_PASSWORD = os.getenv("ADMIN_PASSWORD")
15+
16+
17+
async def create_community_admin(session: AsyncSession):
18+
ADMIN_USER = os.getenv("ADMIN_USER")
19+
ADMIN_PASSWORD = os.getenv("ADMIN_PASSWORD")
20+
password = ADMIN_PASSWORD
21+
hashed_password = auth.hash_password(password)
22+
community = DBCommunity(
23+
username=ADMIN_USER,
24+
email="ADMIN_USER",
25+
password=hashed_password,
26+
role="admin",
27+
)
28+
session: AsyncSession = session
29+
session.add(community)
30+
await session.commit()
31+
await session.refresh(community)
32+
return {"msg": "Admin successfully created"}
33+
34+
35+
class CommunityPostResponse(BaseModel):
36+
status: str = "Community Criado"
37+
38+
39+
def setup():
40+
router = APIRouter(prefix="/admin", tags=["news"])
41+
42+
@router.post(
43+
"create_community",
44+
response_model=CommunityPostResponse,
45+
status_code=status.HTTP_200_OK,
46+
summary="Create Community endpoint",
47+
description="Create Community and returns a confirmation message",
48+
)
49+
async def post_create_community(
50+
request: Request,
51+
admin_community: Annotated[
52+
DBCommunity, Depends(get_current_active_community)
53+
],
54+
community: Annotated[DBCommunity],
55+
):
56+
"""
57+
Server Admin endpoint that creates Community and returns a confirmation message.
58+
"""
59+
admin_role = admin_community.get("role")
60+
if admin_role != "admin":
61+
return {"status": "Unauthorized"}
62+
await create_community(
63+
session=request.app.db_session_factory, community=community
64+
)
65+
66+
return CommunityPostResponse()

app/services/database/models/communities.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class Community(SQLModel, table=True):
1111
username: str
1212
email: str
1313
password: str
14+
role: str = Field(default="user")
1415
created_at: Optional[datetime] = Field(default_factory=datetime.now)
1516
updated_at: Optional[datetime] = Field(
1617
default_factory=datetime.now,

app/services/database/orm/community.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from typing import Optional
22

3+
from fastapi import Request
34
from sqlmodel import select
45
from sqlmodel.ext.asyncio.session import AsyncSession
56

@@ -22,3 +23,19 @@ async def get_community_by_username(
2223
community = result.first()
2324

2425
return community
26+
27+
28+
async def create_community(
29+
request: Request,
30+
community: Community, # community model
31+
) -> Optional[Community]:
32+
"""
33+
Cria um novo membro da comunidade.
34+
Somente usuário autenticado e com role Admin podem executar.
35+
"""
36+
session: AsyncSession = request.app.db_session_factory
37+
session.add(community)
38+
await session.commit()
39+
await session.refresh(community)
40+
41+
return community

docker-compose.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ services:
1515
- SQLITE_URL=sqlite+aiosqlite://
1616
- SECRET_KEY=1a6c5f3b7d2e4a7fb68d0casd3f9a7b2d8c4e5f6a3b0d4e9c7a8f1b6d3c0a7f5e
1717
- ENCRYPTION_KEY=smR739opNB9FJ4hEm5ZIG8Gr-Qnvqtem4ehwl4RIUes=
18+
- ADMIN_USER=admin
19+
- ADMIN_PASSWORD=admin
1820
- ALGORITHM=HS256
1921
- ACCESS_TOKEN_EXPIRE_MINUTES=20
2022
restart: unless-stopped

0 commit comments

Comments
 (0)