def handle(self, *args, **options): if options['truncate']: try: user = User.objects.get(username='******') user.delete() self.stdout.write("System cleared") except User.DoesNotExist: pass serializer = UserSerializer( data={ "username": "******", "email": "*****@*****.**", "passport_number": "AAAAAAAA-AAAAAAAA", "balance": 0, "password": settings.SYSTEM_PASSWORD }) if serializer.is_valid(): password = PasswordSerializer( data={"password": settings.SYSTEM_PASSWORD}) if password.is_valid(): user = serializer.save() password = password.save() user.password = password user.save() self.stdout.write("Success") else: self.stderr.write(str(serializer.errors)) else: self.stderr.write(str(serializer.errors))
def set_password(self, request, pk=None): user = self.get_object() serializer = PasswordSerializer(data=request.data) if serializer.is_valid(): user.set_password(serializer.data['password']) user.save() return Response({'status': 'password set'}) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def set_password(self, request, pk=None, **kwargs): user = self.get_object() serializer = PasswordSerializer(data=request.data, context={'current_user': request.user}) if serializer.is_valid(): user.set_password(serializer.validated_data['password']) user.save() return Response({'status': 'password set'}) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def change_password(request): user = request.user password_serializer = PasswordSerializer(data=request.data) if not password_serializer.is_valid(): return ErrorResponse(password_serializer.errors_to_text, status.HTTP_400_BAD_REQUEST) password = password_serializer.validated_data['password'] user.password = make_password(password) user.token = to_base64(user.email, password) user.save() return SuccessResponse(UserSerializer(user).data, status.HTTP_200_OK)
def signup(self, request, **kwargs): serializer = UserSerializer(data=request.data) if serializer.is_valid(): password = PasswordSerializer(data=request.data) if password.is_valid(): user = serializer.save() password = password.save() user.password = password user.save() payload = api_settings.JWT_PAYLOAD_HANDLER(user) response_data = { "user": self._get_shallow_user_info(serializer), "token": api_settings.JWT_ENCODE_HANDLER(payload) } return Response(response_data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def set_password(self, request, pk=None): if request.method in ('OPTIONS', 'HEAD'): return Response({ 'name': "TADA Users Viewset", 'description': "Method allows reset of password of a user" }) else: serializer = PasswordSerializer(data=request.data) user = User.objects.get(id=pk) if serializer.is_valid(): # set_password also hashes the password that the user will get user.set_password(serializer.data.get('new_password')) user.save() return Response({'status': 'password set'}, status=status.HTTP_200_OK) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def change_password(self, request, pk=None): """修改指定user id的密码 --- parameters: - pk desc: 用户id required: true type: string in: path """ user = get_object_or_404(self.queryset, pk=pk) # 如果修改他人密码需要superuser if user.id != request.user.id and request.user.is_superuser is False: return Response('not superuser', status=status.HTTP_403_FORBIDDEN) serializer = PasswordSerializer(data=request.data) if not serializer.is_valid(): return Response('invalid data', status=status.HTTP_400_BAD_REQUEST) serializer.update(user, serializer.validated_data) return Response('ok')
def post(self,request,user_id): data = request.data access_token = data['access_token'] check_user_id = check_access_token_to_user_id(access_token) if int(user_id) != check_user_id: return Response(status=status.HTTP_400_BAD_REQUEST) usr_obj = User.objects.get(id=user_id) serializer = PasswordSerializer(instance=usr_obj,data=data) serializer.is_valid(raise_exception=True) serializer.save() return Response(status=status.HTTP_200_OK)