Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

user_service.py 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #
  2. # Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. from datetime import datetime
  17. import peewee
  18. from werkzeug.security import generate_password_hash, check_password_hash
  19. from api.db import UserTenantRole
  20. from api.db.db_models import DB, UserTenant
  21. from api.db.db_models import User, Tenant
  22. from api.db.services.common_service import CommonService
  23. from api.utils import get_uuid, current_timestamp, datetime_format
  24. from api.db import StatusEnum
  25. class UserService(CommonService):
  26. model = User
  27. @classmethod
  28. @DB.connection_context()
  29. def filter_by_id(cls, user_id):
  30. try:
  31. user = cls.model.select().where(cls.model.id == user_id).get()
  32. return user
  33. except peewee.DoesNotExist:
  34. return None
  35. @classmethod
  36. @DB.connection_context()
  37. def query_user(cls, email, password):
  38. user = cls.model.select().where((cls.model.email == email),
  39. (cls.model.status == StatusEnum.VALID.value)).first()
  40. if user and check_password_hash(str(user.password), password):
  41. return user
  42. else:
  43. return None
  44. @classmethod
  45. @DB.connection_context()
  46. def save(cls, **kwargs):
  47. if "id" not in kwargs:
  48. kwargs["id"] = get_uuid()
  49. if "password" in kwargs:
  50. kwargs["password"] = generate_password_hash(
  51. str(kwargs["password"]))
  52. kwargs["create_time"] = current_timestamp()
  53. kwargs["create_date"] = datetime_format(datetime.now())
  54. kwargs["update_time"] = current_timestamp()
  55. kwargs["update_date"] = datetime_format(datetime.now())
  56. obj = cls.model(**kwargs).save(force_insert=True)
  57. return obj
  58. @classmethod
  59. @DB.connection_context()
  60. def delete_user(cls, user_ids, update_user_dict):
  61. with DB.atomic():
  62. cls.model.update({"status": 0}).where(
  63. cls.model.id.in_(user_ids)).execute()
  64. @classmethod
  65. @DB.connection_context()
  66. def update_user(cls, user_id, user_dict):
  67. with DB.atomic():
  68. if user_dict:
  69. user_dict["update_time"] = current_timestamp()
  70. user_dict["update_date"] = datetime_format(datetime.now())
  71. cls.model.update(user_dict).where(
  72. cls.model.id == user_id).execute()
  73. class TenantService(CommonService):
  74. model = Tenant
  75. @classmethod
  76. @DB.connection_context()
  77. def get_info_by(cls, user_id):
  78. fields = [
  79. cls.model.id.alias("tenant_id"),
  80. cls.model.name,
  81. cls.model.llm_id,
  82. cls.model.embd_id,
  83. cls.model.rerank_id,
  84. cls.model.asr_id,
  85. cls.model.img2txt_id,
  86. cls.model.tts_id,
  87. cls.model.parser_ids,
  88. UserTenant.role]
  89. return list(cls.model.select(*fields)
  90. .join(UserTenant, on=((cls.model.id == UserTenant.tenant_id) & (UserTenant.user_id == user_id) & (UserTenant.status == StatusEnum.VALID.value) & (UserTenant.role == UserTenantRole.OWNER)))
  91. .where(cls.model.status == StatusEnum.VALID.value).dicts())
  92. @classmethod
  93. @DB.connection_context()
  94. def get_joined_tenants_by_user_id(cls, user_id):
  95. fields = [
  96. cls.model.id.alias("tenant_id"),
  97. cls.model.name,
  98. cls.model.llm_id,
  99. cls.model.embd_id,
  100. cls.model.asr_id,
  101. cls.model.img2txt_id,
  102. UserTenant.role]
  103. return list(cls.model.select(*fields)
  104. .join(UserTenant, on=((cls.model.id == UserTenant.tenant_id) & (UserTenant.user_id == user_id) & (UserTenant.status == StatusEnum.VALID.value) & (UserTenant.role == UserTenantRole.NORMAL)))
  105. .where(cls.model.status == StatusEnum.VALID.value).dicts())
  106. @classmethod
  107. @DB.connection_context()
  108. def decrease(cls, user_id, num):
  109. num = cls.model.update(credit=cls.model.credit - num).where(
  110. cls.model.id == user_id).execute()
  111. if num == 0:
  112. raise LookupError("Tenant not found which is supposed to be there")
  113. class UserTenantService(CommonService):
  114. model = UserTenant
  115. @classmethod
  116. @DB.connection_context()
  117. def save(cls, **kwargs):
  118. if "id" not in kwargs:
  119. kwargs["id"] = get_uuid()
  120. obj = cls.model(**kwargs).save(force_insert=True)
  121. return obj
  122. @classmethod
  123. @DB.connection_context()
  124. def get_by_tenant_id(cls, tenant_id):
  125. fields = [
  126. cls.model.user_id,
  127. cls.model.status,
  128. cls.model.role,
  129. User.nickname,
  130. User.email,
  131. User.avatar,
  132. User.is_authenticated,
  133. User.is_active,
  134. User.is_anonymous,
  135. User.status,
  136. User.update_date,
  137. User.is_superuser]
  138. return list(cls.model.select(*fields)
  139. .join(User, on=((cls.model.user_id == User.id) & (cls.model.status == StatusEnum.VALID.value) & (cls.model.role != UserTenantRole.OWNER)))
  140. .where(cls.model.tenant_id == tenant_id)
  141. .dicts())
  142. @classmethod
  143. @DB.connection_context()
  144. def get_tenants_by_user_id(cls, user_id):
  145. fields = [
  146. cls.model.tenant_id,
  147. cls.model.role,
  148. User.nickname,
  149. User.email,
  150. User.avatar,
  151. User.update_date
  152. ]
  153. return list(cls.model.select(*fields)
  154. .join(User, on=((cls.model.tenant_id == User.id) & (UserTenant.user_id == user_id) & (UserTenant.status == StatusEnum.VALID.value)))
  155. .where(cls.model.status == StatusEnum.VALID.value).dicts())