You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

search_service.py 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. from peewee import fn
  18. from api.db import StatusEnum
  19. from api.db.db_models import DB, Search, User
  20. from api.db.services.common_service import CommonService
  21. from api.utils import current_timestamp, datetime_format
  22. class SearchService(CommonService):
  23. model = Search
  24. @classmethod
  25. def save(cls, **kwargs):
  26. kwargs["create_time"] = current_timestamp()
  27. kwargs["create_date"] = datetime_format(datetime.now())
  28. kwargs["update_time"] = current_timestamp()
  29. kwargs["update_date"] = datetime_format(datetime.now())
  30. obj = cls.model.create(**kwargs)
  31. return obj
  32. @classmethod
  33. @DB.connection_context()
  34. def accessible4deletion(cls, search_id, user_id) -> bool:
  35. search = (
  36. cls.model.select(cls.model.id)
  37. .where(
  38. cls.model.id == search_id,
  39. cls.model.created_by == user_id,
  40. cls.model.status == StatusEnum.VALID.value,
  41. )
  42. .first()
  43. )
  44. return search is not None
  45. @classmethod
  46. @DB.connection_context()
  47. def get_detail(cls, search_id):
  48. fields = [
  49. cls.model.id,
  50. cls.model.avatar,
  51. cls.model.tenant_id,
  52. cls.model.name,
  53. cls.model.description,
  54. cls.model.created_by,
  55. cls.model.search_config,
  56. cls.model.update_time,
  57. User.nickname,
  58. User.avatar.alias("tenant_avatar"),
  59. ]
  60. search = (
  61. cls.model.select(*fields)
  62. .join(User, on=((User.id == cls.model.tenant_id) & (User.status == StatusEnum.VALID.value)))
  63. .where((cls.model.id == search_id) & (cls.model.status == StatusEnum.VALID.value))
  64. .first()
  65. .to_dict()
  66. )
  67. return search
  68. @classmethod
  69. @DB.connection_context()
  70. def get_by_tenant_ids(cls, joined_tenant_ids, user_id, page_number, items_per_page, orderby, desc, keywords):
  71. fields = [
  72. cls.model.id,
  73. cls.model.avatar,
  74. cls.model.tenant_id,
  75. cls.model.name,
  76. cls.model.description,
  77. cls.model.created_by,
  78. cls.model.status,
  79. cls.model.update_time,
  80. cls.model.create_time,
  81. User.nickname,
  82. User.avatar.alias("tenant_avatar"),
  83. ]
  84. query = (
  85. cls.model.select(*fields)
  86. .join(User, on=(cls.model.tenant_id == User.id))
  87. .where(((cls.model.tenant_id.in_(joined_tenant_ids)) | (cls.model.tenant_id == user_id)) & (cls.model.status == StatusEnum.VALID.value))
  88. )
  89. if keywords:
  90. query = query.where(fn.LOWER(cls.model.name).contains(keywords.lower()))
  91. if desc:
  92. query = query.order_by(cls.model.getter_by(orderby).desc())
  93. else:
  94. query = query.order_by(cls.model.getter_by(orderby).asc())
  95. count = query.count()
  96. if page_number and items_per_page:
  97. query = query.paginate(page_number, items_per_page)
  98. return list(query.dicts()), count