internal 1.1.35.2__py3-none-any.whl → 1.1.35.4__py3-none-any.whl

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.

Potentially problematic release.


This version of internal might be problematic. Click here for more details.

@@ -1,4 +1,5 @@
1
1
  from datetime import datetime
2
+ from idlelib import query
2
3
 
3
4
  from typing import List, Tuple, Union, Dict, Type, Optional, Any, Literal, Union, Set, Mapping
4
5
  import typing_extensions
@@ -28,7 +29,7 @@ class InternalBaseDocument(Document):
28
29
  async def get_pagination_list(cls, app: FastAPI, query: list = None, sort: List[Tuple] = None,
29
30
  page_size: int = DEF_PAGE_SIZE, page_no: int = DEF_PAGE_NO,
30
31
  ignore_cache: bool = False, fetch_links: bool = False,
31
- exclude_fields: List[str] = None):
32
+ exclude_field_list: List[str] = None):
32
33
  if not query:
33
34
  final_query = []
34
35
  else:
@@ -48,26 +49,55 @@ class InternalBaseDocument(Document):
48
49
  continue
49
50
  final_sort.append((cls.id, pymongo.ASCENDING))
50
51
 
51
- total_num = await cls.find(*final_query, ignore_cache=ignore_cache, fetch_links=fetch_links).sort(
52
- *final_sort).count()
53
-
54
- total_pages = (total_num + page_size - 1) // page_size
55
-
56
- if total_pages == 0:
57
- page_no = 1
58
- page_data = []
52
+ if exclude_field_list:
53
+ # 當需要排除欄位時,使用 Motor 直接操作
54
+ collection = cls.get_motor_collection()
55
+ projection = {field: 0 for field in exclude_field_list}
56
+
57
+ # 建立查詢條件
58
+ mongo_query = {}
59
+ for q in final_query:
60
+ if hasattr(q, 'query'):
61
+ mongo_query.update(q.query)
62
+
63
+ # 計算總數
64
+ total_num = await collection.count_documents(mongo_query)
65
+ total_pages = (total_num + page_size - 1) // page_size
66
+
67
+ if total_pages == 0:
68
+ page_no = 1
69
+ page_data = []
70
+ else:
71
+ page_no = max(1, min(page_no, total_pages))
72
+
73
+ # 執行分頁查詢
74
+ cursor = collection.find(mongo_query, projection).sort(final_sort).skip(
75
+ (page_no - 1) * page_size).limit(page_size)
76
+ documents = await cursor.to_list(None)
77
+
78
+ # 轉換為 Pydantic 模型
79
+ page_data = []
80
+ for doc in documents:
81
+ try:
82
+ page_data.append(cls.model_validate(doc))
83
+ except Exception as e:
84
+ print(f"模型驗證失敗: {e}")
85
+ continue
59
86
  else:
60
- page_no = max(1, min(page_no, total_pages))
87
+ # 沒有排除欄位時使用 Beanie 的方法
88
+ total_num = await cls.find(*final_query, ignore_cache=ignore_cache, fetch_links=fetch_links).sort(
89
+ *final_sort).count()
61
90
 
62
- # 建立查詢物件用於取得分頁資料
63
- data_query = cls.find(*final_query, ignore_cache=ignore_cache, fetch_links=fetch_links)
91
+ total_pages = (total_num + page_size - 1) // page_size
64
92
 
65
- # 如果有指定要排除的欄位,使用 project() 方法
66
- if exclude_fields:
67
- projection = {field: 0 for field in exclude_fields}
68
- data_query = data_query.project(projection)
93
+ if total_pages == 0:
94
+ page_no = 1
95
+ page_data = []
96
+ else:
97
+ page_no = max(1, min(page_no, total_pages))
69
98
 
70
- page_data = await data_query.sort(*final_sort).limit(page_size).skip((page_no - 1) * page_size).to_list()
99
+ page_data = await cls.find(*final_query, ignore_cache=ignore_cache, fetch_links=fetch_links).sort(
100
+ *final_sort).limit(page_size).skip((page_no - 1) * page_size).to_list()
71
101
 
72
102
  return page_no, page_size, total_num, page_data
73
103
 
@@ -96,7 +126,7 @@ class InternalBaseDocument(Document):
96
126
 
97
127
  @classmethod
