2
0

views.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. import json
  2. from django.shortcuts import redirect
  3. from django.utils.decorators import method_decorator
  4. from django.views import View
  5. from django.contrib.auth import authenticate, login, logout
  6. from django.contrib.auth.decorators import login_required
  7. from service.account.account_base_service import AccountBaseService
  8. from service.format_response import api_response
  9. @method_decorator(login_required, name='dispatch')
  10. class LoonUserView(View):
  11. def get(self, request, *args, **kwargs):
  12. """
  13. 获取用户列表
  14. :param request:
  15. :param args:
  16. :param kwargs:
  17. :return:
  18. """
  19. request_data = request.GET
  20. search_value = request_data.get('search_value', '')
  21. per_page = int(request_data.get('per_page', 10))
  22. page = int(request_data.get('page', 1))
  23. user_result_object_list, msg = AccountBaseService().get_user_list(search_value, page, per_page)
  24. if user_result_object_list is not False:
  25. data = dict(value=user_result_object_list, per_page=msg['per_page'], page=msg['page'], total=msg['total'])
  26. code, msg, = 0, ''
  27. else:
  28. code, data = -1, ''
  29. return api_response(code, msg, data)
  30. @method_decorator(login_required, name='dispatch')
  31. class LoonRoleView(View):
  32. def get(self, request, *args, **kwargs):
  33. """
  34. 用户角色列表
  35. :param request:
  36. :param args:
  37. :param kwargs:
  38. :return:
  39. """
  40. request_data = request.GET
  41. search_value = request_data.get('search_value', '')
  42. per_page = int(request_data.get('per_page', 10))
  43. page = int(request_data.get('page', 1))
  44. role_result_object_list, msg = AccountBaseService().get_role_list(search_value, page, per_page)
  45. if role_result_object_list is not False:
  46. data = dict(value=role_result_object_list, per_page=msg['per_page'], page=msg['page'], total=msg['total'])
  47. code, msg, = 0, ''
  48. else:
  49. code, data = -1, ''
  50. return api_response(code, msg, data)
  51. @method_decorator(login_required, name='dispatch')
  52. class LoonDeptView(View):
  53. def get(self, request, *args, **kwargs):
  54. """
  55. 用户角色列表
  56. :param request:
  57. :param args:
  58. :param kwargs:
  59. :return:
  60. """
  61. request_data = request.GET
  62. search_value = request_data.get('search_value', '')
  63. per_page = int(request_data.get('per_page', 10))
  64. page = int(request_data.get('page', 1))
  65. dept_result_object_list, msg = AccountBaseService().get_dept_list(search_value, page, per_page)
  66. if dept_result_object_list is not False:
  67. data = dict(value=dept_result_object_list, per_page=msg['per_page'], page=msg['page'], total=msg['total'])
  68. code, msg, = 0, ''
  69. else:
  70. code, data = -1, ''
  71. return api_response(code, msg, data)
  72. @method_decorator(login_required, name='dispatch')
  73. class LoonAppTokenView(View):
  74. def get(self, request, *args, **kwargs):
  75. """
  76. 调用权限列表
  77. :param request:
  78. :param args:
  79. :param kwargs:
  80. :return:
  81. """
  82. request_data = request.GET
  83. search_value = request_data.get('search_value', '')
  84. per_page = int(request_data.get('per_page', 10))
  85. page = int(request_data.get('page', 1))
  86. token_result_object_list, msg = AccountBaseService().get_token_list(search_value, page, per_page)
  87. if token_result_object_list is not False:
  88. data = dict(value=token_result_object_list, per_page=msg['per_page'], page=msg['page'], total=msg['total'])
  89. code, msg, = 0, ''
  90. else:
  91. code, data = -1, ''
  92. return api_response(code, msg, data)
  93. def post(self, request, *args, **kwargs):
  94. """
  95. 新增调用权限记录
  96. :param request:
  97. :param args:
  98. :param kwargs:
  99. :return:
  100. """
  101. json_str = request.body.decode('utf-8')
  102. if not json_str:
  103. return api_response(-1, 'post参数为空', {})
  104. request_data_dict = json.loads(json_str)
  105. app_name = request_data_dict.get('app_name', '')
  106. ticket_sn_prefix = request_data_dict.get('ticket_sn_prefix', '')
  107. workflow_ids = request_data_dict.get('workflow_ids', '')
  108. # username = request.user.username
  109. username = request.META.get('HTTP_USERNAME')
  110. flag, msg = AccountBaseService().add_token_record(app_name, ticket_sn_prefix, workflow_ids, username)
  111. if flag is False:
  112. code, data = -1, {}
  113. else:
  114. code, data = 0, {'id': msg}
  115. return api_response(code, msg, data)
  116. @method_decorator(login_required, name='dispatch')
  117. class LoonAppTokenDetailView(View):
  118. def get(self, request, *args, **kwargs):
  119. """
  120. 获取token详情
  121. :param request:
  122. :param args:
  123. :param kwargs:
  124. :return:
  125. """
  126. app_token_id = kwargs.get('app_token_id')
  127. pass
  128. def patch(self, request, *args, **kwargs):
  129. """
  130. 编辑token
  131. :param request:
  132. :param args:
  133. :param kwargs:
  134. :return:
  135. """
  136. app_token_id = kwargs.get('app_token_id')
  137. json_str = request.body.decode('utf-8')
  138. if not json_str:
  139. return api_response(-1, 'patch参数为空', {})
  140. request_data_dict = json.loads(json_str)
  141. app_name = request_data_dict.get('app_name', '')
  142. ticket_sn_prefix = request_data_dict.get('ticket_sn_prefix', '')
  143. workflow_ids = request_data_dict.get('workflow_ids', '')
  144. flag, msg = AccountBaseService.update_token_record(app_token_id, app_name, ticket_sn_prefix, workflow_ids)
  145. if flag is False:
  146. code, data = -1, {}
  147. else:
  148. code, data = 0, {}
  149. return api_response(code, msg, data)
  150. def delete(self, request, *args, **kwargs):
  151. """
  152. 删除记录
  153. :param request:
  154. :param args:
  155. :param kwargs:
  156. :return:
  157. """
  158. app_token_id = kwargs.get('app_token_id')
  159. flag, msg = AccountBaseService.del_token_record(app_token_id)
  160. if flag is False:
  161. code, data = -1, {}
  162. else:
  163. code, data = 0, {}
  164. return api_response(code, msg, data)
  165. class LoonLoginView(View):
  166. def post(self, request, *args, **kwargs):
  167. """
  168. 登录验证
  169. :param request:
  170. :param args:
  171. :param kwargs:
  172. :return:
  173. """
  174. json_str = request.body.decode('utf-8')
  175. if not json_str:
  176. return api_response(-1, 'patch参数为空', {})
  177. request_data_dict = json.loads(json_str)
  178. username = request_data_dict.get('username', '')
  179. password = request_data_dict.get('password', '')
  180. user = authenticate(username=username, password=password)
  181. if user is not None:
  182. login(request, user)
  183. return api_response(0, '', {})
  184. else:
  185. return api_response(-1, 'username or password is invalid', {})
  186. class LoonLogoutView(View):
  187. def get(self, request, *args, **kwargs):
  188. """
  189. 注销
  190. :param request:
  191. :param args:
  192. :param kwargs:
  193. :return:
  194. """
  195. logout(request)
  196. return redirect('/manage')
  197. class LoonUserRoleView(View):
  198. def get(self, request, *args, **kwargs):
  199. """
  200. 用户角色信息
  201. """
  202. user_id = kwargs.get('user_id', 0)
  203. search_value = request.GET.get('search_value', '')
  204. role_info_list, msg = AccountBaseService.get_user_role_info_by_user_id(user_id, search_value)
  205. if role_info_list is not False:
  206. data = dict(value=role_info_list, per_page=msg['per_page'], page=msg['page'], total=msg['total'])
  207. code, msg, = 0, ''
  208. else:
  209. code, data = -1, ''
  210. return api_response(code, msg, data)
  211. class LoonRoleUserView(View):
  212. def get(self, request, *args, **kwargs):
  213. """
  214. 角色的用户信息
  215. :param request:
  216. :param args:
  217. :param kwargs:
  218. :return:
  219. """
  220. role_id = kwargs.get('role_id', 0)
  221. search_value = request.GET.get('search_value', '')
  222. user_info_list, msg = AccountBaseService.get_role_user_info_by_role_id(role_id, search_value)
  223. if user_info_list is not False:
  224. data = dict(value=user_info_list, per_page=msg['per_page'], page=msg['page'], total=msg['total'])
  225. code, msg, = 0, ''
  226. else:
  227. code, data = -1, ''
  228. return api_response(code, msg, data)