workflow_custom_field_service.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import json
  2. from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
  3. from django.db.models import Q
  4. from apps.workflow.models import CustomField
  5. from service.base_service import BaseService
  6. from service.common.log_service import auto_log
  7. class WorkflowCustomFieldService(BaseService):
  8. def __init__(self):
  9. pass
  10. @classmethod
  11. @auto_log
  12. def get_workflow_custom_field(cls, workflow_id: int):
  13. """
  14. 获取工作流的自定义字段信息
  15. update workflow custom field
  16. :param workflow_id:
  17. :return:
  18. """
  19. custom_field_queryset = CustomField.objects.filter(workflow_id=workflow_id).all()
  20. format_custom_field_dict = {}
  21. for custom_field in custom_field_queryset:
  22. if custom_field.label:
  23. label = custom_field.label
  24. else:
  25. label = '{}'
  26. format_custom_field_dict[custom_field.field_key] = dict(
  27. workflow_id=custom_field.workflow_id, field_type_id=custom_field.field_type_id,
  28. field_name=custom_field.field_name,order_id=custom_field.order_id,
  29. default_value=custom_field.default_value, description=custom_field.description,
  30. field_template=custom_field.field_template, boolean_field_display=custom_field.boolean_field_display,
  31. field_choice=custom_field.field_choice, placeholder=custom_field.placeholder, label=label)
  32. return True, format_custom_field_dict
  33. @classmethod
  34. @auto_log
  35. def get_workflow_custom_field_name_list(cls, workflow_id: int):
  36. """
  37. 获取工作流自定义字段
  38. get workflow custom field, field_key list
  39. :param workflow_id:
  40. :return:
  41. """
  42. custom_field_queryset = CustomField.objects.filter(workflow_id=workflow_id).all()
  43. return True, dict(
  44. ticket_custom_field_key_list=[custom_field.field_key for custom_field in custom_field_queryset])
  45. @classmethod
  46. @auto_log
  47. def get_workflow_custom_field_list(cls, workflow_id: int, query_value: str, page: int, per_page: int):
  48. """
  49. 获取工作流自定义字段的列表
  50. get workflow custom field restful info list
  51. :param workflow_id:
  52. :param query_value:
  53. :param page:
  54. :param per_page:
  55. :return:
  56. """
  57. query_params = Q(is_deleted=False, workflow_id=workflow_id)
  58. if query_value:
  59. query_params &= Q(field_key__contains=query_value) | Q(description__contains=query_value) \
  60. | Q(field_name__contains=query_value)
  61. workflow_custom_field_queryset = CustomField.objects.filter(query_params).order_by('id')
  62. paginator = Paginator(workflow_custom_field_queryset, per_page)
  63. try:
  64. workflow_custom_field_result_paginator = paginator.page(page)
  65. except PageNotAnInteger:
  66. workflow_custom_field_result_paginator = paginator.page(1)
  67. except EmptyPage:
  68. # If page is out of range (e.g. 9999), deliver last page of results
  69. workflow_custom_field_result_paginator = paginator.page(paginator.num_pages)
  70. workflow_custom_field_result_list = workflow_custom_field_result_paginator.object_list
  71. workflow_custom_field_result_restful_list = []
  72. for workflow_custom_field_result_object in workflow_custom_field_result_list:
  73. custom_field_dict = workflow_custom_field_result_object.get_dict()
  74. custom_field_dict['boolean_field_display'] = json.loads(custom_field_dict['boolean_field_display'])
  75. custom_field_dict['field_choice'] = json.loads(custom_field_dict['field_choice'])
  76. custom_field_dict['label'] = json.loads(custom_field_dict['label']) if custom_field_dict['label'] else {}
  77. workflow_custom_field_result_restful_list.append(custom_field_dict)
  78. return True, dict(workflow_custom_field_result_restful_list=workflow_custom_field_result_restful_list,
  79. paginator_info=dict(per_page=per_page, page=page, total=paginator.count))
  80. @classmethod
  81. @auto_log
  82. def add_record(cls, workflow_id: int, field_type_id: int, field_key: str, field_name: str, order_id: int, default_value: str, description: str, field_template: str,
  83. boolean_field_display: str, field_choice: str, label: str, creator: str):
  84. """
  85. 新增自定义字段记录
  86. add workflow custom field record
  87. :param workflow_id:
  88. :param field_type_id:
  89. :param field_key:
  90. :param field_name:
  91. :param order_id:
  92. :param default_value:
  93. :param description:
  94. :param field_template:
  95. :param boolean_field_display:
  96. :param field_choice:
  97. :param label:
  98. :param creator:
  99. :return:
  100. """
  101. custom_field_obj = CustomField(workflow_id=workflow_id, field_type_id=field_type_id, field_key=field_key,
  102. field_name=field_name, order_id=order_id, default_value=default_value,
  103. description=description, field_template=field_template,
  104. boolean_field_display=boolean_field_display,
  105. field_choice=field_choice, label=label, creator=creator)
  106. custom_field_obj.save()
  107. return True, dict(custom_field_id=custom_field_obj.id)
  108. @classmethod
  109. @auto_log
  110. def edit_record(cls, custom_field_id: int, workflow_id: int, field_type_id: int, field_key: str, field_name: str,
  111. order_id: int, default_value: str, description: str, field_template: str,
  112. boolean_field_display: str, field_choice: str, label: str)->tuple:
  113. """
  114. 修改自定义字段记录
  115. update custom field record
  116. :param custom_field_id:
  117. :param workflow_id:
  118. :param field_type_id:
  119. :param field_key:
  120. :param field_name:
  121. :param order_id:
  122. :param default_value:
  123. :param description:
  124. :param field_template:
  125. :param boolean_field_display:
  126. :param field_choice:
  127. :param label:
  128. :param creator:
  129. :return:
  130. """
  131. custom_filed_queryset = CustomField.objects.filter(id=custom_field_id)
  132. if custom_filed_queryset:
  133. custom_filed_queryset.update(workflow_id=workflow_id, field_type_id=field_type_id, field_key=field_key,
  134. field_name=field_name, order_id=order_id, default_value=default_value,
  135. description=description, field_template=field_template,
  136. boolean_field_display=boolean_field_display,
  137. field_choice=field_choice, label=label)
  138. return True, ''
  139. @classmethod
  140. @auto_log
  141. def delete_record(cls, custom_field_id: int)->tuple:
  142. """
  143. 删除记录
  144. :param custom_field_id:
  145. :return:
  146. """
  147. custom_field_queryset = CustomField.objects.filter(id=custom_field_id)
  148. if custom_field_queryset:
  149. custom_field_queryset.update(is_deleted=True)
  150. return True, ''
  151. workflow_custom_field_service_ins = WorkflowCustomFieldService()