userlistview.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import datetime
  2. import logging
  3. from django.http import HttpResponse
  4. from rest_framework.response import Response
  5. from rest_framework.views import APIView
  6. from apps.user.middleware.rolecontrol import RoleControl
  7. from apps.user.models import User
  8. from utils.util_add_id import get_id
  9. from utils.util_jwt import create_token_for_login
  10. logger = logging.getLogger('django')
  11. class UserListView(APIView):
  12. authentication_classes = []
  13. # register
  14. @staticmethod
  15. def post(request, *args, **kwargs):
  16. payload = {
  17. 'username': request.POST.get('username'),
  18. 'password': request.POST.get('password'),
  19. 'name': request.POST.get('name'),
  20. 'tel': request.POST.get('tel'),
  21. 'identify': request.POST.get('identify'),
  22. 'group': request.POST.get('group')
  23. }
  24. if not payload['username']:
  25. logger.error("用户名不能为空")
  26. return HttpResponse(status=500, content='用户名不能为空')
  27. if not payload['password']:
  28. logger.error("密码不能为空")
  29. return HttpResponse(status=500, content='密码不能为空')
  30. if not payload['group']:
  31. del payload['group']
  32. users = User.objects.filter(username=payload['username'])
  33. if users:
  34. logger.error("用户名已存在,请修改用户名重新上传")
  35. return HttpResponse(status=500, content='用户名已存在,请修改用户名重新上传')
  36. create_time = update_time = datetime.datetime.now()
  37. payload['create_time'] = create_time
  38. payload['update_time'] = update_time
  39. payload['id'] = get_id(User, 'TestLaboratory_V1_User_1')
  40. new_user = User.objects.create(**payload)
  41. return HttpResponse(status=200, content='创建成功')
  42. # user list
  43. @staticmethod
  44. @RoleControl
  45. def get(request, *args, **kwargs):
  46. username = request.GET.get('username')
  47. identify = request.GET.get('identify')
  48. is_login = request.GET.get('is_login')
  49. sort = request.GET.get('sort')
  50. user_all = User.objects.order_by('-create_time')
  51. if username:
  52. user_all = user_all.filter(username__contains=username)
  53. if identify:
  54. user_all = user_all.filter(identify=identify)
  55. if is_login:
  56. if is_login == '0':
  57. user_all = user_all.filter(is_login=False)
  58. elif is_login == '1':
  59. user_all = user_all.filter(is_login=True)
  60. if sort:
  61. user_all = user_all.order_by(sort)
  62. users_info = []
  63. user_group_index = {}
  64. i = 0
  65. for user in user_all:
  66. if user.group not in user_group_index.keys():
  67. user_group_index[user.group] = i
  68. i += 1
  69. user_group = {'groupName': user.group, 'userList': [{
  70. 'userid': user.id,
  71. 'username': user.username,
  72. 'identify': user.identify,
  73. 'group': user.group,
  74. 'is_login': user.is_login
  75. }]}
  76. users_info.append(user_group)
  77. else:
  78. users_info[user_group_index[user.group]]['userList'].append({
  79. 'userid': user.id,
  80. 'username': user.username,
  81. 'identify': user.identify,
  82. 'group': user.group,
  83. 'is_login': user.is_login
  84. })
  85. return Response({'groupList': users_info})
  86. class UserListLogin(APIView):
  87. authentication_classes = []
  88. @staticmethod
  89. def post(request, *args, **kwargs):
  90. username = request.POST.get('username')
  91. password = request.POST.get('password')
  92. user_obj = User.objects.filter(username=username, password=password)
  93. if not user_obj:
  94. logger.error("用户名或密码错误")
  95. return HttpResponse(status=500, content='用户名或密码错误')
  96. user_obj = user_obj[0]
  97. token = create_token_for_login(
  98. {'username': username, 'password': password, 'userid': user_obj.id})
  99. user_obj.is_login = True
  100. user_obj.save()
  101. user_info = {
  102. 'userid': user_obj.id,
  103. 'username': user_obj.username,
  104. 'identify': user_obj.identify,
  105. 'group': user_obj.group,
  106. 'token': token
  107. }
  108. return Response(user_info)