Skip to content

Commit

Permalink
add login-route
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduard0803 committed Nov 2, 2023
1 parent aef3d41 commit 14a0d4b
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 4 deletions.
29 changes: 29 additions & 0 deletions gestao/db/migrations/versions/2023-11-02-16-33_e8a3e8746d3e.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""rename column user.mother_date to user.mother_name and add column user.password
Revision ID: e8a3e8746d3e
Revises: 0084e7dffc7c
Create Date: 2023-11-02 16:33:58.855003
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'e8a3e8746d3e'
down_revision = '0084e7dffc7c'
branch_labels = None
depends_on = None


def upgrade() -> None:
op.alter_column("user", "mother_date", new_column_name="mother_name", type_=sa.String(length=200))
with op.batch_alter_table("user") as batch_op:
batch_op.add_column(
sa.Column("password", sa.String(200), nullable=True),
)

def downgrade() -> None:
op.alter_column("user", "mother_name", new_column_name="mother_date", type_=sa.String(length=200))
with op.batch_alter_table("user") as batch_op:
batch_op.drop_column("password")
3 changes: 2 additions & 1 deletion gestao/db/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Meta(BaseMeta):
blood_type: str = ormar.String(max_length=200)
gender: str = ormar.String(max_length=200)
father_name: str = ormar.String(max_length=200)
mother_date: str = ormar.String(max_length=200)
mother_name: str = ormar.String(max_length=200)
position: str = ormar.String(max_length=200)
occupancy: str = ormar.String(max_length=200)
admission_date: date = ormar.Date()
Expand All @@ -40,6 +40,7 @@ class Meta(BaseMeta):
status: str = ormar.String(max_length=200, default="active")
workstation: Optional[str] = ormar.String(max_length=200, nullable=True)
nickname: Optional[str] = ormar.String(max_length=200, unique=True, nullable=True)
password: str = ormar.String(max_length=200)
dispatcher: str = ormar.String(max_length=200)
dispatched_date: date = ormar.Date()
created_at: datetime = ormar.DateTime(timezone=True, default=datetime.now)
Expand Down
11 changes: 9 additions & 2 deletions gestao/web/api/user/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class CreateUserDTO(BaseModel):
blood_type: str
gender: str
father_name: str
mother_date: str
mother_name: str
position: str
occupancy: str
admission_date: date
Expand All @@ -46,6 +46,7 @@ class CreateUserDTO(BaseModel):
dependents: Optional[List[CreateUserDependentDTO]]
workstation: Optional[str]
nickname: Optional[str]
password: str


class UpdateUserDTO(BaseModel):
Expand All @@ -62,7 +63,7 @@ class UpdateUserDTO(BaseModel):
blood_type: Optional[str]
gender: Optional[str]
father_name: Optional[str]
mother_date: Optional[str]
mother_name: Optional[str]
position: Optional[str]
occupancy: Optional[str]
admission_date: Optional[date]
Expand All @@ -80,3 +81,9 @@ class UpdateUserDTO(BaseModel):
workstation: Optional[str]
nickname: Optional[str]
status: Optional[str]
password: Optional[str]


class AuthUserDTO(BaseModel):
registration: str
password: str
20 changes: 19 additions & 1 deletion gestao/web/api/user/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from gestao.db.models.dependent import Dependent
from gestao.db.models.user import User
from gestao.web.api.user.enums import UserStatus
from gestao.web.api.user.schemas import CreateUserDTO, UpdateUserDTO
from gestao.web.api.user.schemas import CreateUserDTO, UpdateUserDTO, AuthUserDTO

router = APIRouter()

Expand Down Expand Up @@ -108,3 +108,21 @@ async def disable_user(user_id: str) -> None:
status_code=404,
detail="User not found",
)


@router.get("_auth/login", response_model_exclude={"dependents__user_id"})
async def login_user(user_data: AuthUserDTO) -> User:
try:
user_data_dict = user_data.dict()
return await User.objects.select_related(
User.dependents,
).get(
registration=user_data_dict["registration"],
password=user_data_dict["password"],
)
except Exception:
logging.error("Error occurred in login user", exc_info=True)
raise HTTPException(
status_code=404,
detail="User not found",
)

0 comments on commit 14a0d4b

Please sign in to comment.