from django.http import HttpResponse from rest_framework.response import Response from rest_framework.views import APIView from apps.user.middleware.rolecontrol import RoleControl from apps.user.models import User import logging logger = logging.getLogger('django') class UserView(APIView): authentication_classes = [] @staticmethod @RoleControl def get(request, user_id, *args, **kwargs): user = User.objects.filter(id=user_id) if not user: logger.error("用户不存在") return HttpResponse(status=404, content='用户不存在') user = user[0] return Response({ 'username': user.username, 'name': user.name, 'identify': user.identify, 'tel': user.tel }) class UserEdit(APIView): authentication_classes = [] # register @staticmethod @RoleControl def post(request, user_id, *args, **kwargs): username = request.POST.get('username') tel = request.POST.get('tel') password = request.POST.get('password') if not username: logger.error("用户名不能为空") return HttpResponse(status=500, content='用户名不能为空') if not password: logger.error("密码不能为空") return HttpResponse(status=500, content='密码不能为空') user = User.objects.filter(id=user_id) if not user: logger.error("用户不存在") return HttpResponse(status=404, content='用户不存在') other_user = User.objects.filter(username=username) if other_user: logger.error("用户名已存在,请修改用户名") return HttpResponse(status=500, content='用户名已存在,请修改用户名') user = user[0] user.username = username user.password = password if tel: user.tel = tel user.save() return HttpResponse(status=200, content='修改已保存')