Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

user_service.py 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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(str(kwargs["password"]))
  51. kwargs["create_time"] = current_timestamp()
  52. kwargs["create_date"] = datetime_format(datetime.now())
  53. kwargs["update_time"] = current_timestamp()
  54. kwargs["update_date"] = datetime_format(datetime.now())
  55. obj = cls.model(**kwargs).save(force_insert=True)
  56. return obj
  57. @classmethod
  58. @DB.connection_context()
  59. def delete_user(cls, user_ids, update_user_dict):
  60. with DB.atomic():
  61. cls.model.update({"status": 0}).where(cls.model.id.in_(user_ids)).execute()
  62. @classmethod
  63. @DB.connection_context()
  64. def update_user(cls, user_id, user_dict):
  65. with DB.atomic():
  66. if user_dict:
  67. user_dict["update_time"] = current_timestamp()
  68. user_dict["update_date"] = datetime_format(datetime.now())
  69. cls.model.update(user_dict).where(cls.model.id == user_id).execute()
  70. class TenantService(CommonService):
  71. model = Tenant
  72. @classmethod
  73. @DB.connection_context()
  74. def get_by_user_id(cls, user_id):
  75. fields = [cls.model.id.alias("tenant_id"), cls.model.name, cls.model.llm_id, cls.model.embd_id, cls.model.asr_id, cls.model.img2txt_id, cls.model.parser_ids, UserTenant.role]
  76. return list(cls.model.select(*fields)\
  77. .join(UserTenant, on=((cls.model.id == UserTenant.tenant_id) & (UserTenant.user_id==user_id) & (UserTenant.status == StatusEnum.VALID.value)))\
  78. .where(cls.model.status == StatusEnum.VALID.value).dicts())
  79. @classmethod
  80. @DB.connection_context()
  81. def get_joined_tenants_by_user_id(cls, user_id):
  82. fields = [cls.model.id.alias("tenant_id"), cls.model.name, cls.model.llm_id, cls.model.embd_id, cls.model.asr_id, cls.model.img2txt_id, UserTenant.role]
  83. return list(cls.model.select(*fields)\
  84. .join(UserTenant, on=((cls.model.id == UserTenant.tenant_id) & (UserTenant.user_id==user_id) & (UserTenant.status == StatusEnum.VALID.value) & (UserTenant.role==UserTenantRole.NORMAL.value)))\
  85. .where(cls.model.status == StatusEnum.VALID.value).dicts())
  86. @classmethod
  87. @DB.connection_context()
  88. def decrease(cls, user_id, num):
  89. num = cls.model.update(credit=cls.model.credit - num).where(
  90. cls.model.id == user_id).execute()
  91. if num == 0: raise LookupError("Tenant not found which is supposed to be there")
  92. class UserTenantService(CommonService):
  93. model = UserTenant
  94. @classmethod
  95. @DB.connection_context()
  96. def save(cls, **kwargs):
  97. if "id" not in kwargs:
  98. kwargs["id"] = get_uuid()
  99. obj = cls.model(**kwargs).save(force_insert=True)
  100. return obj