workflow_transition_service.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
  2. from django.db.models import Q
  3. from apps.workflow.models import State, Transition
  4. from service.base_service import BaseService
  5. from service.common.constant_service import CONSTANT_SERVICE
  6. from service.common.log_service import auto_log
  7. class WorkflowTransitionService(BaseService):
  8. def __init__(self):
  9. pass
  10. @classmethod
  11. @auto_log
  12. def get_state_transition_queryset(cls, state_id):
  13. """
  14. 获取状态可以执行的操作
  15. :param state_id:
  16. :return:
  17. """
  18. return Transition.objects.filter(is_deleted=0, source_state_id=state_id).all(), ''
  19. @classmethod
  20. @auto_log
  21. def get_workflow_transition_by_id(cls, transition_id):
  22. """
  23. 获取transiton
  24. :param transition_id:
  25. :return:
  26. """
  27. return Transition.objects.filter(is_deleted=0, id=transition_id).first(), ''
  28. @classmethod
  29. @auto_log
  30. def get_transition_by_args(cls, arg_dict):
  31. """
  32. 获取流转
  33. :param arg_dict: 条件字典
  34. :return:
  35. """
  36. arg_dict.update(is_deleted=0)
  37. return Transition.objects.filter(**arg_dict).all(), ''
  38. @classmethod
  39. @auto_log
  40. def get_transitions_serialize_by_workflow_id(cls, workflow_id, per_page=10, page=1, query_value=''):
  41. if not workflow_id:
  42. return False, 'except workflow_id but not provided'
  43. query_params = Q(workflow_id=workflow_id, is_deleted=False)
  44. if query_value:
  45. query_params &= Q(name__contains=query_value)
  46. workflow_transitions = Transition.objects.filter(query_params)
  47. paginator = Paginator(workflow_transitions, per_page)
  48. try:
  49. workflow_transitions_result_paginator = paginator.page(page)
  50. except PageNotAnInteger:
  51. workflow_transitions_result_paginator = paginator.page(1)
  52. except EmptyPage:
  53. # If page is out of range (e.g. 9999), deliver last page of results
  54. workflow_transitions_result_paginator = paginator.page(paginator.num_pages)
  55. workflow_transitions_object_list = workflow_transitions_result_paginator.object_list
  56. workflow_transitions_restful_list = []
  57. for workflow_transitions_object in workflow_transitions_object_list:
  58. source_state_info = {}
  59. destination_state_info = {}
  60. from service.workflow.workflow_state_service import WorkflowStateService
  61. source_state_obj, msg = WorkflowStateService.get_workflow_state_by_id(workflow_transitions_object.source_state_id)
  62. destination_state_obj, msg = WorkflowStateService.get_workflow_state_by_id(workflow_transitions_object.destination_state_id)
  63. if source_state_obj:
  64. source_state_info['name'] = source_state_obj.name
  65. source_state_info['id'] = source_state_obj.id
  66. if destination_state_obj:
  67. destination_state_info['name'] = destination_state_obj.name
  68. destination_state_info['id'] = destination_state_obj.id
  69. result_dict = dict(id=workflow_transitions_object.id, name=workflow_transitions_object.name,
  70. creator=workflow_transitions_object.creator,
  71. source_state_id=workflow_transitions_object.source_state_id,
  72. source_state_info=source_state_info,
  73. destination_state_info=destination_state_info,
  74. destination_state_id=workflow_transitions_object.destination_state_id,
  75. transition_type_id=workflow_transitions_object.transition_type_id,
  76. timer=workflow_transitions_object.timer,
  77. condition_expression=workflow_transitions_object.condition_expression,
  78. attribute_type_id=workflow_transitions_object.attribute_type_id,
  79. field_require_check=workflow_transitions_object.field_require_check,
  80. alert_enable=workflow_transitions_object.alert_enable,
  81. alert_text=workflow_transitions_object.alert_text,
  82. gmt_created=str(workflow_transitions_object.gmt_created)[:19])
  83. workflow_transitions_restful_list.append(result_dict)
  84. return workflow_transitions_restful_list, dict(per_page=per_page, page=page, total=paginator.count)
  85. @classmethod
  86. @auto_log
  87. def add_workflow_transition(cls, workflow_id, name, transition_type_id, timer, source_state_id,
  88. destination_state_id, condition_expression, attribute_type_id,
  89. field_require_check, alert_enable, alert_text, creator):
  90. transition_obj = Transition(workflow_id=workflow_id, name=name, transition_type_id=transition_type_id,
  91. timer=timer, source_state_id=source_state_id,
  92. destination_state_id=destination_state_id, condition_expression=condition_expression,
  93. attribute_type_id=attribute_type_id, field_require_check=field_require_check,
  94. alert_enable=alert_enable, alert_text=alert_text, creator=creator)
  95. transition_obj.save()
  96. return transition_obj.id, ''
  97. @classmethod
  98. @auto_log
  99. def edit_workflow_transition(cls, transition_id, workflow_id, name, transition_type_id, timer, source_state_id,
  100. destination_state_id, condition_expression, attribute_type_id,
  101. field_require_check, alert_enable, alert_text):
  102. transition_queryset = Transition.objects.filter(is_deleted=0, id=transition_id)
  103. if transition_queryset:
  104. transition_queryset.update(workflow_id=workflow_id, name=name, transition_type_id=transition_type_id,
  105. timer=timer, source_state_id=source_state_id,
  106. destination_state_id=destination_state_id, condition_expression=condition_expression,
  107. attribute_type_id=attribute_type_id, field_require_check=field_require_check,
  108. alert_enable=alert_enable, alert_text=alert_text)
  109. return transition_id, ''
  110. @classmethod
  111. @auto_log
  112. def del_workflow_transition(cls, transition_id):
  113. transition_queryset = Transition.objects.filter(is_deleted=0, id=transition_id)
  114. if transition_queryset:
  115. transition_queryset.update(is_deleted=1)
  116. return transition_id, ''