userview.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. from django.http import HttpResponse
  2. from rest_framework.response import Response
  3. from rest_framework.views import APIView
  4. from apps.user.middleware.rolecontrol import RoleControl
  5. from apps.user.models import User
  6. import logging
  7. logger = logging.getLogger('django')
  8. class UserView(APIView):
  9. authentication_classes = []
  10. @staticmethod
  11. @RoleControl
  12. def get(request, user_id, *args, **kwargs):
  13. user = User.objects.filter(id=user_id)
  14. if not user:
  15. logger.error("用户不存在")
  16. return HttpResponse(status=404, content='用户不存在')
  17. user = user[0]
  18. return Response({
  19. 'username': user.username,
  20. 'name': user.name,
  21. 'identify': user.identify,
  22. 'tel': user.tel,
  23. 'group': user.group
  24. })
  25. class UserEdit(APIView):
  26. authentication_classes = []
  27. # register
  28. @staticmethod
  29. @RoleControl
  30. def post(request, user_id, *args, **kwargs):
  31. username = request.POST.get('username')
  32. tel = request.POST.get('tel')
  33. password = request.POST.get('password')
  34. group = request.POST.get('group')
  35. if not username:
  36. logger.error("用户名不能为空")
  37. return HttpResponse(status=500, content='用户名不能为空')
  38. if not password:
  39. logger.error("密码不能为空")
  40. return HttpResponse(status=500, content='密码不能为空')
  41. user = User.objects.filter(id=user_id)
  42. if not user:
  43. logger.error("用户不存在")
  44. return HttpResponse(status=404, content='用户不存在')
  45. user = user[0]
  46. user.username = username
  47. user.password = password
  48. if tel:
  49. user.tel = tel
  50. if group:
  51. user.group = group
  52. user.save()
  53. return HttpResponse(status=200, content='修改已保存')