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