internal 1.1.26__tar.gz → 1.1.49.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.
Files changed (41) hide show
  1. {internal-1.1.26 → internal-1.1.49.1}/PKG-INFO +1 -1
  2. {internal-1.1.26 → internal-1.1.49.1}/pyproject.toml +1 -1
  3. {internal-1.1.26 → internal-1.1.49.1}/src/internal/base_config.py +1 -1
  4. {internal-1.1.26 → internal-1.1.49.1}/src/internal/common_enum/event_code.py +7 -0
  5. {internal-1.1.26 → internal-1.1.49.1}/src/internal/common_enum/operator_type.py +1 -0
  6. internal-1.1.49.1/src/internal/common_enum/point_type.py +6 -0
  7. {internal-1.1.26 → internal-1.1.49.1}/src/internal/common_enum/websocket_channel.py +1 -0
  8. {internal-1.1.26 → internal-1.1.49.1}/src/internal/const.py +15 -9
  9. {internal-1.1.26 → internal-1.1.49.1}/src/internal/database.py +24 -2
  10. {internal-1.1.26 → internal-1.1.49.1}/src/internal/http/requests.py +15 -4
  11. {internal-1.1.26 → internal-1.1.49.1}/src/internal/http/responses.py +45 -4
  12. internal-1.1.49.1/src/internal/middleware/log_request.py +90 -0
  13. {internal-1.1.26 → internal-1.1.49.1}/src/internal/model/base_model.py +82 -19
  14. {internal-1.1.26 → internal-1.1.49.1}/src/internal/utils.py +30 -3
  15. {internal-1.1.26 → internal-1.1.49.1}/src/internal/validator_utils.py +7 -3
  16. internal-1.1.26/src/internal/middleware/log_request.py +0 -39
  17. {internal-1.1.26 → internal-1.1.49.1}/README.md +0 -0
  18. {internal-1.1.26 → internal-1.1.49.1}/src/internal/__init__.py +0 -0
  19. {internal-1.1.26 → internal-1.1.49.1}/src/internal/base_factory.py +0 -0
  20. {internal-1.1.26 → internal-1.1.49.1}/src/internal/cache_redis.py +0 -0
  21. {internal-1.1.26 → internal-1.1.49.1}/src/internal/common_enum/__init__.py +0 -0
  22. {internal-1.1.26 → internal-1.1.49.1}/src/internal/common_enum/car_relation_type.py +0 -0
  23. {internal-1.1.26 → internal-1.1.49.1}/src/internal/common_enum/description_type.py +0 -0
  24. {internal-1.1.26 → internal-1.1.49.1}/src/internal/common_enum/device_code.py +0 -0
  25. {internal-1.1.26 → internal-1.1.49.1}/src/internal/common_enum/lpr_direction.py +0 -0
  26. {internal-1.1.26 → internal-1.1.49.1}/src/internal/common_enum/notify_type.py +0 -0
  27. {internal-1.1.26 → internal-1.1.49.1}/src/internal/common_enum/order_type.py +0 -0
  28. {internal-1.1.26 → internal-1.1.49.1}/src/internal/exception/__init__.py +0 -0
  29. {internal-1.1.26 → internal-1.1.49.1}/src/internal/exception/app_exception.py +0 -0
  30. {internal-1.1.26 → internal-1.1.49.1}/src/internal/exception/base_exception.py +0 -0
  31. {internal-1.1.26 → internal-1.1.49.1}/src/internal/exception/internal_exception.py +0 -0
  32. {internal-1.1.26 → internal-1.1.49.1}/src/internal/ext/__init__.py +0 -0
  33. {internal-1.1.26 → internal-1.1.49.1}/src/internal/ext/amazon/__init__.py +0 -0
  34. {internal-1.1.26 → internal-1.1.49.1}/src/internal/ext/amazon/aws/__init__.py +0 -0
  35. {internal-1.1.26 → internal-1.1.49.1}/src/internal/ext/amazon/aws/const.py +0 -0
  36. {internal-1.1.26 → internal-1.1.49.1}/src/internal/http/__init__.py +0 -0
  37. {internal-1.1.26 → internal-1.1.49.1}/src/internal/interface/__init__.py +0 -0
  38. {internal-1.1.26 → internal-1.1.49.1}/src/internal/interface/base_interface.py +0 -0
  39. {internal-1.1.26 → internal-1.1.49.1}/src/internal/middleware/__init__.py +0 -0
  40. {internal-1.1.26 → internal-1.1.49.1}/src/internal/model/__init__.py +0 -0
  41. {internal-1.1.26 → internal-1.1.49.1}/src/internal/model/operate.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: internal
