workflow_custom_notice_service.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 CustomNotice
  5. from service.base_service import BaseService
  6. from service.common.log_service import auto_log
  7. class WorkflowCustomNoticeService(BaseService):
  8. """
  9. 工作流通知服务
  10. """
  11. def __init__(self):
  12. pass
  13. @classmethod
  14. @auto_log
  15. def get_notice_list(cls, query_value, page, per_page):
  16. """
  17. 获取通知列表
  18. :param query_value:
  19. :param page:
  20. :param per_page:
  21. :return:
  22. """
  23. query_params = Q(is_deleted=False)
  24. if query_value:
  25. query_params &= Q(name__contains=query_value) | Q(description__contains=query_value)
  26. custom_notice_querset = CustomNotice.objects.filter(query_params).order_by('id')
  27. paginator = Paginator(custom_notice_querset, per_page)
  28. try:
  29. custom_notice_result_paginator = paginator.page(page)
  30. except PageNotAnInteger:
  31. custom_notice_result_paginator = paginator.page(1)
  32. except EmptyPage:
  33. # If page is out of range (e.g. 9999), deliver last page of results
  34. custom_notice_result_paginator = paginator.page(paginator.num_pages)
  35. custom_notice_result_object_list = custom_notice_result_paginator.object_list
  36. custom_notice_result_restful_list = []
  37. for custom_notice_result_object in custom_notice_result_object_list:
  38. custom_notice_result_restful_list.append(dict(id=custom_notice_result_object.id, name=custom_notice_result_object.name,
  39. description=custom_notice_result_object.description,
  40. creator=custom_notice_result_object.creator,
  41. script=custom_notice_result_object.script.name,
  42. title_template=custom_notice_result_object.title_template,
  43. content_template=custom_notice_result_object.content_template,
  44. gmt_created=str(custom_notice_result_object.gmt_created)[:19]))
  45. return custom_notice_result_restful_list, dict(per_page=per_page, page=page, total=paginator.count)
  46. @classmethod
  47. @auto_log
  48. def add_custom_notice(cls, name, script, description, title_template, content_template , creator):
  49. """
  50. 新增通知脚本
  51. :param name:
  52. :param script:
  53. :param description:
  54. :param title_template:
  55. :param content_template:
  56. :param creator:
  57. :return:
  58. """
  59. script_obj = CustomNotice(name=name, script=script, description=description, title_template=title_template, content_template=content_template , creator=creator)
  60. script_obj.save()
  61. return True, script_obj.id
  62. @classmethod
  63. @auto_log
  64. def edit_custom_notice(cls, id, name, script, description, title_template, content_template):
  65. """
  66. 编辑通知脚本
  67. :param name:
  68. :param saved_name:
  69. :param description:
  70. :param title_template:
  71. :param content_template:
  72. :return:
  73. """
  74. custom_notice_obj = CustomNotice.objects.filter(id=id, is_deleted=0)
  75. if script:
  76. custom_notice_obj.update(name=name, script=script, description=description, title_template=title_template, content_template=content_template)
  77. else:
  78. custom_notice_obj.update(name=name, script=script, description=description, title_template=title_template, content_template=content_template)
  79. return True, custom_notice_obj.first().id
  80. @classmethod
  81. @auto_log
  82. def del_custom_notice(cls, id):
  83. """
  84. 删除脚本
  85. :id:
  86. :return:
  87. """
  88. custom_notice_obj = CustomNotice.objects.filter(id=id, is_deleted=0)
  89. if custom_notice_obj:
  90. custom_notice_obj.update(is_deleted=True)
  91. return True, ''
  92. else:
  93. return False, 'the record is not exist or has been deleted'