workflow_custom_field_service.py 8.0 KB

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