views.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. import json
  2. from django.http import HttpResponse
  3. from django.views import View
  4. from schema import Schema, Regex, And, Or, Use, Optional
  5. from apps.loon_base_view import LoonBaseView
  6. from service.account.account_base_service import account_base_service_ins
  7. from service.format_response import api_response
  8. from service.ticket.ticket_base_service import ticket_base_service_ins
  9. class TicketListView(LoonBaseView):
  10. post_schema = Schema({
  11. 'workflow_id': And(int, lambda n: n != 0, error='workflow_id is needed and type should be int'),
  12. 'transition_id': And(int, lambda n: n != 0, error='transition_id is needed and type should be int'),
  13. str: object
  14. })
  15. def get(self, request, *args, **kwargs):
  16. """
  17. 获取工单列表
  18. :param request:
  19. :param args:
  20. :param kwargs:
  21. :return:
  22. """
  23. request_data = request.GET
  24. sn = request_data.get('sn', '')
  25. title = request_data.get('title', '')
  26. # username = request_data.get('username', '')
  27. username = request.META.get('HTTP_USERNAME')
  28. create_start = request_data.get('create_start', '')
  29. create_end = request_data.get('create_end', '')
  30. workflow_ids = request_data.get('workflow_ids', '')
  31. state_ids = request_data.get('state_ids', '')
  32. ticket_ids = request_data.get('ticket_ids', '')
  33. reverse = int(request_data.get('reverse', 1))
  34. per_page = int(request_data.get('per_page', 10))
  35. page = int(request_data.get('page', 1))
  36. act_state_id = request_data.get('act_state_id', '')
  37. from_admin = request_data.get('from_admin', '')
  38. creator = request_data.get('creator', '')
  39. parent_ticket_id = int(request_data.get('parent_ticket_id', 0))
  40. parent_ticket_state_id = int(request_data.get('parent_ticket_state_id', 0))
  41. # 待办,关联的,创建
  42. category = request_data.get('category')
  43. # app_name
  44. app_name = request.META.get('HTTP_APPNAME')
  45. # 未指定创建起止时间则取最近三年的记录
  46. if not(create_start or create_end):
  47. import datetime
  48. end_time = datetime.datetime.now() + datetime.timedelta(hours=1)
  49. last_year_time = datetime.datetime.now() - datetime.timedelta(days=365*3)
  50. create_start = str(last_year_time)[:19]
  51. create_end = str(end_time)[:19]
  52. flag, result = ticket_base_service_ins.get_ticket_list(
  53. sn=sn, title=title, username=username, create_start=create_start, create_end=create_end,
  54. workflow_ids=workflow_ids, state_ids=state_ids, ticket_ids=ticket_ids, category=category, reverse=reverse,
  55. per_page=per_page, page=page, app_name=app_name, act_state_id=act_state_id, from_admin=from_admin,
  56. creator=creator, parent_ticket_id=parent_ticket_id, parent_ticket_state_id=parent_ticket_state_id)
  57. if flag is not False:
  58. paginator_info = result.get('paginator_info')
  59. data = dict(value=result.get('ticket_result_restful_list'), per_page=paginator_info.get('per_page'),
  60. page=paginator_info.get('page'), total=paginator_info.get('total'))
  61. code, msg, = 0, ''
  62. else:
  63. code, data, msg = -1, {}, result
  64. return api_response(code, msg, data)
  65. def post(self, request, *args, **kwargs):
  66. """
  67. 新建工单,需要根据不同类型工单传的参数不一样
  68. :param request:
  69. :param args:
  70. :param kwargs:
  71. :return:
  72. """
  73. json_str = request.body.decode('utf-8')
  74. request_data_dict = json.loads(json_str)
  75. app_name = request.META.get('HTTP_APPNAME')
  76. request_data_dict.update(dict(username=request.META.get('HTTP_USERNAME')))
  77. # 判断是否有创建某工单的权限
  78. app_permission, msg = account_base_service_ins.app_workflow_permission_check(app_name, request_data_dict.get('workflow_id'))
  79. if not app_permission:
  80. return api_response(-1, 'APP:{} have no permission to create this workflow ticket'.format(app_name), '')
  81. flag, result = ticket_base_service_ins.new_ticket(request_data_dict, app_name)
  82. if flag:
  83. code, data = 0, {'ticket_id': result.get('new_ticket_id')}
  84. else:
  85. code, data = -1, {}
  86. return api_response(code, result, data)
  87. class TicketView(LoonBaseView):
  88. def get(self, request, *args, **kwargs):
  89. """
  90. 获取工单详情,根据用户返回不同的内容(是否有工单表单的编辑权限)
  91. :param request:
  92. :param args:
  93. :param kwargs:
  94. :return:
  95. """
  96. request_data = request.GET
  97. ticket_id = kwargs.get('ticket_id')
  98. app_name = request.META.get('HTTP_APPNAME')
  99. app_permission_check, msg = account_base_service_ins.app_ticket_permission_check(app_name, ticket_id)
  100. if not app_permission_check:
  101. return api_response(-1, msg, '')
  102. # username = request_data.get('username', '')
  103. username = request.META.get('HTTP_USERNAME')
  104. if not username:
  105. return api_response(-1, '参数不全,请提供username', '')
  106. flag, result = ticket_base_service_ins.get_ticket_detail(ticket_id, username)
  107. if flag:
  108. code, data = 0, dict(value=result)
  109. else:
  110. code, data, msg = -1, {}, result
  111. return api_response(code, msg, data)
  112. def patch(self, request, *args, **kwargs):
  113. """
  114. 处理工单
  115. :param request:
  116. :param args:
  117. :param kwargs:
  118. :return:
  119. """
  120. json_str = request.body.decode('utf-8')
  121. if not json_str:
  122. return api_response(-1, 'patch参数为空', {})
  123. request_data_dict = json.loads(json_str)
  124. ticket_id = kwargs.get('ticket_id')
  125. app_name = request.META.get('HTTP_APPNAME')
  126. request_data_dict.update(dict(username=request.META.get('HTTP_USERNAME')))
  127. app_permission_check, msg = account_base_service_ins.app_ticket_permission_check(app_name, ticket_id)
  128. if not app_permission_check:
  129. return api_response(-1, msg, {})
  130. result, msg = ticket_base_service_ins.handle_ticket(ticket_id, request_data_dict)
  131. if result or result is not False:
  132. code, data = 0, dict(value=result)
  133. else:
  134. code, data = -1, {}
  135. return api_response(code, msg, data)
  136. def delete(self, request, *args, **kwargs):
  137. """
  138. 删除工单,仅用于管理员干预处理工单,loonflow管理后台的功能
  139. :param request:
  140. :param args:
  141. :param kwargs:
  142. :return:
  143. """
  144. # 校验工单权限
  145. ticket_id = kwargs.get('ticket_id')
  146. username = request.META.get('HTTP_USERNAME')
  147. json_str = request.body.decode('utf-8')
  148. suggestion = ''
  149. if json_str:
  150. request_data_dict = json.loads(json_str)
  151. suggestion = request_data_dict.get('suggestion')
  152. flag, result = ticket_base_service_ins.ticket_admin_permission_check(ticket_id, username)
  153. if flag is False:
  154. return api_response(-1, result, {})
  155. flag, result = ticket_base_service_ins.delete_ticket(ticket_id, username, suggestion)
  156. if flag is False:
  157. return api_response(-1, result, {})
  158. else:
  159. return api_response(0, '', {})
  160. class TicketTransition(LoonBaseView):
  161. """
  162. 工单可以做的操作
  163. """
  164. def get(self, request, *args, **kwargs):
  165. request_data = request.GET
  166. ticket_id = kwargs.get('ticket_id')
  167. # username = request_data.get('username', '')
  168. username = request.META.get('HTTP_USERNAME')
  169. app_name = request.META.get('HTTP_APPNAME')
  170. app_permission_check, msg = account_base_service_ins.app_ticket_permission_check(app_name, ticket_id)
  171. if not app_permission_check:
  172. return api_response(0, msg, dict(value=[]))
  173. if not username:
  174. return api_response(-1, '参数不全,请提供username', '')
  175. flag, result = ticket_base_service_ins.get_ticket_transition(ticket_id, username)
  176. if flag is False:
  177. code, data, msg = -1, {}, result
  178. else:
  179. code, data, msg = 0, dict(value=result.get('transition_dict_list')), ''
  180. return api_response(code, msg, data)
  181. class TicketFlowlog(LoonBaseView):
  182. """
  183. 工单流转记录
  184. """
  185. def get(self, request, *args, **kwargs):
  186. request_data = request.GET
  187. ticket_id = kwargs.get('ticket_id')
  188. username = request.META.get('HTTP_USERNAME')
  189. per_page = int(request_data.get('per_page', 10))
  190. page = int(request_data.get('page', 1))
  191. ticket_data = int(request_data.get('ticket_data', 0))
  192. desc = int(request_data.get('desc', 1)) # 是否降序
  193. app_name = request.META.get('HTTP_APPNAME')
  194. app_permission_check, msg = account_base_service_ins.app_ticket_permission_check(app_name, ticket_id)
  195. if not app_permission_check:
  196. return api_response(-1, msg, '')
  197. if not username:
  198. return api_response(-1, '参数不全,请提供username', '')
  199. flag, result = ticket_base_service_ins.get_ticket_flow_log(ticket_id, username, per_page, page, ticket_data, desc)
  200. if flag is not False:
  201. paginator_info = result.get('paginator_info')
  202. data = dict(value=result.get('ticket_flow_log_restful_list'), per_page=paginator_info.get('per_page'),
  203. page=paginator_info.get('page'), total=paginator_info.get('total'))
  204. code, msg, = 0, ''
  205. else:
  206. code, data = -1, ''
  207. return api_response(code, msg, data)
  208. class TicketFlowStep(LoonBaseView):
  209. """
  210. 工单流转step: 用于显示工单当前状态的step图(线形结构,无交叉)
  211. """
  212. def get(self, request, *args, **kwargs):
  213. request_data = request.GET
  214. ticket_id = kwargs.get('ticket_id')
  215. # username = request_data.get('username', '') # 可用于权限控制
  216. username = request.META.get('HTTP_USERNAME')
  217. app_name = request.META.get('HTTP_APPNAME')
  218. app_permission_check, msg = account_base_service_ins.app_ticket_permission_check(app_name, ticket_id)
  219. if not app_permission_check:
  220. return api_response(-1, msg, '')
  221. if not username:
  222. return api_response(-1, '参数不全,请提供username', '')
  223. flag, result = ticket_base_service_ins.get_ticket_flow_step(ticket_id, username)
  224. if flag is not False:
  225. data = dict(value=result.get('state_step_dict_list'), current_state_id=result.get('current_state_id'))
  226. code, msg, = 0, ''
  227. else:
  228. code, data = -1, ''
  229. return api_response(code, msg, data)
  230. class TicketState(LoonBaseView):
  231. """
  232. 工单状态
  233. """
  234. put_schema = Schema({
  235. 'state_id': And(int, lambda n: n != 0, error='state_id is needed and type should be int'),
  236. Optional('suggestion'): str,
  237. })
  238. def put(self, request, *args, **kwargs):
  239. """
  240. 修改工单状态
  241. :param request:
  242. :param args:
  243. :param kwargs:
  244. :return:
  245. """
  246. json_str = request.body.decode('utf-8')
  247. if not json_str:
  248. return api_response(-1, 'patch参数为空', {})
  249. request_data_dict = json.loads(json_str)
  250. ticket_id = kwargs.get('ticket_id')
  251. username = request.META.get('HTTP_USERNAME')
  252. state_id = request_data_dict.get('state_id')
  253. suggestion = request_data_dict.get('suggestion', '')
  254. app_name = request.META.get('HTTP_APPNAME')
  255. # 调用来源应用是否有此工单对应工作流的权限校验
  256. app_permission_check, msg = account_base_service_ins.app_ticket_permission_check(app_name, ticket_id)
  257. if not app_permission_check:
  258. return api_response(-1, msg, '')
  259. # 强制修改工单状态需要对应工作流的管理员或者超级管理员
  260. flag, result = ticket_base_service_ins.ticket_admin_permission_check(ticket_id, username)
  261. if flag is False:
  262. return api_response(-1, result, {})
  263. flag, result = ticket_base_service_ins.update_ticket_state(ticket_id, state_id, username, suggestion)
  264. if flag is False:
  265. return api_response(-1, result, {})
  266. else:
  267. return api_response(0, '', {})
  268. class TicketsStates(LoonBaseView):
  269. def get(self, request, *args, **kwargs):
  270. """
  271. 批量获取工单状态
  272. :param request:
  273. :param args:
  274. :param kwargs:
  275. :return:
  276. """
  277. request_data = request.GET
  278. # username = request_data.get('username', '') # 可用于权限控制
  279. username = request.META.get('HTTP_USERNAME')
  280. ticket_ids = request_data.get('ticket_ids') # 逗号隔开
  281. ticket_id_list = ticket_ids.split(',')
  282. ticket_id_list = [int(ticket_id) for ticket_id in ticket_id_list]
  283. flag, result = ticket_base_service_ins.get_tickets_states_by_ticket_id_list(ticket_id_list, username)
  284. if flag:
  285. code, msg, data = 0, '', result
  286. else:
  287. code, msg, data = -1, result, ''
  288. return api_response(code, msg, data)
  289. class TicketAccept(LoonBaseView):
  290. def post(self, request, *args, **kwargs):
  291. """
  292. 接单,当工单当前处理人实际为多个人时(角色、部门、多人都有可能, 注意角色和部门有可能实际只有一人)
  293. :param request:
  294. :param args:
  295. :param kwargs:
  296. :return:
  297. """
  298. ticket_id = kwargs.get('ticket_id')
  299. username = request.META.get('HTTP_USERNAME')
  300. app_name = request.META.get('HTTP_APPNAME')
  301. app_permission_check, msg = account_base_service_ins.app_ticket_permission_check(app_name, ticket_id)
  302. if not app_permission_check:
  303. return api_response(-1, msg, '')
  304. result, msg = ticket_base_service_ins.accept_ticket(ticket_id, username)
  305. if result:
  306. code, msg, data = 0, msg, {}
  307. else:
  308. code, msg, data = -1, msg, ''
  309. return api_response(code, msg, data)
  310. class TicketDeliver(LoonBaseView):
  311. post_schema = Schema({
  312. 'target_username': And(str, lambda n: n != '', error='target_username is needed'),
  313. Optional('from_admin'): int,
  314. Optional('suggestion'): str,
  315. })
  316. def post(self, request, *args, **kwargs):
  317. """
  318. 转交操作会直接修改工单处理人,且工单状态不变,所以在使用的时候可以在前端做些提醒 避免用户把工单直接转交给下个人,从而干扰了工单的正常流转(
  319. 如用户提交了一个请假单,部门TL审批状态下,tl本来应该点击"同意",工单会自动流转到财务人员审批状态。 应该避免tl直接将工单转交给了某个财务)。这个地方后续会考虑怎么优化下,目前先在前端做提醒
  320. :param request:
  321. :param args:
  322. :param kwargs:
  323. :return:
  324. """
  325. json_str = request.body.decode('utf-8')
  326. request_data_dict = json.loads(json_str)
  327. ticket_id = kwargs.get('ticket_id')
  328. username = request.META.get('HTTP_USERNAME')
  329. target_username = request_data_dict.get('target_username', '')
  330. suggestion = request_data_dict.get('suggestion', '')
  331. from_admin = request_data_dict.get('from_admin', 0)
  332. app_name = request.META.get('HTTP_APPNAME')
  333. app_permission_check, msg = account_base_service_ins.app_ticket_permission_check(app_name, ticket_id)
  334. if not app_permission_check:
  335. return api_response(-1, msg, {})
  336. if from_admin:
  337. flag, result = ticket_base_service_ins.ticket_admin_permission_check(ticket_id, username)
  338. if flag is False:
  339. return api_response(-1, result, {})
  340. else:
  341. # 非管理员操作,校验用户是否有处理权限
  342. flag, result = ticket_base_service_ins.ticket_handle_permission_check(ticket_id, username)
  343. if flag is False:
  344. return api_response(-1, result, {})
  345. if result.get('permission') is False:
  346. return api_response(-1, result.get('msg'), {})
  347. result, msg = ticket_base_service_ins.deliver_ticket(ticket_id, username, target_username, suggestion)
  348. if result:
  349. code, msg, data = 0, msg, result
  350. else:
  351. code, msg, data = -1, msg, ''
  352. return api_response(code, msg, data)
  353. class TicketAddNode(LoonBaseView):
  354. post_schema = Schema({
  355. 'target_username': And(str, lambda n: n != '', error='target_username is needed'),
  356. Optional('suggestion'): str,
  357. })
  358. def post(self, request, *args, **kwargs):
  359. """
  360. 加签,加签操作会修改工单处理人,工单状态不表
  361. :param request:
  362. :param args:
  363. :param kwargs:
  364. :return:
  365. """
  366. json_str = request.body.decode('utf-8')
  367. request_data_dict = json.loads(json_str)
  368. ticket_id = kwargs.get('ticket_id')
  369. username = request.META.get('HTTP_USERNAME')
  370. target_username = request_data_dict.get('target_username', '')
  371. suggestion = request_data_dict.get('suggestion', '')
  372. app_name = request.META.get('HTTP_APPNAME')
  373. app_permission_check, msg = account_base_service_ins.app_ticket_permission_check(app_name, ticket_id)
  374. if not app_permission_check:
  375. return api_response(-1, msg, '')
  376. result, msg = ticket_base_service_ins.add_node_ticket(ticket_id, username, target_username, suggestion)
  377. if result:
  378. code, msg, data = 0, msg, {}
  379. else:
  380. code, msg, data = -1, msg, ''
  381. return api_response(code, msg, data)
  382. class TicketAddNodeEnd(LoonBaseView):
  383. post_schema = Schema({
  384. Optional('suggestion'): str,
  385. })
  386. def post(self, request, *args, **kwargs):
  387. """
  388. 加签处理完成,加签完成操作后工单处理人回回到之前加签发起人
  389. :param request:
  390. :param args:
  391. :param kwargs:
  392. :return:
  393. """
  394. json_str = request.body.decode('utf-8')
  395. if not json_str:
  396. return api_response(-1, 'post参数为空', {})
  397. request_data_dict = json.loads(json_str)
  398. ticket_id = kwargs.get('ticket_id')
  399. username = request.META.get('HTTP_USERNAME')
  400. suggestion = request_data_dict.get('suggestion', '')
  401. app_name = request.META.get('HTTP_APPNAME')
  402. app_permission_check, msg = account_base_service_ins.app_ticket_permission_check(app_name, ticket_id)
  403. if not app_permission_check:
  404. return api_response(-1, msg, '')
  405. result, msg = ticket_base_service_ins.add_node_ticket_end(ticket_id, username, suggestion)
  406. if result:
  407. code, msg, data = 0, msg, {}
  408. else:
  409. code, msg, data = -1, msg, ''
  410. return api_response(code, msg, data)
  411. class TicketField(LoonBaseView):
  412. def patch(self, request, *args, **kwargs):
  413. """
  414. 修改工单字段
  415. :param request:
  416. :param args:
  417. :param kwargs:
  418. :return:
  419. """
  420. json_str = request.body.decode('utf-8')
  421. if not json_str:
  422. return api_response(-1, 'post参数为空', {})
  423. request_data_dict = json.loads(json_str)
  424. ticket_id = kwargs.get('ticket_id')
  425. username = request.META.get('HTTP_USERNAME')
  426. app_name = request.META.get('HTTP_APPNAME')
  427. app_permission_check, msg = account_base_service_ins.app_ticket_permission_check(app_name, ticket_id)
  428. if not app_permission_check:
  429. return api_response(-1, msg, '')
  430. result, msg = ticket_base_service_ins.update_ticket_field_value(ticket_id, request_data_dict)
  431. if result:
  432. code, msg, data = 0, msg, {}
  433. else:
  434. code, msg, data = -1, msg, ''
  435. return api_response(code, msg, data)
  436. class TicketScriptRetry(LoonBaseView):
  437. def post(self, request, *args, **kwargs):
  438. """
  439. 重新执行工单脚本(用于脚本执行出错的情况), 也可用于hook执行失败的情况
  440. :return:
  441. """
  442. ticket_id = kwargs.get('ticket_id')
  443. username = request.META.get('HTTP_USERNAME')
  444. app_name = request.META.get('HTTP_APPNAME')
  445. app_permission_check, msg = account_base_service_ins.app_ticket_permission_check(app_name, ticket_id)
  446. if not app_permission_check:
  447. return api_response(-1, msg, '')
  448. if not username:
  449. api_response(-1, 'need arg username', '')
  450. result, msg = ticket_base_service_ins.retry_ticket_script(ticket_id, username)
  451. if result:
  452. code, msg, data = 0, 'Ticket script or hook retry start successful', {}
  453. else:
  454. code, msg, data = -1, msg, {}
  455. return api_response(code, msg, data)
  456. class TicketComment(LoonBaseView):
  457. post_schema = Schema({
  458. 'suggestion': And(str, lambda n: n != '', error='suggestion is needed'),
  459. })
  460. def post(self, request, *args, **kwargs):
  461. """
  462. 添加评论
  463. :param request:
  464. :param args:
  465. :param kwargs:
  466. :return:
  467. """
  468. json_str = request.body.decode('utf-8')
  469. if not json_str:
  470. return api_response(-1, 'post参数为空', {})
  471. request_data_dict = json.loads(json_str)
  472. ticket_id = kwargs.get('ticket_id')
  473. username = request.META.get('HTTP_USERNAME')
  474. suggestion = request_data_dict.get('suggestion', '')
  475. result, msg = ticket_base_service_ins.add_comment(ticket_id, username, suggestion)
  476. if result:
  477. code, msg, data = 0, 'add ticket comment successful', {}
  478. else:
  479. code, msg, data = -1, msg, ''
  480. return api_response(code, msg, data)
  481. class TicketHookCallBack(LoonBaseView):
  482. def post(self, request, *args, **kwargs):
  483. """
  484. 工单hook回调,用于hoot请求后,被请求方执行完任务后回调loonflow,以触发工单继续流转
  485. :param request:
  486. :param args:
  487. :param kwargs:
  488. :return:
  489. """
  490. ticket_id = kwargs.get('ticket_id')
  491. json_str = request.body.decode('utf-8')
  492. if not json_str:
  493. return api_response(-1, 'post参数为空', {})
  494. request_data_dict = json.loads(json_str)
  495. app_name = request.META.get('HTTP_APPNAME')
  496. result, msg = ticket_base_service_ins.hook_call_back(ticket_id, app_name, request_data_dict)
  497. if result:
  498. code, msg, data = 0, 'add ticket comment successful', {}
  499. else:
  500. code, msg, data = -1, msg, ''
  501. return api_response(code, msg, data)
  502. class TicketParticipantInfo(LoonBaseView):
  503. def get(self, request, *args, **kwargs):
  504. """
  505. 工单当前处理人详情,调用方后端可用获取处理人信息后提供催办等功能
  506. :param request:
  507. :param args:
  508. :param kwargs:
  509. :return:
  510. """
  511. ticket_id = kwargs.get('ticket_id')
  512. flag, msg = ticket_base_service_ins.get_ticket_participant_info(ticket_id)
  513. if flag:
  514. code, msg, data = 0, '', msg
  515. else:
  516. code, msg, data = -1, msg, {}
  517. return api_response(code, msg, data)
  518. class TicketClose(LoonBaseView):
  519. def post(self, request, *args, **kwargs):
  520. """
  521. 强制关闭工单
  522. :param request:
  523. :param args:
  524. :param kwargs:
  525. :return:
  526. """
  527. json_str = request.body.decode('utf-8')
  528. ticket_id = kwargs.get('ticket_id')
  529. request_data_dict = json.loads(json_str)
  530. username = request.META.get('HTTP_USERNAME')
  531. suggestion = request_data_dict.get('suggestion', '')
  532. flag, result = ticket_base_service_ins.close_ticket_permission_check(ticket_id, username)
  533. if flag is False:
  534. return api_response(-1, result, {})
  535. flag, msg = ticket_base_service_ins.close_ticket(ticket_id, username, suggestion)
  536. if flag:
  537. code, msg, data = 0, '', msg
  538. else:
  539. code, msg, data = -1, msg, {}
  540. return api_response(code, msg, data)
  541. class TicketsNumStatistics(LoonBaseView):
  542. def get(self, request, *args, **kwargs):
  543. """
  544. 工单个数统计
  545. :param request:
  546. :param args:
  547. :param kwargs:
  548. :return:
  549. """
  550. start_date = kwargs.get('start_date', '')
  551. end_date = kwargs.get('end_date', '')
  552. username = request.META.get('HTTP_USERNAME')
  553. flag, result = ticket_base_service_ins.get_ticket_num_statistics(start_date, end_date, username)
  554. if flag:
  555. return api_response(0, '', result.get('result_list'))
  556. else:
  557. return api_response(-1, result, {})
  558. class TicketRetreat(LoonBaseView):
  559. def post(self, request, *args, **kwargs):
  560. """
  561. 撤回工单,允许创建人在指定状态撤回工单至初始状态,状态设置中开启允许撤回
  562. :param request:
  563. :param args:
  564. :param kwargs:
  565. :return:
  566. """
  567. ticket_id = kwargs.get('ticket_id')
  568. username = request.META.get('HTTP_USERNAME')
  569. json_str = request.body.decode('utf-8')
  570. request_data_dict = json.loads(json_str)
  571. suggestion = request_data_dict.get('suggestion', '')
  572. flag, result = ticket_base_service_ins.retreat_ticket(ticket_id, username, suggestion)
  573. if flag:
  574. return api_response(0, '', {})
  575. else:
  576. return api_response(-1, result, {})
  577. class UploadFile(LoonBaseView):
  578. def post(self, request, *args, **kwargs):
  579. """
  580. 上传文件
  581. :param request:
  582. :param args:
  583. :param kwargs:
  584. :return:
  585. """
  586. flag, result = ticket_base_service_ins.upload_file(request)
  587. if flag:
  588. return api_response(0, '', result)
  589. else:
  590. return api_response(-1, result, {})