feat: add base class for all schemas and to camel case mapper
This commit is contained in:
parent
f97d419467
commit
fe91bb7103
@ -47,6 +47,7 @@ async def get_user_login(connection: AsyncConnection, login: str) -> Optional[Us
|
|||||||
|
|
||||||
if not user_db:
|
if not user_db:
|
||||||
return None
|
return None
|
||||||
|
print("user_db", user_db)
|
||||||
|
|
||||||
user_data = {
|
user_data = {
|
||||||
column.name: (
|
column.name: (
|
||||||
@ -56,6 +57,7 @@ async def get_user_login(connection: AsyncConnection, login: str) -> Optional[Us
|
|||||||
)
|
)
|
||||||
for column in account_table.columns
|
for column in account_table.columns
|
||||||
}
|
}
|
||||||
|
print("user_data", user_data)
|
||||||
|
|
||||||
return User.model_validate(user_data)
|
return User.model_validate(user_data)
|
||||||
|
|
||||||
|
@ -11,9 +11,7 @@ from fastapi import (
|
|||||||
status,
|
status,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
from pydantic.main import BaseModel
|
|
||||||
from fastapi_jwt_auth import AuthJWT
|
from fastapi_jwt_auth import AuthJWT
|
||||||
|
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
@ -26,7 +24,7 @@ from api.services.auth import authenticate_user
|
|||||||
|
|
||||||
from api.db.logic.auth import add_new_refresh_token, upgrade_old_refresh_token
|
from api.db.logic.auth import add_new_refresh_token, upgrade_old_refresh_token
|
||||||
|
|
||||||
from api.schemas.endpoints.auth import Auth
|
from api.schemas.endpoints.auth import Auth, AccessToken
|
||||||
|
|
||||||
api_router = APIRouter(
|
api_router = APIRouter(
|
||||||
prefix="/auth",
|
prefix="/auth",
|
||||||
@ -87,12 +85,7 @@ async def login_for_access_token(
|
|||||||
|
|
||||||
Authorize.set_refresh_cookies(refresh_token)
|
Authorize.set_refresh_cookies(refresh_token)
|
||||||
|
|
||||||
return {
|
return AccessToken(access_token=access_token)
|
||||||
"access_token": access_token,
|
|
||||||
# "access_token_expires": access_token_expires_time,
|
|
||||||
# "refresh_token": refresh_token,
|
|
||||||
# "refresh_token_expires": refresh_token_expires_time
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@api_router.post("/refresh")
|
@api_router.post("/refresh")
|
||||||
@ -121,9 +114,4 @@ async def refresh(
|
|||||||
|
|
||||||
new_access_token = Authorize.create_access_token(subject=current_user, expires_time=access_token_expires)
|
new_access_token = Authorize.create_access_token(subject=current_user, expires_time=access_token_expires)
|
||||||
|
|
||||||
return {
|
return AccessToken(access_token=new_access_token)
|
||||||
"access_token": new_access_token,
|
|
||||||
# "access_token_expires": access_token_expires_time,
|
|
||||||
# "refresh_token": refresh_token,
|
|
||||||
# "refresh_token_expires": refresh_token_expires_time
|
|
||||||
}
|
|
||||||
|
@ -2,7 +2,10 @@ import datetime
|
|||||||
from enum import Enum
|
from enum import Enum
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from pydantic import BaseModel, EmailStr, Field
|
from pydantic import EmailStr, Field
|
||||||
|
|
||||||
|
from api.schemas.base import Base
|
||||||
|
|
||||||
|
|
||||||
# Модель для хранения информации из запроса
|
# Модель для хранения информации из запроса
|
||||||
|
|
||||||
@ -21,7 +24,7 @@ class Status(Enum):
|
|||||||
DELETED = "DELETED"
|
DELETED = "DELETED"
|
||||||
|
|
||||||
|
|
||||||
class User(BaseModel):
|
class User(Base):
|
||||||
id: Optional[int] = None
|
id: Optional[int] = None
|
||||||
name: str = Field(..., max_length=100)
|
name: str = Field(..., max_length=100)
|
||||||
login: str = Field(..., max_length=100)
|
login: str = Field(..., max_length=100)
|
||||||
|
@ -1,9 +1,12 @@
|
|||||||
import datetime
|
import datetime
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from typing import Optional, Dict
|
from typing import Optional, Dict
|
||||||
from pydantic import BaseModel, Field
|
from pydantic import Field
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
|
from api.schemas.base import Base
|
||||||
|
|
||||||
|
|
||||||
# Модель для хранения информации из запроса
|
# Модель для хранения информации из запроса
|
||||||
|
|
||||||
|
|
||||||
@ -20,7 +23,7 @@ class StatusKey(Enum):
|
|||||||
DELETED = "DELETED"
|
DELETED = "DELETED"
|
||||||
|
|
||||||
|
|
||||||
class AccountKeyring(BaseModel):
|
class AccountKeyring(Base):
|
||||||
owner_id: int
|
owner_id: int
|
||||||
key_type: TypeKey # Используем тот же KeyType
|
key_type: TypeKey # Используем тот же KeyType
|
||||||
key_id: Optional[str] = Field(None, max_length=40) # Изменено на None как default
|
key_id: Optional[str] = Field(None, max_length=40) # Изменено на None как default
|
||||||
|
11
api/api/schemas/base.py
Normal file
11
api/api/schemas/base.py
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
from pydantic import BaseModel, ConfigDict
|
||||||
|
|
||||||
|
from api.utils.mapper import to_camel
|
||||||
|
|
||||||
|
|
||||||
|
class Base(BaseModel):
|
||||||
|
model_config = ConfigDict(
|
||||||
|
from_attributes=True,
|
||||||
|
alias_generator=to_camel,
|
||||||
|
populate_by_name=True
|
||||||
|
)
|
@ -1,7 +1,10 @@
|
|||||||
from enum import Enum
|
from enum import Enum
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from pydantic import BaseModel, EmailStr, Field
|
from pydantic import EmailStr, Field
|
||||||
|
|
||||||
|
from api.schemas.base import Base
|
||||||
|
|
||||||
|
|
||||||
# Таблица для получения информации из запроса
|
# Таблица для получения информации из запроса
|
||||||
|
|
||||||
@ -20,7 +23,7 @@ class Status(Enum):
|
|||||||
DELETED = "DELETED"
|
DELETED = "DELETED"
|
||||||
|
|
||||||
|
|
||||||
class UserUpdate(BaseModel):
|
class UserUpdate(Base):
|
||||||
id: Optional[int] = None
|
id: Optional[int] = None
|
||||||
name: Optional[str] = Field(None, max_length=100)
|
name: Optional[str] = Field(None, max_length=100)
|
||||||
login: Optional[str] = Field(None, max_length=100)
|
login: Optional[str] = Field(None, max_length=100)
|
||||||
|
@ -1,9 +1,12 @@
|
|||||||
import datetime
|
import datetime
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from pydantic import BaseModel, Field
|
from pydantic import Field
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
|
from api.schemas.base import Base
|
||||||
|
|
||||||
|
|
||||||
# Таблица для получения информации из запроса
|
# Таблица для получения информации из запроса
|
||||||
|
|
||||||
|
|
||||||
@ -20,7 +23,7 @@ class StatusKey(Enum):
|
|||||||
DELETED = "DELETED"
|
DELETED = "DELETED"
|
||||||
|
|
||||||
|
|
||||||
class AccountKeyringUpdate(BaseModel):
|
class AccountKeyringUpdate(Base):
|
||||||
owner_id: Optional[int] = None
|
owner_id: Optional[int] = None
|
||||||
key_type: Optional[TypeKey] = None
|
key_type: Optional[TypeKey] = None
|
||||||
key_id: Optional[str] = Field(None, max_length=40)
|
key_id: Optional[str] = Field(None, max_length=40)
|
||||||
|
@ -1,12 +1,17 @@
|
|||||||
from pydantic import BaseModel
|
from api.schemas.base import Base
|
||||||
|
|
||||||
|
|
||||||
# Таблица для получения информации из запроса
|
# Таблица для получения информации из запроса
|
||||||
|
|
||||||
|
|
||||||
class Auth(BaseModel):
|
class Auth(Base):
|
||||||
login: str
|
login: str
|
||||||
password: str
|
password: str
|
||||||
|
|
||||||
|
|
||||||
class Refresh(BaseModel):
|
class AccessToken(Base):
|
||||||
|
access_token: str
|
||||||
|
|
||||||
|
|
||||||
|
class Refresh(Base):
|
||||||
refresh_token: str
|
refresh_token: str
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
from pydantic import BaseModel, Field
|
from pydantic import Field
|
||||||
from typing import Dict, Any
|
from typing import Dict, Any
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
|
||||||
|
from api.schemas.base import Base
|
||||||
|
|
||||||
|
|
||||||
class State(Enum):
|
class State(Enum):
|
||||||
AUTO = "Auto"
|
AUTO = "Auto"
|
||||||
@ -15,7 +17,7 @@ class Status(Enum):
|
|||||||
DELETED = "Deleted"
|
DELETED = "Deleted"
|
||||||
|
|
||||||
|
|
||||||
class ListEvent(BaseModel):
|
class ListEvent(Base):
|
||||||
id: int
|
id: int
|
||||||
name: str = Field(..., max_length=40)
|
name: str = Field(..., max_length=40)
|
||||||
title: str = Field(..., max_length=64)
|
title: str = Field(..., max_length=64)
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
from pydantic import BaseModel, Field, conint
|
from pydantic import Field, conint
|
||||||
from typing import Dict, Any
|
from typing import Dict, Any
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
|
||||||
|
from api.schemas.base import Base
|
||||||
|
|
||||||
|
|
||||||
class Status(Enum):
|
class Status(Enum):
|
||||||
ACTIVE = "Active"
|
ACTIVE = "Active"
|
||||||
@ -11,7 +13,7 @@ class Status(Enum):
|
|||||||
DELETED = "Deleted"
|
DELETED = "Deleted"
|
||||||
|
|
||||||
|
|
||||||
class MyModel(BaseModel):
|
class MyModel(Base):
|
||||||
id: int
|
id: int
|
||||||
link_name: str = Field(..., max_length=20)
|
link_name: str = Field(..., max_length=20)
|
||||||
node_id: int
|
node_id: int
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
from pydantic import BaseModel, Field
|
from pydantic import Field
|
||||||
from typing import Dict, Any
|
from typing import Dict, Any
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
|
||||||
|
from api.schemas.base import Base
|
||||||
|
|
||||||
|
|
||||||
class Status(Enum):
|
class Status(Enum):
|
||||||
ACTIVE = "Active"
|
ACTIVE = "Active"
|
||||||
@ -11,7 +13,7 @@ class Status(Enum):
|
|||||||
DELETED = "Deleted"
|
DELETED = "Deleted"
|
||||||
|
|
||||||
|
|
||||||
class ProcessSchema(BaseModel):
|
class ProcessSchema(Base):
|
||||||
id: int
|
id: int
|
||||||
title: str = Field(..., max_length=100)
|
title: str = Field(..., max_length=100)
|
||||||
description: str
|
description: str
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
from pydantic import BaseModel, Field
|
|
||||||
from typing import Dict, Any
|
from typing import Dict, Any
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
|
from api.schemas.base import Base
|
||||||
|
|
||||||
class ProcessStatusSchema(BaseModel):
|
|
||||||
|
class ProcessStatusSchema(Base):
|
||||||
id: int
|
id: int
|
||||||
version: int
|
version: int
|
||||||
snapshot: Dict[str, Any]
|
snapshot: Dict[str, Any]
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
from pydantic import BaseModel
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import Dict, Any
|
from typing import Dict, Any
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
|
||||||
|
from api.schemas.base import Base
|
||||||
|
|
||||||
|
|
||||||
class NodeType(Enum):
|
class NodeType(Enum):
|
||||||
pass
|
pass
|
||||||
@ -14,7 +15,7 @@ class Status(Enum):
|
|||||||
DELETED = "Deleted"
|
DELETED = "Deleted"
|
||||||
|
|
||||||
|
|
||||||
class Ps_Node(BaseModel):
|
class Ps_Node(Base):
|
||||||
id: int
|
id: int
|
||||||
ps_id: int
|
ps_id: int
|
||||||
node_type: NodeType
|
node_type: NodeType
|
||||||
|
3
api/api/utils/mapper.py
Normal file
3
api/api/utils/mapper.py
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
def to_camel(string: str) -> str:
|
||||||
|
splitted = string.split("_")
|
||||||
|
return splitted[0] + "".join(word.capitalize() for word in splitted[1:])
|
Loading…
Reference in New Issue
Block a user