workflow_runscript_service.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import json
  2. from django.db.models import Q
  3. from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
  4. from apps.workflow.models import WorkflowScript
  5. from service.base_service import BaseService
  6. from service.common.log_service import auto_log
  7. class WorkflowRunScriptService(BaseService):
  8. """
  9. 工作流执行脚本服务
  10. """
  11. def __init__(self):
  12. pass
  13. @classmethod
  14. @auto_log
  15. def get_run_script_list(cls, query_value: str, page: int, per_page: int)->tuple:
  16. """
  17. 获取执行脚本列表
  18. get run script list
  19. :param query_value:
  20. :param page:
  21. :param per_page:
  22. :return:
  23. """
  24. query_params = Q(is_deleted=False)
  25. if query_value:
  26. query_params &= Q(name__contains=query_value) | Q(description__contains=query_value)
  27. run_script_querset = WorkflowScript.objects.filter(query_params).order_by('id')
  28. paginator = Paginator(run_script_querset, per_page)
  29. try:
  30. run_script_result_paginator = paginator.page(page)
  31. except PageNotAnInteger:
  32. run_script_result_paginator = paginator.page(1)
  33. except EmptyPage:
  34. # If page is out of range (e.g. 9999), deliver last page of results
  35. run_script_result_paginator = paginator.page(paginator.num_pages)
  36. run_script_result_object_list = run_script_result_paginator.object_list
  37. run_script_result_restful_list = []
  38. for run_script_result_object in run_script_result_object_list:
  39. run_script_result_restful_list.append(dict(
  40. id=run_script_result_object.id, name=run_script_result_object.name,
  41. description=run_script_result_object.description, saved_name=run_script_result_object.saved_name.name,
  42. is_active=run_script_result_object.is_active, creator=run_script_result_object.creator,
  43. gmt_created=str(run_script_result_object.gmt_created)[:19]))
  44. return True, dict(run_script_result_restful_list=run_script_result_restful_list,
  45. paginator_info=dict(per_page=per_page, page=page, total=paginator.count))
  46. @classmethod
  47. @auto_log
  48. def add_run_script(cls, name: str, saved_name: str, description: str, is_active: int, creator: str)->tuple:
  49. """
  50. 新增工作流脚本
  51. add run script
  52. :param name:
  53. :param saved_name:
  54. :param description:
  55. :param is_active:
  56. :param creator:
  57. :return:
  58. """
  59. script_obj = WorkflowScript(name=name, saved_name=saved_name, description=description, is_active=is_active,
  60. creator=creator)
  61. script_obj.save()
  62. return True, dict(script_id=script_obj.id)
  63. @classmethod
  64. @auto_log
  65. def edit_run_script(cls, run_script_id: int, name: str, saved_name: str, description: str, is_active: int)->tuple:
  66. """
  67. 更新工作流脚本
  68. edit run script
  69. :param run_script_id:
  70. :param name:
  71. :param saved_name:
  72. :param description:
  73. :param is_active:
  74. :return:
  75. """
  76. script_obj = WorkflowScript.objects.filter(id=run_script_id, is_deleted=0)
  77. if saved_name:
  78. script_obj.update(name=name, saved_name=saved_name, description=description, is_active=is_active)
  79. else:
  80. script_obj.update(name=name, description=description, is_active=is_active)
  81. return True, {}
  82. @classmethod
  83. @auto_log
  84. def del_run_script(cls, run_script_id: int)->tuple:
  85. """
  86. 删除脚本
  87. delete run script
  88. :id:
  89. :return:
  90. """
  91. script_obj = WorkflowScript.objects.filter(id=run_script_id, is_deleted=0)
  92. if script_obj:
  93. script_obj.update(is_deleted=True)
  94. return True, ''
  95. else:
  96. return False, 'the record is not exist or has been deleted'
  97. @classmethod
  98. @auto_log
  99. def get_run_script_by_id(cls, run_script_id: int)->tuple:
  100. """
  101. 根据id获取执行脚本
  102. get script by id
  103. :param run_script_id:
  104. :return:
  105. """
  106. script_obj = WorkflowScript.objects.filter(id=run_script_id, is_deleted=0).first()
  107. return True, dict(script_obj=script_obj)
  108. workflow_run_script_service_ins = WorkflowRunScriptService()