gitscope
https://dev.kronoz.win/r/marzneshin.git
app/models/user.py · nightly
Source
1 12b255b Dawsh 2y ago import secrets
2 a47eb4c Dawsh 2y ago from datetime import datetime
3 e19fade Erfan 1y ago from enum import StrEnum
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 e19fade Erfan 1y ago class UserStatus(StrEnum):
18 d377c4c Dawsh 2y ago ACTIVE = "active"
19 d377c4c Dawsh 2y ago INACTIVE = "inactive"
20 a47eb4c Dawsh 2y ago
21 12b255b Dawsh 2y ago
22 e19fade Erfan 1y ago class UserDataUsageResetStrategy(StrEnum):
23 a47eb4c Dawsh 2y ago no_reset = "no_reset"
24 a47eb4c Dawsh 2y ago day = "day"
25 a47eb4c Dawsh 2y ago week = "week"
26 a47eb4c Dawsh 2y ago month = "month"
27 a47eb4c Dawsh 2y ago year = "year"
28 a47eb4c Dawsh 2y ago
29 a47eb4c Dawsh 2y ago
30 e19fade Erfan 1y ago class UserExpireStrategy(StrEnum):
31 d377c4c Dawsh 2y ago NEVER = "never"
32 d377c4c Dawsh 2y ago FIXED_DATE = "fixed_date"
33 d377c4c Dawsh 2y ago START_ON_FIRST_USE = "start_on_first_use"
34 d377c4c Dawsh 2y ago
35 d377c4c Dawsh 2y ago
36 db68a58 Dawsh 2y ago class User(BaseModel):
37 db68a58 Dawsh 2y ago id: int | None = None
38 f1ab16c Dawsh 2y ago username: Annotated[
39 f1ab16c Dawsh 2y ago str, StringConstraints(to_lower=True, pattern=USERNAME_REGEXP)
40 f1ab16c Dawsh 2y ago ]
41 d377c4c Dawsh 2y ago expire_strategy: UserExpireStrategy
42 d377c4c Dawsh 2y ago expire_date: datetime | None = Field(None)
43 ae5301f Erfan 1y ago usage_duration: (
44 ae5301f Erfan 1y ago Annotated[
45 ae5301f Erfan 1y ago int,
46 ae5301f Erfan 1y ago Field(
47 ae5301f Erfan 1y ago le=9999 * 365 * 24 * 60 * 60,
48 ae5301f Erfan 1y ago description="usage_duration must be less than 9999 years",
49 ae5301f Erfan 1y ago ),
50 ae5301f Erfan 1y ago ]
51 ae5301f Erfan 1y ago | None
52 ae5301f Erfan 1y ago ) = (None,)
53 d377c4c Dawsh 2y ago activation_deadline: datetime | None = Field(None)
54 a47eb4c Dawsh 2y ago key: str = Field(default_factory=lambda: secrets.token_hex(16))
55 db68a58 Dawsh 2y ago data_limit: int | None = Field(
56 a47eb4c Dawsh 2y ago ge=0, default=None, description="data_limit can be 0 or greater"
57 a47eb4c Dawsh 2y ago )
58 d377c4c Dawsh 2y ago data_limit_reset_strategy: UserDataUsageResetStrategy = (
59 d377c4c Dawsh 2y ago UserDataUsageResetStrategy.no_reset
60 a47eb4c Dawsh 2y ago )
61 db68a58 Dawsh 2y ago note: Annotated[str, Field(max_length=500)] | None = None
62 db68a58 Dawsh 2y ago sub_updated_at: datetime | None = Field(None)
63 db68a58 Dawsh 2y ago sub_last_user_agent: str | None = Field(None)
64 db68a58 Dawsh 2y ago online_at: datetime | None = Field(None)
65 a47eb4c Dawsh 2y ago
66 a47eb4c Dawsh 2y ago model_config = ConfigDict(from_attributes=True)
67 a47eb4c Dawsh 2y ago
68 4442e87 mahdim10 2y ago @model_validator(mode="after")
69 4442e87 mahdim10 2y ago def validate_expiry(self):
70 4442e87 mahdim10 2y ago if self.expire_strategy == UserExpireStrategy.START_ON_FIRST_USE:
71 4442e87 mahdim10 2y ago if not self.usage_duration:
72 4442e87 mahdim10 2y ago raise ValueError(
73 4442e87 mahdim10 2y ago "User expire_strategy cannot be start_on_first_use without a valid usage_duration."
74 4442e87 mahdim10 2y ago )
75 4442e87 mahdim10 2y ago
76 4442e87 mahdim10 2y ago # Set expire_date to None if expire_strategy is START_ON_FIRST_USE
77 4442e87 mahdim10 2y ago self.expire_date = None
78 4442e87 mahdim10 2y ago
79 4442e87 mahdim10 2y ago elif self.expire_strategy == UserExpireStrategy.FIXED_DATE:
80 4442e87 mahdim10 2y ago if not self.expire_date:
81 4442e87 mahdim10 2y ago raise ValueError(
82 4442e87 mahdim10 2y ago "User expire_strategy cannot be fixed_date without a valid expire date."
83 4442e87 mahdim10 2y ago )
84 4442e87 mahdim10 2y ago
85 4442e87 mahdim10 2y ago # Set usage_duration and activation_deadline to None if expire_strategy is START_ON_FIRST_USE
86 4442e87 mahdim10 2y ago self.usage_duration = None
87 4442e87 mahdim10 2y ago self.activation_deadline = None
88 4442e87 mahdim10 2y ago
89 4442e87 mahdim10 2y ago elif self.expire_strategy == UserExpireStrategy.NEVER:
90 4442e87 mahdim10 2y ago # Set expire_date, usage_duration and activation_deadline to None if expire_strategy is NEVER
91 4442e87 mahdim10 2y ago self.expire_date = None
92 4442e87 mahdim10 2y ago self.usage_duration = None
93 4442e87 mahdim10 2y ago self.activation_deadline = None
94 4442e87 mahdim10 2y ago
95 4442e87 mahdim10 2y ago return self
96 4442e87 mahdim10 2y ago
97 4d647dd Dawsh 2y ago
98 a47eb4c Dawsh 2y ago class UserCreate(User):
99 db68a58 Dawsh 2y ago service_ids: list[int] = Field([])
100 01cdf2e Dawsh 2y ago model_config = ConfigDict(
101 01cdf2e Dawsh 2y ago json_schema_extra={
102 01cdf2e Dawsh 2y ago "example": {
103 01cdf2e Dawsh 2y ago "username": "user1234",
104 a44fd3b mahdim10 2y ago "service_ids": [1, 2, 3],
105 d377c4c Dawsh 2y ago "expire_strategy": "start_on_first_use",
106 d377c4c Dawsh 2y ago "usage_duration": 86400 * 14,
107 d377c4c Dawsh 2y ago "activation_deadline": "2024-11-03T20:30:00",
108 01cdf2e Dawsh 2y ago "data_limit": 0,
109 01cdf2e Dawsh 2y ago "data_limit_reset_strategy": "no_reset",
110 01cdf2e Dawsh 2y ago "note": "",
111 01cdf2e Dawsh 2y ago }
112 a47eb4c Dawsh 2y ago }
113 01cdf2e Dawsh 2y ago )
114 a47eb4c Dawsh 2y ago
115 a47eb4c Dawsh 2y ago
116 d377c4c Dawsh 2y ago class UserModify(UserCreate):
117 db68a58 Dawsh 2y ago service_ids: list[int] | None = Field(None)
118 d377c4c Dawsh 2y ago data_limit_reset_strategy: UserDataUsageResetStrategy | None = Field(None)
119 4b0123b Erfan 1y ago expire_strategy: UserExpireStrategy | None = Field(None)
120 01cdf2e Dawsh 2y ago model_config = ConfigDict(
121 01cdf2e Dawsh 2y ago json_schema_extra={
122 01cdf2e Dawsh 2y ago "example": {
123 01cdf2e Dawsh 2y ago "username": "mammad1234",
124 d377c4c Dawsh 2y ago "service_ids": [1, 2, 3],
125 d377c4c Dawsh 2y ago "expire_strategy": "fixed_date",
126 d377c4c Dawsh 2y ago "expire_date": "2023-11-03T20:30:00",
127 01cdf2e Dawsh 2y ago "data_limit": 0,
128 01cdf2e Dawsh 2y ago "data_limit_reset_strategy": "no_reset",
129 01cdf2e Dawsh 2y ago "note": "",
130 01cdf2e Dawsh 2y ago }
131 a47eb4c Dawsh 2y ago }
132 01cdf2e Dawsh 2y ago )
133 a47eb4c Dawsh 2y ago
134 a47eb4c Dawsh 2y ago
135 a47eb4c Dawsh 2y ago class UserResponse(User):
136 a47eb4c Dawsh 2y ago id: int
137 d377c4c Dawsh 2y ago activated: bool
138 d377c4c Dawsh 2y ago is_active: bool
139 d377c4c Dawsh 2y ago expired: bool
140 d377c4c Dawsh 2y ago data_limit_reached: bool
141 d377c4c Dawsh 2y ago enabled: bool
142 a47eb4c Dawsh 2y ago used_traffic: int
143 d377c4c Dawsh 2y ago lifetime_used_traffic: int
144 00d6be5 Erfan 2y ago sub_revoked_at: datetime | None
145 a47eb4c Dawsh 2y ago created_at: datetime
146 db68a58 Dawsh 2y ago service_ids: list[int]
147 4708fcb Dawsh 2y ago subscription_url: str
148 b4ec790 Dawsh 2y ago owner_username: str | None
149 c778675 Mardin 2y ago traffic_reset_at: datetime | None
150 9bfa731 Dawsh 2y ago
151 a47eb4c Dawsh 2y ago model_config = ConfigDict(from_attributes=True)
152 9bfa731 Dawsh 2y ago
153 a47eb4c Dawsh 2y ago
154 e6e6467 Dawsh 2y ago class UserNodeUsageSeries(BaseModel):
155 d377c4c Dawsh 2y ago node_id: int | None = None
156 a47eb4c Dawsh 2y ago node_name: str
157 e6e6467 Dawsh 2y ago usages: list[tuple[int, int]] # list[UserUsage]
158 a47eb4c Dawsh 2y ago
159 a47eb4c Dawsh 2y ago
160 e6e6467 Dawsh 2y ago class UserUsageSeriesResponse(BaseModel):
161 a47eb4c Dawsh 2y ago username: str
162 e6e6467 Dawsh 2y ago node_usages: list[UserNodeUsageSeries]
163 b15de9e Erfan 2y ago total: int