first commit
This commit is contained in:
0
server/backend/__init__.py
Normal file
0
server/backend/__init__.py
Normal file
66
server/backend/endpoints.py
Normal file
66
server/backend/endpoints.py
Normal file
@@ -0,0 +1,66 @@
|
||||
from fastapi import FastAPI, HTTPException
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from . import pydentic
|
||||
from server.database import db
|
||||
import asyncio
|
||||
|
||||
api = FastAPI()
|
||||
|
||||
api.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"], # "*" — разрешить всем; можно указать список конкретных доменов
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"], # GET, POST, PUT, DELETE и т.д.
|
||||
allow_headers=["*"], # Разрешить любые заголовки
|
||||
)
|
||||
|
||||
@api.get("/", response_model=pydentic.IdofPersons)
|
||||
async def get_all_rows():
|
||||
for row in await db.get_all_rows():
|
||||
if row:
|
||||
return row
|
||||
else:
|
||||
raise HTTPException(status_code=404, detail="The user isn't found")
|
||||
@api.get("/get_user/{id}", response_model=pydentic.IdofPersons)
|
||||
async def get_user(id:int):
|
||||
user = await db.GetUser(id)
|
||||
if user:
|
||||
return user
|
||||
else:
|
||||
raise HTTPException(status_code=404, detail="The user isn't found")
|
||||
@api.post("/user_create", response_model=pydentic.IdofPersons)
|
||||
async def create_user(row:pydentic.CreateUser):
|
||||
new_user_id = max(item.id for item in await db.get_all_rows())
|
||||
new_row = pydentic.IdofPersons(id = new_user_id, email=row.email, description=row.description, activated = row.activated, password = row.password)
|
||||
await db.CreateUser(new_row)
|
||||
return new_row
|
||||
@api.delete("/user_delete/{id}", response_model=pydentic.IdofPersons)
|
||||
async def delete_user(id: int):
|
||||
user = await db.GetUser(id)
|
||||
if not user:
|
||||
raise HTTPException(status_code=404, detail="The user isn't found")
|
||||
await db.DeleteUser(id)
|
||||
return user
|
||||
@api.put("/user_update/{id}", response_model=pydentic.IdofPersons)
|
||||
async def update_user(id: int, updated_row: pydentic.UserUpdate):
|
||||
user = await db.GetUser(id)
|
||||
if not user:
|
||||
raise HTTPException(status_code=404, detail="The user isn't found")
|
||||
changed = False
|
||||
if updated_row.email is not None and updated_row.email != user.email:
|
||||
user.email = updated_row.email
|
||||
changed = True
|
||||
if updated_row.description is not None and updated_row.description != user.description:
|
||||
user.description = updated_row.description
|
||||
changed = True
|
||||
if updated_row.activated is not None and updated_row.activated != user.activated:
|
||||
user.activated = updated_row.activated
|
||||
changed = True
|
||||
if updated_row.password is not None and updated_row.password != user.password:
|
||||
user.password = updated_row.password
|
||||
changed = True
|
||||
if changed:
|
||||
await db.UpdateUser(user)
|
||||
else:
|
||||
pass
|
||||
return user
|
||||
37
server/backend/pydentic.py
Normal file
37
server/backend/pydentic.py
Normal file
@@ -0,0 +1,37 @@
|
||||
from pydantic import BaseModel, Field, EmailStr, constr,validator
|
||||
from typing import List, Optional
|
||||
from enum import IntEnum
|
||||
#Валидация пароля
|
||||
import re
|
||||
def check_password_complexity(cls, password):
|
||||
if password is None:
|
||||
return password
|
||||
if not re.search(r'[A-Za-z]', password):
|
||||
raise ValueError('Password must contain at least one letter')
|
||||
if not re.search(r'\d', password):
|
||||
raise ValueError('Password must contain at least one digit')
|
||||
if not re.search(r'[!@#$%^&*(),.?":{}|<>]', password):
|
||||
raise ValueError('Password must contain at least one special symbol')
|
||||
return password
|
||||
|
||||
#Валидация полей с пользователями
|
||||
class UsersInfo(BaseModel):
|
||||
email:EmailStr = Field(..., min_length=6, max_length=254, description="email of the user")
|
||||
description: str = Field(..., description="description of the user")
|
||||
activated:bool = Field(..., description="Has the user activated their account")
|
||||
password:constr(min_length=8) = Field(..., description="Password with min 8 chars, letters and digits")
|
||||
@validator('password')
|
||||
def password_validator(cls, password):
|
||||
return check_password_complexity(cls, password)
|
||||
class IdofPersons(UsersInfo):
|
||||
id:int = Field(..., description="Unique identifier of the user")
|
||||
class CreateUser(UsersInfo):
|
||||
pass
|
||||
class UserUpdate(BaseModel):
|
||||
email:Optional[EmailStr] = Field(None, min_length=6, max_length=254, description="users' email")
|
||||
description:Optional[str] = Field(None, description="description of the user")
|
||||
activated:Optional[bool] = Field(None, description="Has the user activated their account")
|
||||
password:Optional[constr(min_length=8)] = Field(None, description="Password with min 8 chars, letters and digits")
|
||||
@validator('password')
|
||||
def password_validator(cls, password):
|
||||
return check_password_complexity(cls, password)
|
||||
Reference in New Issue
Block a user