3
- Version: 1.1.26
3
+ Version: 1.1.49.1
4
4
  Summary:
5
5
  Author: Ray
6
6
  Author-email: ray@cruisys.com
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "internal"
3
- version = "1.1.26"
3
+ version = "1.1.49.1"
4
4
  description = ""
5
5
  authors = [{ name = "Ray", email = "ray@cruisys.com" }]
6
6
  readme = "README.md"
@@ -13,7 +13,7 @@ class BaseConfig(BaseSettings):
13
13
  # Request
14
14
  REQUEST_VERIFY_SSL: bool = False
15
15
  REQUEST_PROXY: str = ''
16
- REQUEST_RETRY_COUNT: int = 1
16
+ REQUEST_RETRY_COUNT: int = 0
17
17
  REQUEST_RETRY_DELAY_INITIAL_SECONDS: float = 1.0
18
18
  REQUEST_RETRY_DELAY_FACTOR: float = 1.5
19
19
  REQUEST_RETRY_DELAY_RANDOM_JITTER_MIN: float = 0.0
@@ -8,9 +8,15 @@ class EventCodeEnum(str, Enum):
8
8
  MANUAL_CREATE_SERVICE_TICKET = "manual_create_service_ticket"
9
9
  MANUAL_IMPORT_RESERVATION_SMWS = "manual_import_reservation_smws"
10
10
  MANUAL_MODIFY_USER_SERVICE_TICKET = "manual_modify_user_service_ticket"
11
+ MANUAL_MODIFY_ENABLE_NOTIFY_SERVICE_TICKET = "manual_modify_enable_notify_service_ticket"
12
+ MANUAL_MODIFY_DISABLE_NOTIFY_SERVICE_TICKET = "manual_modify_disable_notify_service_ticket"
11
13
  MANUAL_MODIFY_ESTIMATED_ARRIVAL_TIME_SERVICE_TICKET = "manual_modify_estimated_arrival_time_service_ticket"
12
14
  MANUAL_MODIFY_ESTIMATED_DELIVERY_TIME_SERVICE_TICKET = "manual_modify_estimated_delivery_time_service_ticket"
13
15
  MANUAL_DELETE_ESTIMATED_DELIVERY_TIME_SERVICE_TICKET = "manual_delete_estimated_delivery_time_service_ticket"
16
+ MANUAL_IMPORT_RESERVATION_MODIFY_USER_SERVICE_TICKET = "manual_import_reservation_modify_user_service_ticket"
17
+ MANUAL_IMPORT_RESERVATION_MODIFY_ESTIMATED_ARRIVAL_TIME_SERVICE_TICKET = "manual_import_reservation_modify_estimated_arrival_time_service_ticket"
18
+ MANUAL_IMPORT_RESERVATION_MODIFY_ESTIMATED_DELIVERY_TIME_SERVICE_TICKET = "manual_import_reservation_modify_estimated_delivery_time_service_ticket"
19
+ MANUAL_IMPORT_RESERVATION_DELETE_ESTIMATED_DELIVERY_TIME_SERVICE_TICKET = "manual_import_reservation_delete_estimated_delivery_time_service_ticket"
14
20
  MANUAL_BOOKING_MESSAGE_SERVICE_TICKET = "manual_booking_message_service_ticket"
15
21
  MANUAL_DELIVERY_MESSAGE_SERVICE_TICKET = "manual_delivery_message_service_ticket"
16
22
 
@@ -39,3 +45,4 @@ class EventCodeEnum(str, Enum):
39
45
 
40
46
  REPAIR_OVERDUE = "repair_overdue"
41
47
  DETAILING_OVERDUE = "detailing_overdue"
48
+ DELIVERY_OVERDUE = "delivery_overdue"
@@ -7,3 +7,4 @@ class OperatorTypeEnum(str, Enum):
7
7
  CUSTOMER = "customer"
8
8
  PROVIDER = "provider"
9
9
  CLIENT = "client"
10
+ ADMIN = "admin"
@@ -0,0 +1,6 @@
1
+ from enum import Enum
2
+
3
+
4
+ class PointTypeEnum(str, Enum):
5
+ INVOICE_REWARD = "invoice_reward"
6
+ ACTIVITY = "activity"
@@ -6,3 +6,4 @@ class WebsocketChannelEnum(str, Enum):
6
6
  RECEPTION_CENTER = "reception_center" # 接待中心