98
128
  async def get_list(cls, app: FastAPI, query: list = None, sort: List[Tuple] = None, ignore_cache: bool = False,
99
- fetch_links: bool = False, exclude_fields: List[str] = None):
129
+ fetch_links: bool = False, exclude_field_list: List[str] = None):
100
130
  if not query:
101
131
  final_query = []
102
132
  else:
@@ -116,15 +146,34 @@ class InternalBaseDocument(Document):
116
146
  continue
117
147
  final_sort.append((cls.id, pymongo.ASCENDING))
118
148
 
119
- # 建立查詢物件
120
- query_obj = cls.find(*final_query, ignore_cache=ignore_cache, fetch_links=fetch_links)
121
-
122
- # 如果有指定要排除的欄位,使用 project() 方法
123
- if exclude_fields:
124
- projection = {field: 0 for field in exclude_fields}
125
- query_obj = query_obj.project(projection)
149
+ if exclude_field_list:
150
+ # 當需要排除欄位時,使用 Motor 直接操作
151
+ collection = cls.get_motor_collection()
152
+ projection = {field: 0 for field in exclude_field_list}
153
+
154
+ # 建立查詢條件
155
+ mongo_query = {}
156
+ for q in final_query:
157
+ if hasattr(q, 'query'):
158
+ mongo_query.update(q.query)
159
+
160
+ # 執行查詢
161
+ cursor = collection.find(mongo_query, projection).sort(final_sort)
162
+ documents = await cursor.to_list(None)
163
+
164
+ # 轉換為 Pydantic 模型
165
+ data = []
166
+ for doc in documents:
167
+ try:
168
+ data.append(cls.model_validate(doc))
169
+ except Exception as e:
170
+ print(f"模型驗證失敗: {e}")
171
+ continue
172
+ else:
173
+ # 沒有排除欄位時使用 Beanie 的方法
174
+ data = await cls.find(*final_query, ignore_cache=ignore_cache, fetch_links=fetch_links).sort(
175
+ *final_sort).to_list()
126
176
 
127
- data = await query_obj.sort(*final_sort).to_list()
128
177
  return data
129
178
 
130
179
  def model_dump(
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: internal
3
- Version: 1.1.35.2
3
+ Version: 1.1.35.4
4
4
  Summary:
5
5
  Author: Ray
6
6
  Author-email: ray@cruisys.com
@@ -31,10 +31,10 @@ internal/interface/base_interface.py,sha256=3YaVjIgLi_pZpLk5SEIk8WVkuICM8qPavT8r
31
31
  internal/middleware/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
32
32
  internal/middleware/log_request.py,sha256=OFwWnGfzXnllcQsBHolAWVsahXKoEhw3OQ1YWOm7RHM,2087
33
33
  internal/model/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
34
- internal/model/base_model.py,sha256=fgqdGrtPLyqAtQU8RQb9hLxvarDuubFgv2uEFczmBbw,6150
34
+ internal/model/base_model.py,sha256=vZPDpqzH7Eigwdfw1BgCtr40mflpf41M2__-6DyUPpQ,7987
35
35
  internal/model/operate.py,sha256=QSM6yXYXpJMwrqkUGEWZLrEBaUgqHwVHY_Fi4S42hKc,3190
36
36
  internal/utils.py,sha256=wK1QumW1AaWE1ga2-WcDH2rtXRr2hSLwXzy-iI5dTzY,3962
37
37
  internal/validator_utils.py,sha256=CqjaVFoAu5MqvBG_AkTP-r7AliWawtUWB851USj4moI,1519
38
- internal-1.1.35.2.dist-info/METADATA,sha256=-wMPKHaGDzPkf7fUi2xXlyUmDTZQK7BeYFSVusCevYQ,941
39
- internal-1.1.35.2.dist-info/WHEEL,sha256=RaoafKOydTQ7I_I3JTrPCg6kUmTgtm4BornzOqyEfJ8,88
40
- internal-1.1.35.2.dist-info/RECORD,,
38
+ internal-1.1.35.4.dist-info/METADATA,sha256=4bWOQq0zKqtjCYRBYrSHZ15SMiFW0bUDsUK7YiydR0I,941
39
+ internal-1.1.35.4.dist-info/WHEEL,sha256=RaoafKOydTQ7I_I3JTrPCg6kUmTgtm4BornzOqyEfJ8,88
40
+ internal-1.1.35.4.dist-info/RECORD,,