rainycode 1.3.0__tar.gz → 1.4.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {rainycode-1.3.0 → rainycode-1.4.1}/PKG-INFO +1 -1
- rainycode-1.4.1/common_models/base_model.py +268 -0
- {rainycode-1.3.0 → rainycode-1.4.1}/common_utils/captcha_util.py +10 -1
- {rainycode-1.3.0 → rainycode-1.4.1}/core/databases/aiodb.py +27 -6
- {rainycode-1.3.0 → rainycode-1.4.1}/pyproject.toml +1 -1
- {rainycode-1.3.0 → rainycode-1.4.1}/rainycode.egg-info/PKG-INFO +1 -1
- rainycode-1.3.0/common_models/base_model.py +0 -209
- {rainycode-1.3.0 → rainycode-1.4.1}/common_base/aiorequests.py +0 -0
- {rainycode-1.3.0 → rainycode-1.4.1}/common_base/consts.py +0 -0
- {rainycode-1.3.0 → rainycode-1.4.1}/common_base/exception.py +0 -0
- {rainycode-1.3.0 → rainycode-1.4.1}/common_base/logging.py +0 -0
- {rainycode-1.3.0 → rainycode-1.4.1}/common_base/response.py +0 -0
- {rainycode-1.3.0 → rainycode-1.4.1}/common_depends/auth_depend.py +0 -0
- {rainycode-1.3.0 → rainycode-1.4.1}/common_models/__init__.py +0 -0
- {rainycode-1.3.0 → rainycode-1.4.1}/common_models/user_model.py +0 -0
- {rainycode-1.3.0 → rainycode-1.4.1}/common_models/wechat_model.py +0 -0
- {rainycode-1.3.0 → rainycode-1.4.1}/common_servers/api/auth_api.py +0 -0
- {rainycode-1.3.0 → rainycode-1.4.1}/common_servers/api/wechat_api.py +0 -0
- {rainycode-1.3.0 → rainycode-1.4.1}/common_servers/router.py +0 -0
- {rainycode-1.3.0 → rainycode-1.4.1}/common_servers/schemas/auth_schema.py +0 -0
- {rainycode-1.3.0 → rainycode-1.4.1}/common_servers/service/auth_svc.py +0 -0
- {rainycode-1.3.0 → rainycode-1.4.1}/common_servers/service/wechat_svc.py +0 -0
- {rainycode-1.3.0 → rainycode-1.4.1}/common_utils/bcrypt_util.py +0 -0
- {rainycode-1.3.0 → rainycode-1.4.1}/common_utils/ip_util.py +0 -0
- {rainycode-1.3.0 → rainycode-1.4.1}/common_utils/jwt_util.py +0 -0
- {rainycode-1.3.0 → rainycode-1.4.1}/common_utils/snowflake_util.py +0 -0
- {rainycode-1.3.0 → rainycode-1.4.1}/common_utils/wechat_util.py +0 -0
- {rainycode-1.3.0 → rainycode-1.4.1}/core/base_config.py +0 -0
- {rainycode-1.3.0 → rainycode-1.4.1}/core/databases/aioredis.py +0 -0
- {rainycode-1.3.0 → rainycode-1.4.1}/core/middleware/http_middleware.py +0 -0
- {rainycode-1.3.0 → rainycode-1.4.1}/core/start.py +0 -0
- {rainycode-1.3.0 → rainycode-1.4.1}/rainycode.egg-info/SOURCES.txt +0 -0
- {rainycode-1.3.0 → rainycode-1.4.1}/rainycode.egg-info/dependency_links.txt +0 -0
- {rainycode-1.3.0 → rainycode-1.4.1}/rainycode.egg-info/requires.txt +0 -0
- {rainycode-1.3.0 → rainycode-1.4.1}/rainycode.egg-info/top_level.txt +0 -0
- {rainycode-1.3.0 → rainycode-1.4.1}/setup.cfg +0 -0
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import uuid
|
|
2
|
+
from collections.abc import Iterable
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
from common_utils.snowflake_util import get_snowflake_id
|
|
6
|
+
from core.databases.aiodb import MySQLUtils
|
|
7
|
+
from tortoise import BaseDBAsyncClient, Model, fields
|
|
8
|
+
from tortoise.expressions import Q
|
|
9
|
+
from tortoise.queryset import BulkCreateQuery, QuerySet, QuerySetSingle
|
|
10
|
+
from typing_extensions import Self
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class BaseModel(MySQLUtils):
|
|
14
|
+
|
|
15
|
+
@classmethod
|
|
16
|
+
async def create(
|
|
17
|
+
cls,
|
|
18
|
+
using_db: BaseDBAsyncClient | None = None,
|
|
19
|
+
**kwargs: Any
|
|
20
|
+
) -> Self:
|
|
21
|
+
if not kwargs.get('id', 0) and 'id' in cls._meta.fields:
|
|
22
|
+
# uuid生成id
|
|
23
|
+
if kwargs.get('id_uuid', False):
|
|
24
|
+
kwargs['id'] = str(uuid.uuid4()).replace('-', '')
|
|
25
|
+
# 雪花算法生成id
|
|
26
|
+
if kwargs.get('id_snowflake', False):
|
|
27
|
+
kwargs['id'] = get_snowflake_id()
|
|
28
|
+
return await super().create(using_db=using_db, **kwargs)
|
|
29
|
+
|
|
30
|
+
@classmethod
|
|
31
|
+
def get(
|
|
32
|
+
cls,
|
|
33
|
+
*args: Q,
|
|
34
|
+
using_db: BaseDBAsyncClient | None = None,
|
|
35
|
+
**kwargs: Any
|
|
36
|
+
) -> QuerySetSingle[Self]:
|
|
37
|
+
if 'deleted' in cls._meta.fields:
|
|
38
|
+
deleted = kwargs.get('deleted')
|
|
39
|
+
if deleted is None:
|
|
40
|
+
# 默认只查未删除的记录
|
|
41
|
+
kwargs['deleted'] = 0
|
|
42
|
+
elif deleted == -1:
|
|
43
|
+
# -1 表示不过滤 deleted 字段,未删除(0)和已删除(1)的记录都可查
|
|
44
|
+
kwargs.pop('deleted', None)
|
|
45
|
+
# 其余值(0/1)原样透传
|
|
46
|
+
return super().get(*args, using_db=using_db, **kwargs)
|
|
47
|
+
|
|
48
|
+
@classmethod
|
|
49
|
+
def filter(
|
|
50
|
+
cls,
|
|
51
|
+
*args: Q,
|
|
52
|
+
**kwargs: Any
|
|
53
|
+
) -> QuerySet[Self]:
|
|
54
|
+
if 'deleted' in cls._meta.fields:
|
|
55
|
+
deleted = kwargs.get('deleted')
|
|
56
|
+
if deleted is None:
|
|
57
|
+
# 默认只查未删除的记录
|
|
58
|
+
kwargs['deleted'] = 0
|
|
59
|
+
elif deleted == -1:
|
|
60
|
+
# -1 表示不过滤 deleted 字段,未删除(0)和已删除(1)的记录都可查
|
|
61
|
+
kwargs.pop('deleted', None)
|
|
62
|
+
# 其余值(0/1)原样透传
|
|
63
|
+
return cls._meta.manager.get_queryset().filter(*args, **kwargs)
|
|
64
|
+
|
|
65
|
+
@classmethod
|
|
66
|
+
def bulk_create(
|
|
67
|
+
cls,
|
|
68
|
+
objects: Iterable[Self],
|
|
69
|
+
batch_size: int | None = None,
|
|
70
|
+
ignore_conflicts: bool = False,
|
|
71
|
+
update_fields: Iterable[str] | None = None,
|
|
72
|
+
on_conflict: Iterable[str] | None = None,
|
|
73
|
+
using_db: BaseDBAsyncClient | None = None,
|
|
74
|
+
**kwargs: Any
|
|
75
|
+
) -> "BulkCreateQuery[Self]":
|
|
76
|
+
if 'id' in cls._meta.fields:
|
|
77
|
+
for obj in objects:
|
|
78
|
+
if not getattr(obj, 'id', 0):
|
|
79
|
+
# uuid生成id
|
|
80
|
+
if kwargs.get('id_uuid', False):
|
|
81
|
+
obj.id = str(uuid.uuid4()).replace('-', '')
|
|
82
|
+
# 雪花算法生成id
|
|
83
|
+
if kwargs.get('id_snowflake', False):
|
|
84
|
+
obj.id = get_snowflake_id()
|
|
85
|
+
|
|
86
|
+
params = {
|
|
87
|
+
"objects": objects,
|
|
88
|
+
"batch_size": batch_size,
|
|
89
|
+
"ignore_conflicts": ignore_conflicts,
|
|
90
|
+
"update_fields": update_fields,
|
|
91
|
+
"on_conflict": on_conflict,
|
|
92
|
+
"using_db": using_db,
|
|
93
|
+
}
|
|
94
|
+
return super().bulk_create(**params)
|
|
95
|
+
|
|
96
|
+
async def save(
|
|
97
|
+
self,
|
|
98
|
+
using_db: BaseDBAsyncClient | None = None,
|
|
99
|
+
update_fields: Iterable[str] | None = None,
|
|
100
|
+
force_create: bool = False,
|
|
101
|
+
force_update: bool = False,
|
|
102
|
+
**kwargs: Any
|
|
103
|
+
) -> None:
|
|
104
|
+
if not getattr(self, 'id', 0) and kwargs.get('id_uuid', False) and 'id' in self._meta.fields:
|
|
105
|
+
setattr(self, 'id', str(uuid.uuid4()).replace('-', ''))
|
|
106
|
+
return await super().save(using_db=using_db, update_fields=update_fields, force_create=force_create,
|
|
107
|
+
force_update=force_update)
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
class TimeModel(BaseModel):
|
|
111
|
+
update_time = fields.DatetimeField(auto_now=True, description='更新时间')
|
|
112
|
+
create_time = fields.DatetimeField(auto_now_add=True, description='创建时间')
|
|
113
|
+
|
|
114
|
+
class Meta:
|
|
115
|
+
abstract = True
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
# class CreateModel(BaseModel):
|
|
119
|
+
# create_time = fields.DatetimeField(auto_now_add=True, description='创建时间')
|
|
120
|
+
# creator = fields.CharField(max_length=50, null=True, description="创建人")
|
|
121
|
+
|
|
122
|
+
# class Meta:
|
|
123
|
+
# abstract = True
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
# class UpdateModel(BaseModel):
|
|
127
|
+
# update_time = fields.DatetimeField(auto_now=True, description='更新时间')
|
|
128
|
+
# create_time = fields.DatetimeField(auto_now_add=True, description='创建时间')
|
|
129
|
+
# updater = fields.CharField(max_length=50, null=True, description="更新人")
|
|
130
|
+
# creator = fields.CharField(max_length=50, null=True, description="创建人")
|
|
131
|
+
|
|
132
|
+
# class Meta:
|
|
133
|
+
# abstract = True
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
# class DeleteModel(BaseModel):
|
|
137
|
+
# deleted = fields.IntField(default=0, description="是否已删除, 0:未删除, 1:已删除")
|
|
138
|
+
# create_time = fields.DatetimeField(auto_now_add=True, description="创建时间")
|
|
139
|
+
# update_time = fields.DatetimeField(auto_now=True, description="更新时间")
|
|
140
|
+
# creator = fields.CharField(max_length=50, null=True, description="创建人")
|
|
141
|
+
# updater = fields.CharField(max_length=50, null=True, description="更新人")
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
class CreateRelatedMeta(type(Model)):
|
|
145
|
+
"""元类:更新外键字段,解决用户related_name重复问题"""
|
|
146
|
+
def __new__(cls, name: str, bases: tuple, attrs: dict):
|
|
147
|
+
if name == 'CreateModel':
|
|
148
|
+
return super().__new__(cls, name, bases, attrs)
|
|
149
|
+
|
|
150
|
+
creator_related_name = f"{name.lower()}_creator"
|
|
151
|
+
attrs["creator"] = fields.ForeignKeyField(
|
|
152
|
+
'main.User',
|
|
153
|
+
null=True,
|
|
154
|
+
related_name=creator_related_name,
|
|
155
|
+
db_constraint=False,
|
|
156
|
+
description="创建人"
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
new_class = super().__new__(cls, name, bases, attrs)
|
|
160
|
+
return new_class
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
class CreateModel(BaseModel, metaclass=CreateRelatedMeta):
|
|
164
|
+
create_time = fields.DatetimeField(auto_now_add=True, description='创建时间')
|
|
165
|
+
creator = fields.ForeignKeyField(
|
|
166
|
+
'main.User',
|
|
167
|
+
null=True,
|
|
168
|
+
db_constraint=False,
|
|
169
|
+
description="创建人"
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
class Meta:
|
|
173
|
+
abstract = True
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
class UpdateRelatedMeta(type(Model)):
|
|
177
|
+
"""元类:更新外键字段,解决用户related_name重复问题"""
|
|
178
|
+
def __new__(cls, name: str, bases: tuple, attrs: dict):
|
|
179
|
+
if name == 'UpdateModel':
|
|
180
|
+
return super().__new__(cls, name, bases, attrs)
|
|
181
|
+
|
|
182
|
+
creator_related_name = f"{name.lower()}_creator"
|
|
183
|
+
attrs["creator"] = fields.ForeignKeyField(
|
|
184
|
+
'main.User',
|
|
185
|
+
null=True,
|
|
186
|
+
related_name=creator_related_name,
|
|
187
|
+
db_constraint=False,
|
|
188
|
+
description="创建人"
|
|
189
|
+
)
|
|
190
|
+
|
|
191
|
+
updater_related_name = f"{name.lower()}_updater"
|
|
192
|
+
attrs["updater"] = fields.ForeignKeyField(
|
|
193
|
+
'main.User',
|
|
194
|
+
null=True,
|
|
195
|
+
related_name=updater_related_name,
|
|
196
|
+
db_constraint=False,
|
|
197
|
+
description="更新人"
|
|
198
|
+
)
|
|
199
|
+
|
|
200
|
+
new_class = super().__new__(cls, name, bases, attrs)
|
|
201
|
+
return new_class
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
class UpdateModel(BaseModel, metaclass=UpdateRelatedMeta):
|
|
205
|
+
update_time = fields.DatetimeField(auto_now=True, description='更新时间')
|
|
206
|
+
updater = fields.ForeignKeyField(
|
|
207
|
+
'main.User',
|
|
208
|
+
null=True,
|
|
209
|
+
db_constraint=False,
|
|
210
|
+
description="更新人"
|
|
211
|
+
)
|
|
212
|
+
create_time = fields.DatetimeField(auto_now_add=True, description='创建时间')
|
|
213
|
+
creator = fields.ForeignKeyField(
|
|
214
|
+
'main.User',
|
|
215
|
+
null=True,
|
|
216
|
+
db_constraint=False,
|
|
217
|
+
description="创建人"
|
|
218
|
+
)
|
|
219
|
+
class Meta:
|
|
220
|
+
abstract = True
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
class DeleteRelatedMeta(type(Model)):
|
|
224
|
+
"""元类:更新外键字段,解决用户related_name重复问题"""
|
|
225
|
+
def __new__(cls, name: str, bases: tuple, attrs: dict):
|
|
226
|
+
if name == 'DeleteModel':
|
|
227
|
+
return super().__new__(cls, name, bases, attrs)
|
|
228
|
+
|
|
229
|
+
creator_related_name = f"{name.lower()}_creator"
|
|
230
|
+
attrs["creator"] = fields.ForeignKeyField(
|
|
231
|
+
'main.User',
|
|
232
|
+
null=True,
|
|
233
|
+
related_name=creator_related_name,
|
|
234
|
+
db_constraint=False,
|
|
235
|
+
description="创建人"
|
|
236
|
+
)
|
|
237
|
+
|
|
238
|
+
updater_related_name = f"{name.lower()}_updater"
|
|
239
|
+
attrs["updater"] = fields.ForeignKeyField(
|
|
240
|
+
'main.User',
|
|
241
|
+
null=True,
|
|
242
|
+
related_name=updater_related_name,
|
|
243
|
+
db_constraint=False,
|
|
244
|
+
description="更新人"
|
|
245
|
+
)
|
|
246
|
+
|
|
247
|
+
new_class = super().__new__(cls, name, bases, attrs)
|
|
248
|
+
return new_class
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
class DeleteModel(BaseModel, metaclass=DeleteRelatedMeta):
|
|
252
|
+
deleted = fields.IntField(default=0, description="是否已删除, 0:未删除, 1:已删除")
|
|
253
|
+
update_time = fields.DatetimeField(auto_now=True, description='更新时间')
|
|
254
|
+
updater = fields.ForeignKeyField(
|
|
255
|
+
'main.User',
|
|
256
|
+
null=True,
|
|
257
|
+
db_constraint=False,
|
|
258
|
+
description="更新人"
|
|
259
|
+
)
|
|
260
|
+
create_time = fields.DatetimeField(auto_now_add=True, description='创建时间')
|
|
261
|
+
creator = fields.ForeignKeyField(
|
|
262
|
+
'main.User',
|
|
263
|
+
null=True,
|
|
264
|
+
db_constraint=False,
|
|
265
|
+
description="创建人"
|
|
266
|
+
)
|
|
267
|
+
class Meta:
|
|
268
|
+
abstract = True
|
|
@@ -10,7 +10,16 @@ class CaptchaUtil:
|
|
|
10
10
|
"""
|
|
11
11
|
验证码工具类
|
|
12
12
|
"""
|
|
13
|
-
@classmethod
|
|
13
|
+
@classmethod
|
|
14
|
+
def generate_code(cls, length: int = 6) -> str:
|
|
15
|
+
"""
|
|
16
|
+
生成随机验证码(纯随机生成;存储/TTL/校验由调用方自行处理)
|
|
17
|
+
字符集去 0/O、1/I/L,避免误读
|
|
18
|
+
"""
|
|
19
|
+
alphabet = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"
|
|
20
|
+
return "".join(random.choice(alphabet) for _ in range(length))
|
|
21
|
+
|
|
22
|
+
@classmethod
|
|
14
23
|
def generate_captcha(cls) -> tuple[str, str]:
|
|
15
24
|
"""
|
|
16
25
|
生成带有噪声和干扰的验证码图片:4位随机字符
|
|
@@ -234,8 +234,15 @@ class MySQLUtils(Model):
|
|
|
234
234
|
queryset_model: QuerySetSingle | Model | None = None,
|
|
235
235
|
**kwargs
|
|
236
236
|
) -> dict[str, Any] | None:
|
|
237
|
-
if 'deleted' in cls._meta.fields
|
|
238
|
-
kwargs
|
|
237
|
+
if 'deleted' in cls._meta.fields:
|
|
238
|
+
deleted = kwargs.get('deleted')
|
|
239
|
+
if deleted is None:
|
|
240
|
+
# 默认只查未删除的记录
|
|
241
|
+
kwargs['deleted'] = 0
|
|
242
|
+
elif deleted == -1:
|
|
243
|
+
# -1 表示不过滤 deleted 字段,未删除(0)和已删除(1)的记录都可查
|
|
244
|
+
kwargs.pop('deleted', None)
|
|
245
|
+
# 其余值(0/1)原样透传
|
|
239
246
|
|
|
240
247
|
queryKwargs = {}
|
|
241
248
|
if fetch_related:
|
|
@@ -274,8 +281,15 @@ class MySQLUtils(Model):
|
|
|
274
281
|
queryset_model: QuerySetSingle | Model | None = None,
|
|
275
282
|
**kwargs
|
|
276
283
|
) -> dict[str, Any] | None:
|
|
277
|
-
if 'deleted' in cls._meta.fields
|
|
278
|
-
kwargs
|
|
284
|
+
if 'deleted' in cls._meta.fields:
|
|
285
|
+
deleted = kwargs.get('deleted')
|
|
286
|
+
if deleted is None:
|
|
287
|
+
# 默认只查未删除的记录
|
|
288
|
+
kwargs['deleted'] = 0
|
|
289
|
+
elif deleted == -1:
|
|
290
|
+
# -1 表示不过滤 deleted 字段,未删除(0)和已删除(1)的记录都可查
|
|
291
|
+
kwargs.pop('deleted', None)
|
|
292
|
+
# 其余值(0/1)原样透传
|
|
279
293
|
|
|
280
294
|
queryKwargs = {}
|
|
281
295
|
if fetch_related:
|
|
@@ -318,8 +332,15 @@ class MySQLUtils(Model):
|
|
|
318
332
|
order_by: list[str] | None = None,
|
|
319
333
|
**kwargs
|
|
320
334
|
) -> dict[str, Any] | list[dict[str, Any]]:
|
|
321
|
-
if 'deleted' in cls._meta.fields
|
|
322
|
-
kwargs
|
|
335
|
+
if 'deleted' in cls._meta.fields:
|
|
336
|
+
deleted = kwargs.get('deleted')
|
|
337
|
+
if deleted is None:
|
|
338
|
+
# 默认只查未删除的记录
|
|
339
|
+
kwargs['deleted'] = 0
|
|
340
|
+
elif deleted == -1:
|
|
341
|
+
# -1 表示不过滤 deleted 字段,未删除(0)和已删除(1)的记录都可查
|
|
342
|
+
kwargs.pop('deleted', None)
|
|
343
|
+
# 其余值(0/1)原样透传
|
|
323
344
|
|
|
324
345
|
queryKwargs = {}
|
|
325
346
|
if fetch_related:
|
|
@@ -1,209 +0,0 @@
|
|
|
1
|
-
import uuid
|
|
2
|
-
from collections.abc import Iterable
|
|
3
|
-
from typing import Any, Optional
|
|
4
|
-
|
|
5
|
-
from common_utils.snowflake_util import get_snowflake_id
|
|
6
|
-
from core.databases.aiodb import MySQLUtils
|
|
7
|
-
from tortoise import BaseDBAsyncClient, fields
|
|
8
|
-
from tortoise.expressions import Q
|
|
9
|
-
from tortoise.queryset import BulkCreateQuery, QuerySet, QuerySetSingle
|
|
10
|
-
from typing_extensions import Self
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
class BaseModel(MySQLUtils):
|
|
14
|
-
|
|
15
|
-
@classmethod
|
|
16
|
-
async def create(
|
|
17
|
-
cls,
|
|
18
|
-
using_db: BaseDBAsyncClient | None = None,
|
|
19
|
-
**kwargs: Any
|
|
20
|
-
) -> Self:
|
|
21
|
-
if not kwargs.get('id', 0) and 'id' in cls._meta.fields:
|
|
22
|
-
# uuid生成id
|
|
23
|
-
if kwargs.get('id_uuid', False):
|
|
24
|
-
kwargs['id'] = str(uuid.uuid4()).replace('-', '')
|
|
25
|
-
# 雪花算法生成id
|
|
26
|
-
if kwargs.get('id_snowflake', False):
|
|
27
|
-
kwargs['id'] = get_snowflake_id()
|
|
28
|
-
return await super().create(using_db=using_db, **kwargs)
|
|
29
|
-
|
|
30
|
-
@classmethod
|
|
31
|
-
def get(
|
|
32
|
-
cls,
|
|
33
|
-
*args: Q,
|
|
34
|
-
using_db: BaseDBAsyncClient | None = None,
|
|
35
|
-
**kwargs: Any
|
|
36
|
-
) -> QuerySetSingle[Self]:
|
|
37
|
-
if 'deleted' in cls._meta.fields and kwargs.get('deleted', None) is None:
|
|
38
|
-
kwargs['deleted'] = False
|
|
39
|
-
return super().get(*args, using_db=using_db, **kwargs)
|
|
40
|
-
|
|
41
|
-
@classmethod
|
|
42
|
-
def filter(
|
|
43
|
-
cls,
|
|
44
|
-
*args: Q,
|
|
45
|
-
**kwargs: Any
|
|
46
|
-
) -> QuerySet[Self]:
|
|
47
|
-
if 'deleted' in cls._meta.fields and kwargs.get('deleted', None) is None:
|
|
48
|
-
kwargs['deleted'] = False
|
|
49
|
-
return cls._meta.manager.get_queryset().filter(*args, **kwargs)
|
|
50
|
-
|
|
51
|
-
@classmethod
|
|
52
|
-
def bulk_create(
|
|
53
|
-
cls,
|
|
54
|
-
objects: Iterable[Self],
|
|
55
|
-
batch_size: int | None = None,
|
|
56
|
-
ignore_conflicts: bool = False,
|
|
57
|
-
update_fields: Iterable[str] | None = None,
|
|
58
|
-
on_conflict: Iterable[str] | None = None,
|
|
59
|
-
using_db: BaseDBAsyncClient | None = None,
|
|
60
|
-
**kwargs: Any
|
|
61
|
-
) -> "BulkCreateQuery[Self]":
|
|
62
|
-
if 'id' in cls._meta.fields:
|
|
63
|
-
for obj in objects:
|
|
64
|
-
if not getattr(obj, 'id', 0):
|
|
65
|
-
# uuid生成id
|
|
66
|
-
if kwargs.get('id_uuid', False):
|
|
67
|
-
obj.id = str(uuid.uuid4()).replace('-', '')
|
|
68
|
-
# 雪花算法生成id
|
|
69
|
-
if kwargs.get('id_snowflake', False):
|
|
70
|
-
obj.id = get_snowflake_id()
|
|
71
|
-
|
|
72
|
-
params = {
|
|
73
|
-
"objects": objects,
|
|
74
|
-
"batch_size": batch_size,
|
|
75
|
-
"ignore_conflicts": ignore_conflicts,
|
|
76
|
-
"update_fields": update_fields,
|
|
77
|
-
"on_conflict": on_conflict,
|
|
78
|
-
"using_db": using_db,
|
|
79
|
-
}
|
|
80
|
-
return super().bulk_create(**params)
|
|
81
|
-
|
|
82
|
-
async def save(
|
|
83
|
-
self,
|
|
84
|
-
using_db: BaseDBAsyncClient | None = None,
|
|
85
|
-
update_fields: Iterable[str] | None = None,
|
|
86
|
-
force_create: bool = False,
|
|
87
|
-
force_update: bool = False,
|
|
88
|
-
**kwargs: Any
|
|
89
|
-
) -> None:
|
|
90
|
-
if not getattr(self, 'id', 0) and kwargs.get('id_uuid', False) and 'id' in self._meta.fields:
|
|
91
|
-
setattr(self, 'id', str(uuid.uuid4()).replace('-', ''))
|
|
92
|
-
return await super().save(using_db=using_db, update_fields=update_fields, force_create=force_create,
|
|
93
|
-
force_update=force_update)
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
class TimeModel(BaseModel):
|
|
97
|
-
create_time = fields.DatetimeField(auto_now_add=True, description='创建时间')
|
|
98
|
-
update_time = fields.DatetimeField(auto_now=True, description='更新时间')
|
|
99
|
-
|
|
100
|
-
class Meta:
|
|
101
|
-
abstract = True
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
class CreateModel(BaseModel):
|
|
105
|
-
create_time = fields.DatetimeField(auto_now_add=True, description='创建时间')
|
|
106
|
-
creator = fields.CharField(max_length=50, null=True, description="创建人")
|
|
107
|
-
|
|
108
|
-
class Meta:
|
|
109
|
-
abstract = True
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
class UpdateModel(BaseModel):
|
|
113
|
-
create_time = fields.DatetimeField(auto_now_add=True, description='创建时间')
|
|
114
|
-
update_time = fields.DatetimeField(auto_now=True, description='更新时间')
|
|
115
|
-
creator = fields.CharField(max_length=50, null=True, description="创建人")
|
|
116
|
-
updater = fields.CharField(max_length=50, null=True, description="更新人")
|
|
117
|
-
|
|
118
|
-
class Meta:
|
|
119
|
-
abstract = True
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
class DeleteModel(BaseModel):
|
|
123
|
-
deleted = fields.BooleanField(default=False, description="是否已删除")
|
|
124
|
-
create_time = fields.DatetimeField(auto_now_add=True, description="创建时间")
|
|
125
|
-
update_time = fields.DatetimeField(auto_now=True, description="更新时间")
|
|
126
|
-
creator = fields.CharField(max_length=50, null=True, description="创建人")
|
|
127
|
-
updater = fields.CharField(max_length=50, null=True, description="更新人")
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
# class CreateRelatedMeta(type(Model)):
|
|
131
|
-
# """元类:更新外键字段,解决用户related_name重复问题"""
|
|
132
|
-
# def __new__(cls, name: str, bases: tuple, attrs: dict):
|
|
133
|
-
# if name == 'UpdateModel':
|
|
134
|
-
# return super().__new__(cls, name, bases, attrs)
|
|
135
|
-
|
|
136
|
-
# creator_related_name = f"{name.lower()}_creator"
|
|
137
|
-
# attrs["creator"] = fields.ForeignKeyField(
|
|
138
|
-
# 'main.SystemUserModel',
|
|
139
|
-
# null=True,
|
|
140
|
-
# related_name=creator_related_name,
|
|
141
|
-
# db_constraint=False,
|
|
142
|
-
# description="创建人"
|
|
143
|
-
# )
|
|
144
|
-
|
|
145
|
-
# new_class = super().__new__(cls, name, bases, attrs)
|
|
146
|
-
# return new_class
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
# class CreateModel(BaseModel, metaclass=CreateRelatedMeta):
|
|
150
|
-
# deleted = fields.BooleanField(default=False, description='是否已删除')
|
|
151
|
-
# create_time = fields.DatetimeField(auto_now_add=True, description='创建时间')
|
|
152
|
-
# creator = fields.ForeignKeyField(
|
|
153
|
-
# 'main.SystemUserModel',
|
|
154
|
-
# null=True,
|
|
155
|
-
# db_constraint=False,
|
|
156
|
-
# description="创建人"
|
|
157
|
-
# )
|
|
158
|
-
|
|
159
|
-
# class Meta:
|
|
160
|
-
# abstract = True
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
# class UpdateRelatedMeta(type(Model)):
|
|
164
|
-
# """元类:更新外键字段,解决用户related_name重复问题"""
|
|
165
|
-
# def __new__(cls, name: str, bases: tuple, attrs: dict):
|
|
166
|
-
# if name == 'UpdateModel':
|
|
167
|
-
# return super().__new__(cls, name, bases, attrs)
|
|
168
|
-
|
|
169
|
-
# creator_related_name = f"{name.lower()}_creator"
|
|
170
|
-
# attrs["creator"] = fields.ForeignKeyField(
|
|
171
|
-
# 'main.SystemUserModel',
|
|
172
|
-
# null=True,
|
|
173
|
-
# related_name=creator_related_name,
|
|
174
|
-
# db_constraint=False,
|
|
175
|
-
# description="创建人"
|
|
176
|
-
# )
|
|
177
|
-
|
|
178
|
-
# updater_related_name = f"{name.lower()}_updater"
|
|
179
|
-
# attrs["updater"] = fields.ForeignKeyField(
|
|
180
|
-
# 'main.SystemUserModel',
|
|
181
|
-
# null=True,
|
|
182
|
-
# related_name=updater_related_name,
|
|
183
|
-
# db_constraint=False,
|
|
184
|
-
# description="更新人"
|
|
185
|
-
# )
|
|
186
|
-
|
|
187
|
-
# new_class = super().__new__(cls, name, bases, attrs)
|
|
188
|
-
# return new_class
|
|
189
|
-
|
|
190
|
-
# class UpdateModel(BaseModel, metaclass=UpdateRelatedMeta):
|
|
191
|
-
# deleted = fields.BooleanField(default=False, description='是否已删除')
|
|
192
|
-
# create_time = fields.DatetimeField(auto_now_add=True, description='创建时间')
|
|
193
|
-
# update_time = fields.DatetimeField(auto_now=True, description='更新时间')
|
|
194
|
-
# creator = fields.ForeignKeyField(
|
|
195
|
-
# 'main.SystemUserModel',
|
|
196
|
-
# null=True,
|
|
197
|
-
# db_constraint=False,
|
|
198
|
-
# description="创建人"
|
|
199
|
-
# )
|
|
200
|
-
|
|
201
|
-
# updater = fields.ForeignKeyField(
|
|
202
|
-
# 'main.SystemUserModel',
|
|
203
|
-
# null=True,
|
|
204
|
-
# db_constraint=False,
|
|
205
|
-
# description="更新人"
|
|
206
|
-
# )
|
|
207
|
-
|
|
208
|
-
# class Meta:
|
|
209
|
-
# abstract = True
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|