7
7
  CAR_MOVEMENT = "car_movement" # 車輛動態
8
8
  USER_BELL_NOTIFICATION = "user_bell_notification" # 個人小鈴噹
9
+ DAILY_DELIVERY_SUMMARY = "daily_delivery_summary" # 當日交車統計
@@ -44,12 +44,18 @@ REDIS_LPR_DATA_LIST_PREFIX = "lpr_data_list"
44
44
 
45
45
  CORRELATION_ID_HEADER_KEY_NAME = "X-Request-ID"
46
46
 
47
- SOURCE_CAR_BY_SMART_WORKSHOP_IMPORTED_SERVICE_TICKET = 'CarBySmartWorkshopImportedServiceTicket'
48
- SOURCE_CAR_BY_SMART_WORKSHOP_IMPORTED_DMS_DATA = 'CarBySmartWorkshopImportedDMSData'
49
- SOURCE_CAR_BY_WEBHOOK = 'CarByWebhook'
50
- SOURCE_CAR_BY_SMART_WORKSHOP = 'CarBySmartWorkshopManual'
51
-
52
- SOURCE_CUSTOMER_BY_SMART_WORKSHOP_IMPORTED_SERVICE_TICKET = 'CustomerBySmartWorkshopImportedServiceTicket'
53
- SOURCE_CUSTOMER_BY_SMART_WORKSHOP_IMPORTED_DMS_DATA = 'CustomerBySmartWorkshopImportedDMSData'
54
- SOURCE_CUSTOMER_BY_WEBHOOK = 'CustomerByWebhook'
55
- SOURCE_CUSTOMER_BY_SMART_WORKSHOP = 'CustomerBySmartWorkshopManual'
47
+ SOURCE_SMART_WORKSHOP_IMPORTED_SERVICE_TICKET = 'SmartWorkshopImportedServiceTicket'
48
+ SOURCE_SMART_WORKSHOP_UPDATE_SERVICE_TICKET = 'SmartWorkshopUpdateServiceTicket'
49
+ SOURCE_SMART_WORKSHOP_IMPORTED_UPDATE_SERVICE_TICKET = 'SmartWorkshopImportedUpdateServiceTicket'
50
+ SOURCE_SMART_WORKSHOP_IMPORTED_DMS_DATA = 'SmartWorkshopImportedDMSData'
51
+ SOURCE_SMART_WORKSHOP_IMPORTED_SELF_SELLING_DATA = 'SmartWorkshopImportedSelfSellingData'
52
+ SOURCE_WEBHOOK = 'Webhook'
53
+ SOURCE_SMART_WORKSHOP = 'SmartWorkshopManual'
54
+ SOURCE_SERVICE_GO_LOGIN = 'ServiceGoLogin'
55
+ SOURCE_SERVICE_GO_REGISTER = 'ServiceGoRegister'
56
+ SOURCE_VIP_CODE_MANUAL_APPROVE = 'VIPCodeManualApprove'
57
+ SOURCE_BINDING_SERVICE_GO = 'BindingFromServiceGo'
58
+ SOURCE_BINDING_VIP_CODE = 'BindingFromVIPCode'
59
+ SOURCE_BINDING_SMART_WORKSHOP = 'BindingFromSmartWorkshop'
60
+ SOURCE_UNBINDING_SERVICE_GO = 'UnbindingFromServiceGo'
61
+ SOURCE_UNBINDING_SMART_WORKSHOP = 'UnbindingFromSmartWorkshop'
@@ -2,6 +2,18 @@ from motor.motor_asyncio import AsyncIOMotorClient
2
2
  from pymongo.errors import ServerSelectionTimeoutError
3
3
 
4
4
  from .exception.internal_exception import DatabaseInitializeFailureException, DatabaseConnectFailureException
5
+ from bson import Decimal128
6
+ from bson.codec_options import CodecOptions, TypeDecoder, TypeRegistry
7
+ from decimal import Decimal
8
+
9
+
10
+ class Decimal128Decoder(TypeDecoder):
11
+ """自定義 Decimal128 解碼器,自動轉換為 Python Decimal"""
12
+ bson_type = Decimal128
13
+ python_type = Decimal
14
+
15
+ def transform_bson(self, value):
16
+ return value.to_decimal()
5
17
 
6
18
 
7
19
  class MongoDB:
@@ -39,10 +51,20 @@ class MongoDB:
39
51
  if self.client:
40
52
  self.client.close()
41
53
 
42
- def get_database(self):
54
+ def get_database(self, db_name: str = None):
43
55
  if not self.client:
