userview.py 2.0 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. })
  24. class UserEdit(APIView):
  25. authentication_classes = []
  26. # register
  27. @staticmethod
  28. @RoleControl
  29. def post(request, user_id, *args, **kwargs):
  30. username = request.POST.get('username')
  31. tel = request.POST.get('tel')
  32. password = request.POST.get('password')
  33. if not username:
  34. logger.error("用户名不能为空")
  35. return HttpResponse(status=500, content='用户名不能为空')
  36. if not password:
  37. logger.error("密码不能为空")
  38. return HttpResponse(status=500, content='密码不能为空')
  39. user = User.objects.filter(id=user_id)
  40. if not user:
  41. logger.error("用户不存在")
  42. return HttpResponse(status=404, content='用户不存在')
  43. other_user = User.objects.filter(username=username)
  44. if other_user:
  45. logger.error("用户名已存在,请修改用户名")
  46. return HttpResponse(status=500, content='用户名已存在,请修改用户名')
  47. user = user[0]
  48. user.username = username
  49. user.password = password
  50. if tel:
  51. user.tel = tel
  52. user.save()
  53. return HttpResponse(status=200, content='修改已保存')