Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.idea
/__pycache__
/venv
38 changes: 38 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel, Field

wsd_app = FastAPI()

user_list = [
{
'mail': 'abc@yandex.ru',
'password': 'abc_pwd'
},
{
'mail': 'qwerty@gmail.com',
'password': 'qwerty'
}
]


@wsd_app.get("/statistics/")
async def get_statistics(indicator: str):
if indicator == "num_of_users":
return {'number of users': len(user_list)}
raise HTTPException(status_code=404, detail="Don't know this indicator")


class User(BaseModel):
mail: str = Field(..., regex=r".+@.+\..+")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

У pydantic есть отдельный тип EmailStr для валидации полей, хранящих почту.

password1: str
password2: str
Comment on lines +27 to +28

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Аналогично у pydantic есть поля и для паролей.

Совет с точки зрения проектирования: данные пользователя (имя, возраст, почта и все, что может быть нужно для какой-то аналитики и таргетирования) лучше хранить отдельно от данных для авторизации (логин/пароль/соли и т.д.).

Так можно ненароком забыть и спалить где-нибудь пользовательский пароль, и они очень сильно расстроятся, что хуже возможно засудят 😁



@wsd_app.post("/registration/", status_code=201)
async def create_account(user: User):
if user.mail in [d['mail'] for d in user_list]:
raise HTTPException(status_code=403, detail="This mail has been already registered")
if user.password1 != user.password2:
raise HTTPException(status_code=403, detail="Passwords don't match")
user_list.append({'mail': user.mail, 'password': user.password1})
return "Account is created"