44
56
  raise DatabaseConnectFailureException()
45
- return self.client[self.db_name]
57
+
58
+ type_registry = TypeRegistry([Decimal128Decoder()])
59
+ codec_options = CodecOptions(type_registry=type_registry)
60
+
61
+ name = db_name if db_name else self.db_name
62
+ return self.client.get_database(name, codec_options=codec_options)
63
+
64
+ def get_collection(self, collection_name: str, db_name: str = None):
65
+ """直接取得指定的集合"""
66
+ db = self.get_database(db_name)
67
+ return db[collection_name]
46
68
 
47
69
  async def get_mongodb_uri(self) -> str:
48
70
  if self.user_name and self.password:
@@ -1,4 +1,5 @@
1
1
  import asyncio
2
+ import json
2
3
  import random
3
4
 
4
5
  import httpx
@@ -17,11 +18,21 @@ async def invoke_request(timeout: httpx.Timeout, method: str, url: str, app: Fas
17
18
  if "json" in kwargs:
18
19
  kwargs["json"] = jsonable_encoder(kwargs["json"])
19
20
 
20
- app.state.logger.info(f"{app.title} \nURL: {method} {url} \nkwargs: {kwargs} \ninvoke_request() request")
21
+ app.state.logger.info(f"REQUEST | {kwargs.get('headers').get(CORRELATION_ID_HEADER_KEY_NAME)} | {method} | {url} | kwargs={kwargs}")
22
+
21
23
  response = await client.request(method, url, **kwargs)
22
- app.state.logger.info(
23
- f"【{app.title}】 \nURL: {method} {url} \nkwargs: {kwargs} \ninvoke_request() response \n\nstatus_code: {response.status_code} \ntext: {response.text}"
24
- )
24
+
25
+ try:
26
+ response_json = json.loads(response.text)
27
+ response_text = json.dumps(response_json, ensure_ascii=False)
28
+ app.state.logger.info(
29
+ f"RESPONSE | {kwargs.get('headers').get(CORRELATION_ID_HEADER_KEY_NAME)} | {method} | {url} | {response.status_code} | {response_text}"
30
+ )
31
+ except json.decoder.JSONDecodeError:
32
+ app.state.logger.info(
33
+ f"RESPONSE | {kwargs.get('headers').get(CORRELATION_ID_HEADER_KEY_NAME)} | {method} | {url} | {response.status_code} | {response.text}"
34
+ )
35
+
25
36
  return response
26
37
  except httpx.TimeoutException as exc:
27
38
  message = f"【{app.title}】 \nURL: {method} {url} \nkwargs: {kwargs} \ninvoke_request(), TimeoutException, exc: {exc}"
@@ -2,6 +2,7 @@ import json
2
2
  import arrow
3
3
 
4
4
  from datetime import datetime, date
5
+ from pydantic import BaseModel
5
6
 
6
7
  import httpx
7
8
 
@@ -14,23 +15,35 @@ from ..const import ARR_HUMAN_READ_FMT, ARR_DATE_FMT
14
15
 
15
16
  async def async_response(data=None, message=None, code=None, page_no=None, total_num=None, page_size=None,
16
17
  time_zone="UTC", time_format=ARR_HUMAN_READ_FMT, date_format=ARR_DATE_FMT,
17
- status_code=status.HTTP_200_OK):
18
+ status_code=status.HTTP_200_OK, is_need_exclude_secret_key=True):
18
19
  def _serialize(data):
19
- if issubclass(type(data), Document):
20
+ if issubclass(type(data), Document) or issubclass(type(data), BaseModel):
20
21
  link_field_list = []
21
22
  datetime_field_list = []
22
23
  date_field_list = []
24
+ replace_dict = {}
23
25
  for field_name in data.__annotations__:
24
26
  field_type = getattr(data, field_name)
27
+ if field_name in ["contact", "pick_up", "car", "customer"]:
28
+ if field_type:
29
+ replace_dict[field_name] = _serialize(field_type)
30
+
25
31
  if isinstance(field_type, Link):
26
32
  link_field_list.append(field_name)
27
33
 
28
- if isinstance(field_type, datetime):
34
+ if field_name.endswith('_date') or field_name == "birthday" or field_name.endswith('_birthday'):
35
+ date_field_list.append(field_name)
36
+ elif isinstance(field_type, datetime):
29
37
  datetime_field_list.append(field_name)
30
38
  elif isinstance(field_type, date):
31
39
  date_field_list.append(field_name)
32
40
 
