您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

file2document_service.py 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 api.db.db_models import DB
  18. from api.db.db_models import File, Document, File2Document
  19. from api.db.services.common_service import CommonService
  20. from api.utils import current_timestamp, datetime_format
  21. class File2DocumentService(CommonService):
  22. model = File2Document
  23. @classmethod
  24. @DB.connection_context()
  25. def get_by_file_id(cls, file_id):
  26. objs = cls.model.select().where(cls.model.file_id == file_id)
  27. return objs
  28. @classmethod
  29. @DB.connection_context()
  30. def get_by_document_id(cls, document_id):
  31. objs = cls.model.select().where(cls.model.document_id == document_id)
  32. return objs
  33. @classmethod
  34. @DB.connection_context()
  35. def insert(cls, obj):
  36. if not cls.save(**obj):
  37. raise RuntimeError("Database error (File)!")
  38. e, obj = cls.get_by_id(obj["id"])
  39. if not e:
  40. raise RuntimeError("Database error (File retrieval)!")
  41. return obj
  42. @classmethod
  43. @DB.connection_context()
  44. def delete_by_file_id(cls, file_id):
  45. return cls.model.delete().where(cls.model.file_id == file_id).execute()
  46. @classmethod
  47. @DB.connection_context()
  48. def delete_by_document_id(cls, doc_id):
  49. return cls.model.delete().where(cls.model.document_id == doc_id).execute()
  50. @classmethod
  51. @DB.connection_context()
  52. def update_by_file_id(cls, file_id, obj):
  53. obj["update_time"] = current_timestamp()
  54. obj["update_date"] = datetime_format(datetime.now())
  55. num = cls.model.update(obj).where(cls.model.id == file_id).execute()
  56. e, obj = cls.get_by_id(cls.model.id)
  57. return obj