Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

user_service.py 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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, get_format_time, 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_by_user_id(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.parser_ids,
  87. UserTenant.role]
  88. return list(cls.model.select(*fields)
  89. .join(UserTenant, on=((cls.model.id == UserTenant.tenant_id) & (UserTenant.user_id == user_id) & (UserTenant.status == StatusEnum.VALID.value)))
  90. .where(cls.model.status == StatusEnum.VALID.value).dicts())
  91. @classmethod
  92. @DB.connection_context()
  93. def get_joined_tenants_by_user_id(cls, user_id):
  94. fields = [
  95. cls.model.id.alias("tenant_id"),
  96. cls.model.name,
  97. cls.model.llm_id,
  98. cls.model.embd_id,
  99. cls.model.asr_id,
  100. cls.model.img2txt_id,
  101. UserTenant.role]
  102. return list(cls.model.select(*fields)
  103. .join(UserTenant, on=((cls.model.id == UserTenant.tenant_id) & (UserTenant.user_id == user_id) & (UserTenant.status == StatusEnum.VALID.value) & (UserTenant.role == UserTenantRole.NORMAL.value)))
  104. .where(cls.model.status == StatusEnum.VALID.value).dicts())
  105. @classmethod
  106. @DB.connection_context()
  107. def decrease(cls, user_id, num):
  108. num = cls.model.update(credit=cls.model.credit - num).where(
  109. cls.model.id == user_id).execute()
  110. if num == 0:
  111. raise LookupError("Tenant not found which is supposed to be there")
  112. class UserTenantService(CommonService):
  113. model = UserTenant
  114. @classmethod
  115. @DB.connection_context()
  116. def save(cls, **kwargs):
  117. if "id" not in kwargs:
  118. kwargs["id"] = get_uuid()
  119. obj = cls.model(**kwargs).save(force_insert=True)
  120. return obj