33
- data = json.loads(data.model_dump_json(exclude={"password", "metadata", "otp_code_universal"}, by_alias=False))
41
+ if is_need_exclude_secret_key:
42
+ data = json.loads(
43
+ data.model_dump_json(exclude={"password", "metadata", "otp_code_universal"}, by_alias=False))
44
+ else:
45
+ data = json.loads(data.model_dump_json(by_alias=False))
46
+
34
47
  if link_field_list:
35
48
  for field_name in link_field_list:
36
49
  if isinstance(data[field_name], dict) and "id" in data[field_name].keys():
@@ -42,6 +55,34 @@ async def async_response(data=None, message=None, code=None, page_no=None, total
42
55
  if data[field_name]:
43
56
  data[field_name] = arrow.get(data[field_name]).to(time_zone).format(date_format)
44
57
 
58
+ for key, value in replace_dict.items():
59
+ data[key] = value
60
+
61
+ if "create_time" in data.keys() and data.get("create_time"):
62
+ data["create_time"] = arrow.get(data["create_time"]).to(time_zone).format(ARR_HUMAN_READ_FMT)
63
+
64
+ if "update_time" in data.keys() and data.get("update_time"):
65
+ data["update_time"] = arrow.get(data["update_time"]).to(time_zone).format(ARR_HUMAN_READ_FMT)
66
+
67
+ elif isinstance(data, dict):
68
+ datetime_field_list = []
69
+ date_field_list = []
70
+ for field_name, field_type in data.items():
71
+ if field_name.endswith('_date') or field_name == "birthday" or field_name.endswith('_birthday'):
72
+ date_field_list.append(field_name)
73
+ elif isinstance(field_type, datetime):
74
+ datetime_field_list.append(field_name)
75
+ elif isinstance(field_type, date):
76
+ date_field_list.append(field_name)
77
+
78
+ data = json.loads(json.dumps(data, default=jsonable_encoder))
79
+ for field_name in datetime_field_list:
80
+ if data[field_name]:
81
+ data[field_name] = arrow.get(data[field_name]).to(time_zone).format(time_format)
82
+ for field_name in date_field_list:
83
+ if data[field_name]:
84
+ data[field_name] = arrow.get(data[field_name]).to(time_zone).format(date_format)
85
+
45
86
  if "create_time" in data.keys() and data.get("create_time"):
46
87
  data["create_time"] = arrow.get(data["create_time"]).to(time_zone).format(ARR_HUMAN_READ_FMT)
47
88
 
@@ -0,0 +1,90 @@
1
+ import json
2
+ import logging
3
+ import time
4
+
5
+ from collections import defaultdict
6
+ from fastapi import FastAPI, Request
7
+ from starlette.middleware.base import BaseHTTPMiddleware
8
+ from ..const import CORRELATION_ID_HEADER_KEY_NAME
9
+
10
+
11
+ class LogRequestMiddleware(BaseHTTPMiddleware):
12
+ def __init__(self, app: FastAPI, logger: logging.Logger):
13
+ super().__init__(app)
14
+ self.app = app
15
+ self.logger = logger
16
+
17
+ async def dispatch(self, request: Request, call_next):
18
+ # 记录请求的URL和参数
19
+ url = request.url.path
20
+ method = request.method
21
+ headers = request.headers
22
+ request_id = headers.get(CORRELATION_ID_HEADER_KEY_NAME, "")
23
+ content_type = headers.get('content-type', '') or headers.get('Content-Type', '')
24
+ is_multipart = 'multipart' in content_type.lower()
25
+
26
+ query_params = request.query_params
27
+ temp = defaultdict(list)
28
+ for key, value in query_params.multi_items():
29
+ temp[key].append(value)
30
+ # 如果只有一個值就轉成單一值
31
+ params = {k: v[0] if len(v) == 1 else v for k, v in temp.items()}
32
+
33
+ body = await request.body()
34
+
35
+ # 解碼 body 為字符串(如果不是 multipart)
36
+ if is_multipart:
37
+ body_str = "因上傳檔案不顯示body"
38
+ else:
39
+ try:
40
+ # 嘗試使用 UTF-8 解碼
41
+ body_str = body.decode('utf-8')
42
+ except UnicodeDecodeError:
43
+ # 如果解碼失敗,顯示原始 bytes
44
+ body_str = str(body)
45
+
46
+ request_info = {
47
+ "request_id": request_id,
48
+ "method": method,
49
+ "url": url,
50
+ "params": params,
51
+ "body": body_str,
52
+ "content_type": content_type,
53
+ "user_agent": headers.get("user-agent", "")
54
+ }
55
+ self.logger.info(f"Request started: {json.dumps(request_info, ensure_ascii=False)}")
56
+
57
+ # 记录请求处理时间
58
+ start_time = time.time()
59
+ try:
60
+ response = await call_next(request)
61
+ process_time = time.time() - start_time
62
+ status_code = response.status_code
63
+
64
+ # 记录成功响应
65
+ response_info = {
66
+ "request_id": request_id,
67
+ "method": method,
68
+ "url": url,
69
+ "status_code": status_code,
70
+ "process_time": round(process_time, 4)
71
+ }
72
+
73
+ self.logger.info(f"Request completed: {json.dumps(response_info, ensure_ascii=False)}")
74
+
75
+ return response
76
+
77
+ except Exception as e:
78
+ process_time = time.time() - start_time
79
+
80
+ # 记录异常
81
+ error_info = {
82
+ "request_id": request_id,
83
+ "method": method,
84
+ "url": url,
85
+ "error": str(e),
86
+ "process_time": round(process_time, 4)
87
+ }
88
+
89
+ self.logger.error(f"Request failed: {json.dumps(error_info, ensure_ascii=False)}")
90
+ raise
@@ -1,8 +1,6 @@
1
1
  from datetime import datetime
2
2
 
3
3
  from typing import List, Tuple, Union, Dict, Type, Optional, Any, Literal, Union, Set, Mapping
4
- import typing_extensions
5
- from pydantic_core import PydanticUndefined
6
4
  from typing_extensions import Self, TypeAlias, Unpack
7
5
 
8
6
  import arrow
@@ -27,8 +25,8 @@ class InternalBaseDocument(Document):
27
25
  @classmethod
28
26
  async def get_pagination_list(cls, app: FastAPI, query: list = None, sort: List[Tuple] = None,
29
27
  page_size: int = DEF_PAGE_SIZE, page_no: int = DEF_PAGE_NO,
30
- ignore_cache: bool = False,
31
- fetch_links: bool = False):
28
+ ignore_cache: bool = False, fetch_links: bool = False,
29
+ exclude_field_list: List[str] = None):
32
30
  if not query:
