gitscope
https://dev.kronoz.win/r/marzneshin.git
app/models/user.py · v0.5.5
Source
1 12b255b Dawsh 2y ago import secrets
2 a47eb4c Dawsh 2y ago from datetime import datetime
3 a47eb4c Dawsh 2y ago from enum import Enum
4 d377c4c Dawsh 2y ago from typing import Annotated
5 a47eb4c Dawsh 2y ago
6 01cdf2e Dawsh 2y ago from pydantic import (
7 01cdf2e Dawsh 2y ago ConfigDict,
8 01cdf2e Dawsh 2y ago BaseModel,
9 01cdf2e Dawsh 2y ago Field,
10 01cdf2e Dawsh 2y ago StringConstraints,
11 857e289 Mrdn 2y ago model_validator,
12 01cdf2e Dawsh 2y ago )
13 a47eb4c Dawsh 2y ago
14 f446990 Dawsh 2y ago USERNAME_REGEXP = r"^\w{3,32}$"
15 a47eb4c Dawsh 2y ago
16 a47eb4c Dawsh 2y ago
17 a47eb4c Dawsh 2y ago class ReminderType(str, Enum):
18 a47eb4c Dawsh 2y ago expiration_date = "expiration_date"
19 a47eb4c Dawsh 2y ago data_usage = "data_usage"
20 a47eb4c Dawsh 2y ago
21 a47eb4c Dawsh 2y ago
22 a47eb4c Dawsh 2y ago class UserStatus(str, Enum):
23 d377c4c Dawsh 2y ago ACTIVE = "active"
24 d377c4c Dawsh 2y ago INACTIVE = "inactive"
25 a47eb4c Dawsh 2y ago
26 12b255b Dawsh 2y ago
27 d377c4c Dawsh 2y ago class UserDataUsageResetStrategy(str, Enum):
28 a47eb4c Dawsh 2y ago no_reset = "no_reset"
29 a47eb4c Dawsh 2y ago day = "day"
30 a47eb4c Dawsh 2y ago week = "week"
31 a47eb4c Dawsh 2y ago month = "month"
32 a47eb4c Dawsh 2y ago year = "year"
33 a47eb4c Dawsh 2y ago
34 a47eb4c Dawsh 2y ago
35 d377c4c Dawsh 2y ago class UserExpireStrategy(str, Enum):
36 d377c4c Dawsh 2y ago NEVER = "never"
37 d377c4c Dawsh 2y ago FIXED_DATE = "fixed_date"
38 d377c4c Dawsh 2y ago START_ON_FIRST_USE = "start_on_first_use"
39 d377c4c Dawsh 2y ago
40 d377c4c Dawsh 2y ago
41 db68a58 Dawsh 2y ago class User(BaseModel):
42 db68a58 Dawsh 2y ago id: int | None = None
43 f1ab16c Dawsh 2y ago username: Annotated[
44 f1ab16c Dawsh 2y ago str, StringConstraints(to_lower=True, pattern=USERNAME_REGEXP)
45 f1ab16c Dawsh 2y ago ]
46 d377c4c Dawsh 2y ago expire_strategy: UserExpireStrategy
47 d377c4c Dawsh 2y ago expire_date: datetime | None = Field(None)
48 d377c4c Dawsh 2y ago usage_duration: int | None = Field(None)
49 d377c4c Dawsh 2y ago activation_deadline: datetime | None = Field(None)
50 a47eb4c Dawsh 2y ago key: str = Field(default_factory=lambda: secrets.token_hex(16))
51 db68a58 Dawsh 2y ago data_limit: int | None = Field(
52 a47eb4c Dawsh 2y ago ge=0, default=None, description="data_limit can be 0 or greater"
53 a47eb4c Dawsh 2y ago )
54 d377c4c Dawsh 2y ago data_limit_reset_strategy: UserDataUsageResetStrategy = (
55 d377c4c Dawsh 2y ago UserDataUsageResetStrategy.no_reset
56 a47eb4c Dawsh 2y ago )
57 db68a58 Dawsh 2y ago note: Annotated[str, Field(max_length=500)] | None = None
58 db68a58 Dawsh 2y ago sub_updated_at: datetime | None = Field(None)
59 db68a58 Dawsh 2y ago sub_last_user_agent: str | None = Field(None)
60 db68a58 Dawsh 2y ago online_at: datetime | None = Field(None)
61 a47eb4c Dawsh 2y ago
62 a47eb4c Dawsh 2y ago model_config = ConfigDict(from_attributes=True)
63 a47eb4c Dawsh 2y ago
64 4d647dd Dawsh 2y ago
65 a47eb4c Dawsh 2y ago class UserCreate(User):
66 db68a58 Dawsh 2y ago service_ids: list[int] = Field([])
67 01cdf2e Dawsh 2y ago model_config = ConfigDict(
68 01cdf2e Dawsh 2y ago json_schema_extra={
69 01cdf2e Dawsh 2y ago "example": {
70 01cdf2e Dawsh 2y ago "username": "user1234",
71 01cdf2e Dawsh 2y ago "services": [1, 2, 3],
72 d377c4c Dawsh 2y ago "expire_strategy": "start_on_first_use",
73 d377c4c Dawsh 2y ago "usage_duration": 86400 * 14,
74 d377c4c Dawsh 2y ago "activation_deadline": "2024-11-03T20:30:00",
75 01cdf2e Dawsh 2y ago "data_limit": 0,
76 01cdf2e Dawsh 2y ago "data_limit_reset_strategy": "no_reset",
77 01cdf2e Dawsh 2y ago "note": "",
78 01cdf2e Dawsh 2y ago }
79 a47eb4c Dawsh 2y ago }
80 01cdf2e Dawsh 2y ago )
81 a47eb4c Dawsh 2y ago
82 857e289 Mrdn 2y ago @model_validator(mode="after")
83 857e289 Mrdn 2y ago def validate_expiry(self):
84 d377c4c Dawsh 2y ago if self.expire_strategy == UserExpireStrategy.START_ON_FIRST_USE:
85 d377c4c Dawsh 2y ago if not self.usage_duration:
86 01cdf2e Dawsh 2y ago raise ValueError(
87 d377c4c Dawsh 2y ago "User expire_strategy can not be start_on_first_use without a valid usage_duration."
88 01cdf2e Dawsh 2y ago )
89 d377c4c Dawsh 2y ago elif (
90 d377c4c Dawsh 2y ago self.expire_strategy == UserExpireStrategy.FIXED_DATE
91 d377c4c Dawsh 2y ago and not self.expire_date
92 5c78ce8 Dawsh 2y ago ):
93 f1ab16c Dawsh 2y ago raise ValueError(
94 d377c4c Dawsh 2y ago "User expire_strategy can not be fixed_date without a valid expire date."
95 f1ab16c Dawsh 2y ago )
96 857e289 Mrdn 2y ago return self
97 857e289 Mrdn 2y ago
98 a47eb4c Dawsh 2y ago
99 d377c4c Dawsh 2y ago class UserModify(UserCreate):
100 db68a58 Dawsh 2y ago service_ids: list[int] | None = Field(None)
101 d377c4c Dawsh 2y ago data_limit_reset_strategy: UserDataUsageResetStrategy | None = Field(None)
102 01cdf2e Dawsh 2y ago model_config = ConfigDict(
103 01cdf2e Dawsh 2y ago json_schema_extra={
104 01cdf2e Dawsh 2y ago "example": {
105 01cdf2e Dawsh 2y ago "username": "mammad1234",
106 d377c4c Dawsh 2y ago "service_ids": [1, 2, 3],
107 d377c4c Dawsh 2y ago "expire_strategy": "fixed_date",
108 d377c4c Dawsh 2y ago "expire_date": "2023-11-03T20:30:00",
109 01cdf2e Dawsh 2y ago "data_limit": 0,
110 01cdf2e Dawsh 2y ago "data_limit_reset_strategy": "no_reset",
111 01cdf2e Dawsh 2y ago "note": "",
112 01cdf2e Dawsh 2y ago }
113 a47eb4c Dawsh 2y ago }
114 01cdf2e Dawsh 2y ago )
115 a47eb4c Dawsh 2y ago
116 a47eb4c Dawsh 2y ago
117 a47eb4c Dawsh 2y ago class UserResponse(User):
118 a47eb4c Dawsh 2y ago id: int
119 d377c4c Dawsh 2y ago activated: bool
120 d377c4c Dawsh 2y ago is_active: bool
121 d377c4c Dawsh 2y ago expired: bool
122 d377c4c Dawsh 2y ago data_limit_reached: bool
123 d377c4c Dawsh 2y ago enabled: bool
124 a47eb4c Dawsh 2y ago used_traffic: int
125 d377c4c Dawsh 2y ago lifetime_used_traffic: int
126 a47eb4c Dawsh 2y ago created_at: datetime
127 db68a58 Dawsh 2y ago service_ids: list[int]
128 4708fcb Dawsh 2y ago subscription_url: str
129 d0daa61 Mardin 2y ago owner_username: str
130 c778675 Mardin 2y ago traffic_reset_at: datetime | None
131 9bfa731 Dawsh 2y ago
132 a47eb4c Dawsh 2y ago model_config = ConfigDict(from_attributes=True)
133 9bfa731 Dawsh 2y ago
134 a47eb4c Dawsh 2y ago
135 a47eb4c Dawsh 2y ago class UserUsageResponse(BaseModel):
136 d377c4c Dawsh 2y ago node_id: int | None = None
137 a47eb4c Dawsh 2y ago node_name: str
138 a47eb4c Dawsh 2y ago used_traffic: int
139 a47eb4c Dawsh 2y ago
140 a47eb4c Dawsh 2y ago
141 a47eb4c Dawsh 2y ago class UserUsagesResponse(BaseModel):
142 a47eb4c Dawsh 2y ago username: str
143 d377c4c Dawsh 2y ago usages: list[UserUsageResponse]