userlistview.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. }
  23. if not payload['username']:
  24. logger.error("用户名不能为空")
  25. return HttpResponse(status=500, content='用户名不能为空')
  26. if not payload['password']:
  27. logger.error("密码不能为空")
  28. return HttpResponse(status=500, content='密码不能为空')
  29. users = User.objects.filter(username=payload['username'])
  30. if users:
  31. logger.error("用户名已存在,请修改用户名重新上传")
  32. return HttpResponse(status=500, content='用户名已存在,请修改用户名重新上传')
  33. create_time = update_time = datetime.datetime.now()
  34. payload['create_time'] = create_time
  35. payload['update_time'] = update_time
  36. payload['id'] = get_id(User, 'TestLaboratory_V1_User_1')
  37. User.objects.create(**payload)
  38. return HttpResponse(status=200, content='创建成功')
  39. # user list
  40. @staticmethod
  41. @RoleControl
  42. def get(request, *args, **kwargs):
  43. username = request.GET.get('username')
  44. identify = request.GET.get('identify')
  45. is_login = request.GET.get('is_login')
  46. sort = request.GET.get('sort')
  47. user_all = User.objects.order_by('-create_time')
  48. if username:
  49. user_all = user_all.filter(username__contains=username)
  50. if identify:
  51. user_all = user_all.filter(identify=identify)
  52. if is_login:
  53. if is_login == '0':
  54. user_all = user_all.filter(is_login=False)
  55. elif is_login == '1':
  56. user_all = user_all.filter(is_login=True)
  57. if sort:
  58. user_all = user_all.order_by(sort)
  59. users_info = []
  60. for user in user_all:
  61. users_info.append({
  62. 'userid': user.id,
  63. 'username': user.username,
  64. 'name': user.name,
  65. 'identify': user.identify,
  66. 'tel': user.tel,
  67. 'is_login': user.is_login
  68. })
  69. return Response(users_info)
  70. class UserListLogin(APIView):
  71. authentication_classes = []
  72. @staticmethod
  73. def post(request, *args, **kwargs):
  74. username = request.POST.get('username')
  75. password = request.POST.get('password')
  76. user_obj = User.objects.filter(username=username, password=password)
  77. if not user_obj:
  78. logger.error("用户名或密码错误")
  79. return HttpResponse(status=500, content='用户名或密码错误')
  80. user_obj = user_obj[0]
  81. token = create_token_for_login(
  82. {'username': username, 'password': password, 'userid': user_obj.id})
  83. user_obj.is_login = True
  84. user_obj.save()
  85. user_info = {
  86. 'userid': user_obj.id,
  87. 'username': user_obj.username,
  88. 'identify': user_obj.identify,
  89. 'token': token
  90. }
  91. return Response(user_info)