33
31
  final_query = []
34
32
  else:
@@ -46,21 +44,59 @@ class InternalBaseDocument(Document):
46
44
  else:
47
45
  print(f"order type value error: temp_sort:{temp_sort}")
48
46
  continue
49
- final_sort.append((cls.id, pymongo.ASCENDING))
50
47
 
51
- total_num = await cls.find(*final_query, ignore_cache=ignore_cache, fetch_links=fetch_links).sort(
52
- *final_sort).count()
48
+ if not any(s[0] == str(cls.id) for s in sort):
49
+ final_sort.append((cls.id, pymongo.ASCENDING))
50
+
51
+ if exclude_field_list:
52
+ # 當需要排除欄位時,使用 Motor 直接操作
53
+ collection = cls.get_motor_collection()
54
+ projection = {field: 0 for field in exclude_field_list}
55
+
56
+ # 建立查詢條件
57
+ mongo_query = {}
58
+ for q in final_query:
59
+ if hasattr(q, 'query'):
60
+ mongo_query.update(q.query)
61
+
62
+ # 計算總數
63
+ total_num = await collection.count_documents(mongo_query)
64
+ total_pages = (total_num + page_size - 1) // page_size
65
+
66
+ if total_pages == 0:
67
+ page_no = 1
68
+ page_data = []
69
+ else:
70
+ page_no = max(1, min(page_no, total_pages))
71
+
72
+ # 執行分頁查詢
73
+ cursor = collection.find(mongo_query, projection).sort(final_sort).skip(
74
+ (page_no - 1) * page_size).limit(page_size)
75
+ documents = await cursor.to_list(None)
76
+
77
+ # 轉換為 Pydantic 模型
78
+ page_data = []
79
+ for doc in documents:
80
+ try:
81
+ page_data.append(cls.model_validate(doc))
82
+ except Exception as e:
83
+ print(f"模型驗證失敗: {e}")
84
+ continue
85
+ else:
86
+ # 沒有排除欄位時使用 Beanie 的方法
87
+ total_num = await cls.find(*final_query, ignore_cache=ignore_cache, fetch_links=fetch_links).sort(
88
+ *final_sort).count()
53
89
 
54
- total_pages = (total_num + page_size - 1) // page_size
90
+ total_pages = (total_num + page_size - 1) // page_size
55
91
 
