crud.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. from collections.abc import Sequence
  2. from datetime import datetime
  3. from typing import Any
  4. from app.api.v1.module_system.auth.schema import AuthSchema
  5. from app.plugin.module_payment.apikey.model import TenantApiKeyModel, TenantApiLogModel
  6. from app.plugin.module_payment.apikey.schema import (
  7. TenantApiKeyCreate,
  8. TenantApiKeyUpdate,
  9. TenantApiLogCreate,
  10. )
  11. from app.core.base_crud import CRUDBase
  12. class TenantApiKeyCRUD(CRUDBase[TenantApiKeyModel, TenantApiKeyCreate, TenantApiKeyUpdate]):
  13. """租户API Key数据层"""
  14. def __init__(self, auth: AuthSchema) -> None:
  15. self.auth = auth
  16. super().__init__(model=TenantApiKeyModel, auth=auth)
  17. async def get_by_id_crud(
  18. self, id: int, preload: list[str | Any] | None = None
  19. ) -> TenantApiKeyModel | None:
  20. return await self.get(id=id, preload=preload)
  21. async def get_by_api_key(self, api_key: str) -> TenantApiKeyModel | None:
  22. return await self.get(
  23. preload=[],
  24. api_key=api_key,
  25. status="0",
  26. expired_at=("None", None),
  27. )
  28. async def create_crud(
  29. self,
  30. api_key: str,
  31. api_secret: str,
  32. tenant_id: int,
  33. expired_at: datetime,
  34. description: str | None = None,
  35. ) -> TenantApiKeyModel:
  36. data = {
  37. "tenant_id": tenant_id,
  38. "description": description,
  39. "api_key": api_key,
  40. "api_secret": api_secret,
  41. "expired_at": expired_at,
  42. }
  43. obj = await self.create(data=data, skip_tenant_id=True)
  44. return obj
  45. async def update_status_crud(self, api_key_id: int, status: str) -> TenantApiKeyModel | None:
  46. return await self.update(id=api_key_id, data={"status": status})
  47. async def update_last_used_crud(self, api_key_id: int) -> None:
  48. api_key_obj = await self.get_by_id_crud(api_key_id)
  49. if api_key_obj:
  50. api_key_obj.last_used_at = datetime.now()
  51. await self.auth.db.flush()
  52. async def delete_crud(self, api_key_id: int) -> bool:
  53. try:
  54. await self.delete(ids=[api_key_id])
  55. return True
  56. except Exception:
  57. return False
  58. async def get_list_crud(
  59. self,
  60. tenant_id: int | None = None,
  61. status: str | None = None,
  62. skip: int = 0,
  63. limit: int = 100,
  64. ) -> Sequence[TenantApiKeyModel]:
  65. search: dict[str, Any] = {}
  66. if tenant_id:
  67. search["tenant_id"] = tenant_id
  68. if status:
  69. search["status"] = status
  70. return await self.list(
  71. search=search if search else None,
  72. order_by=[{"created_time": "desc"}],
  73. )
  74. class TenantApiLogCRUD(CRUDBase[TenantApiLogModel, TenantApiLogCreate, TenantApiLogCreate]):
  75. """租户API调用日志数据层"""
  76. def __init__(self, auth: AuthSchema) -> None:
  77. self.auth = auth
  78. super().__init__(model=TenantApiLogModel, auth=auth)
  79. async def create_crud(
  80. self,
  81. api_key_id: int | None,
  82. tenant_id: int,
  83. endpoint: str,
  84. method: str,
  85. request_ip: str,
  86. request_data: str | None,
  87. response_code: int,
  88. response_time: float,
  89. ) -> TenantApiLogModel:
  90. data = TenantApiLogCreate(
  91. api_key_id=api_key_id,
  92. tenant_id=tenant_id,
  93. endpoint=endpoint,
  94. method=method,
  95. request_ip=request_ip,
  96. request_data=request_data,
  97. response_code=response_code,
  98. response_time=response_time,
  99. )
  100. return await self.create(data=data, skip_tenant_id=True)
  101. async def get_list_crud(
  102. self,
  103. tenant_id: int | None = None,
  104. api_key_id: int | None = None,
  105. endpoint: str | None = None,
  106. skip: int = 0,
  107. limit: int = 100,
  108. ) -> Sequence[TenantApiLogModel]:
  109. search: dict[str, Any] = {}
  110. if tenant_id:
  111. search["tenant_id"] = tenant_id
  112. if api_key_id:
  113. search["api_key_id"] = api_key_id
  114. if endpoint:
  115. search["endpoint"] = ("like", endpoint)
  116. return await self.list(
  117. search=search if search else None,
  118. order_by=[{"created_time": "desc"}],
  119. )