gitscope
https://dev.kronoz.win/r/marzneshin.git
app/models/admin.py · v0.3.2
Source
1 a47eb4c Dawsh 2y ago from typing import Optional
2 a47eb4c Dawsh 2y ago
3 a47eb4c Dawsh 2y ago from fastapi.security import OAuth2PasswordBearer
4 a47eb4c Dawsh 2y ago from passlib.context import CryptContext
5 a47eb4c Dawsh 2y ago from pydantic import ConfigDict, BaseModel
6 a47eb4c Dawsh 2y ago
7 a47eb4c Dawsh 2y ago pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
8 f1ab16c Dawsh 2y ago oauth2_scheme = OAuth2PasswordBearer(
9 f1ab16c Dawsh 2y ago tokenUrl="/api/admins/token"
10 f1ab16c Dawsh 2y ago ) # Admin view url
11 a47eb4c Dawsh 2y ago
12 a47eb4c Dawsh 2y ago
13 a47eb4c Dawsh 2y ago class Token(BaseModel):
14 a47eb4c Dawsh 2y ago access_token: str
15 9ddfbc8 Mardin 2y ago is_sudo: bool
16 a47eb4c Dawsh 2y ago token_type: str = "bearer"
17 a47eb4c Dawsh 2y ago
18 a47eb4c Dawsh 2y ago
19 a47eb4c Dawsh 2y ago class Admin(BaseModel):
20 9ddfbc8 Mardin 2y ago id: int | None = None
21 a47eb4c Dawsh 2y ago username: str
22 a47eb4c Dawsh 2y ago is_sudo: bool
23 a47eb4c Dawsh 2y ago model_config = ConfigDict(from_attributes=True)
24 a47eb4c Dawsh 2y ago
25 a47eb4c Dawsh 2y ago
26 a47eb4c Dawsh 2y ago class AdminCreate(Admin):
27 a47eb4c Dawsh 2y ago password: str
28 a47eb4c Dawsh 2y ago
29 a47eb4c Dawsh 2y ago @property
30 a47eb4c Dawsh 2y ago def hashed_password(self):
31 a47eb4c Dawsh 2y ago return pwd_context.hash(self.password)
32 a47eb4c Dawsh 2y ago
33 a47eb4c Dawsh 2y ago
34 a47eb4c Dawsh 2y ago class AdminModify(BaseModel):
35 a47eb4c Dawsh 2y ago password: str
36 a47eb4c Dawsh 2y ago is_sudo: bool
37 a47eb4c Dawsh 2y ago
38 a47eb4c Dawsh 2y ago @property
39 a47eb4c Dawsh 2y ago def hashed_password(self):
40 a47eb4c Dawsh 2y ago return pwd_context.hash(self.password)
41 a47eb4c Dawsh 2y ago
42 a47eb4c Dawsh 2y ago
43 a47eb4c Dawsh 2y ago class AdminPartialModify(AdminModify):
44 f1ab16c Dawsh 2y ago __annotations__ = {
45 f1ab16c Dawsh 2y ago k: Optional[v] for k, v in AdminModify.__annotations__.items()
46 f1ab16c Dawsh 2y ago }
47 a47eb4c Dawsh 2y ago
48 a47eb4c Dawsh 2y ago
49 a47eb4c Dawsh 2y ago class AdminInDB(Admin):
50 a47eb4c Dawsh 2y ago username: str
51 a47eb4c Dawsh 2y ago hashed_password: str
52 a47eb4c Dawsh 2y ago
53 a47eb4c Dawsh 2y ago def verify_password(self, plain_password):
54 a47eb4c Dawsh 2y ago return pwd_context.verify(plain_password, self.hashed_password)