56
- if total_pages == 0:
57
- page_no = 1
58
- page_data = []
59
- else:
60
- page_no = max(1, min(page_no, total_pages))
92
+ if total_pages == 0:
93
+ page_no = 1
94
+ page_data = []
95
+ else:
96
+ page_no = max(1, min(page_no, total_pages))
61
97
 
62
- page_data = await cls.find(*final_query, ignore_cache=ignore_cache, fetch_links=fetch_links).sort(
63
- *final_sort).limit(page_size).skip((page_no - 1) * page_size).to_list()
98
+ page_data = await cls.find(*final_query, ignore_cache=ignore_cache, fetch_links=fetch_links).sort(
99
+ *final_sort).limit(page_size).skip((page_no - 1) * page_size).to_list()
64
100
 
65
101
  return page_no, page_size, total_num, page_data
66
102
 
@@ -89,7 +125,7 @@ class InternalBaseDocument(Document):
89
125
 
90
126
  @classmethod
91
127
  async def get_list(cls, app: FastAPI, query: list = None, sort: List[Tuple] = None, ignore_cache: bool = False,
92
- fetch_links: bool = False):
128
+ fetch_links: bool = False, exclude_field_list: List[str] = None):
93
129
  if not query:
94
130
  final_query = []
95
131
  else:
@@ -107,10 +143,37 @@ class InternalBaseDocument(Document):
107
143
  else:
108
144
  print(f"order type value error: temp_sort:{temp_sort}")
109
145
  continue
110
- final_sort.append((cls.id, pymongo.ASCENDING))
111
146
 
112
- data = await cls.find(*final_query, ignore_cache=ignore_cache, fetch_links=fetch_links).sort(
113
- *final_sort).to_list()
147
+ if not any(s[0] == str(cls.id) for s in sort):
148
+ final_sort.append((cls.id, pymongo.ASCENDING))
149
+
150
+ if exclude_field_list:
151
+ # 當需要排除欄位時,使用 Motor 直接操作
152
+ collection = cls.get_motor_collection()
153
+ projection = {field: 0 for field in exclude_field_list}
154
+
155
+ # 建立查詢條件
156
+ mongo_query = {}
157
+ for q in final_query:
158
+ if hasattr(q, 'query'):
159
+ mongo_query.update(q.query)
160
+
161
+ # 執行查詢
162
+ cursor = collection.find(mongo_query, projection).sort(final_sort)
163
+ documents = await cursor.to_list(None)
164
+
165
+ # 轉換為 Pydantic 模型
166
+ data = []
167
+ for doc in documents:
168
+ try:
169
+ data.append(cls.model_validate(doc))
170
+ except Exception as e:
171
+ print(f"模型驗證失敗: {e}")
172
+ continue
173
+ else:
174
+ # 沒有排除欄位時使用 Beanie 的方法
175
+ data = await cls.find(*final_query, ignore_cache=ignore_cache, fetch_links=fetch_links).sort(
176
+ *final_sort).to_list()
114
177
 
115
178
  return data
116
179
 
@@ -1,11 +1,12 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  import json
3
+ import hashlib
3
4
  from datetime import datetime, timezone
4
5
 
5
6
  import arrow
6
7
 
7
8
  from .base_config import BaseConfig
8
- from .const import STR_EMPTY, ARR_EXPORT_DATETIME_FMT, STR_DASH, REDIS_LPR_DATA_LIST_PREFIX
9
+ from .const import STR_EMPTY, ARR_EXPORT_DATETIME_FMT, STR_DASH, REDIS_LPR_DATA_LIST_PREFIX, STR_SPACE
9
10
 
10
11
 
11
12
  def is_today(time, system_time_zone):
@@ -48,7 +49,7 @@ def update_dict_with_cast(curr_settings: BaseConfig, new_conf: dict):
48
49
 
49
50
 
50
51
  def sanitize_plate_no(plate_no):
51
- return plate_no.replace(STR_DASH, STR_EMPTY).upper()
52
+ return plate_no.replace(STR_SPACE, STR_EMPTY).replace(STR_DASH, STR_EMPTY).upper()
52
53
 
53
54
 
54
55
  def get_current_utc() -> datetime:
@@ -105,6 +106,7 @@ def get_dealer_by_organization_id(organization_id: str) -> str:
105
106
 
106
107
  return organization_id
107
108
 
109
+
108
110
  def extract_title(name):
109
111
  if '小姐' in name:
110
112
  return '小姐'
@@ -115,6 +117,7 @@ def extract_title(name):
115
117
  else:
116
118
  return None
117
119
 
120
+
118
121
  def extract_name(name):
119
122
  """从姓名中提取真实姓名"""
120
123
  # 检查是否包含 '小姐' 或 '先生' 并提取姓名
@@ -125,4 +128,28 @@ def extract_name(name):
125
128
  elif '先生' in name:
126
129
  return name.split('先生')[0].strip()
127
130
  else:
128
- return name.strip()
131
+ return name.strip()
132
+
133
+
134
+ def hash_login_password(passwd):
135
+ prefix = '___'
136
+ postfix = '_______'
137
+ data = str(passwd)
138
+ sha_256 = hashlib.sha256()
139
+ sha_256.update(prefix.encode())
140
+ sha_256.update(data.encode())
141
+ sha_256.update(postfix.encode())
142
+
143
+ return sha_256.hexdigest()
144
+
145
+
146
+ def hash_secret_key(passwd):
147
+ prefix = '___'
148
+ postfix = '_______'
149
+ data = str(passwd)
150
+ sha_256 = hashlib.sha256()
151
+ sha_256.update(prefix.encode())
152
+ sha_256.update(data.encode())
153
+ sha_256.update(postfix.encode())
154
+
155
+ return sha_256.hexdigest()
@@ -10,8 +10,12 @@ def verify_and_sanitize_plate_no(value: str, is_require: bool = False):
10
10
  raise PlateNoFormatException()
11
11
 
12
12
  if value:
13
- if not re.match(r'^[A-Za-z0-9]{6,7}$', value):
13
+ # 移除空格和常見分隔符號進行檢查
14
+ cleaned = sanitize_plate_no(value)
15
+
16
+ if not re.match(r'^[外使領試臨軍A-Za-z0-9]{5,7}\Z', cleaned):
14
17
  raise PlateNoFormatException()
18
+
15
19
  return sanitize_plate_no(value)
16
20
 
17
21
 
@@ -20,7 +24,7 @@ def verify_vin(value: str, is_require: bool = False):
20
24
  raise VinLengthOrFormatException()
21
25
 
22
26
  if value:
23
- if not re.match(r'^(?:[A-Za-z0-9]{7}|[A-Za-z0-9]{10}|[A-Za-z0-9]{17})$', value):
27
+ if not re.match(r'^(?:[A-Za-z0-9]{7}|[A-Za-z0-9]{10}|[A-Za-z0-9]{17})\Z', value):
24
28
  raise VinLengthOrFormatException()
25
29
 
26
30
 
@@ -29,7 +33,7 @@ def verify_phone(value: str, is_require: bool = False):
29
33
  raise PhoneFormatException()
30
34
 
31
35
  if value:
32
- if not re.match(r'^09\d{8}$', value):
36
+ if not re.match(r'^09\d{8}\Z', value):
33
37
  raise PhoneFormatException()
34
38
 
35
39
 
@@ -1,39 +0,0 @@
1
- import logging
2
- import time
3
-
4
- from collections import defaultdict
5
- from fastapi import FastAPI, Request
6
- from starlette.middleware.base import BaseHTTPMiddleware
7
- from ..const import CORRELATION_ID_HEADER_KEY_NAME
8
-
9
-
10
- class LogRequestMiddleware(BaseHTTPMiddleware):
11
- def __init__(self, app: FastAPI, logger: logging.Logger):
12
- super().__init__(app)
13
- self.app = app
14
- self.logger = logger
15
-
16
- async def dispatch(self, request: Request, call_next):
17
- # 记录请求的URL和参数
18
- url = request.url.path
19
- method = request.method
20
- headers = request.headers
21
- request_id = headers.get(CORRELATION_ID_HEADER_KEY_NAME, "")
22
-
23
- query_params = request.query_params
24
- temp = defaultdict(list)
25
- for key, value in query_params.multi_items():
26
- temp[key].append(value)
27
- # 如果只有一個值就轉成單一值
28
- params = {k: v[0] if len(v) == 1 else v for k, v in temp.items()}
29
-
30
- body = await request.body()
31
- self.logger.info(f"[Request id: {request_id}] \nURL: {method} {url} \nParams: {params} \nBody: {body} \nHeaders: {headers} \nstart processing...")
32
-
33
- # 记录请求处理时间
34
- start_time = time.time()
35
- response = await call_next(request)
36
- process_time = time.time() - start_time
37
-
38
- self.logger.info(f"[Request id: {request_id}] \nURL: {method} {url} \nParams: {params} \nBody: {body} \nCompleted in {process_time:.4f} seconds")
39
- return response
File without changes