def put(self, request, api_version, token, format=None): user = self.get_object(token) serializer = UserSerializer(user, data=request.data) if (serializer.is_valid()): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def signup(req): """ Create user and return """ if ('email' in req.POST): req.POST['username'] = req.POST['email'] serializer = UserSerializer(data=req.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def signup(request): """ register the user :param request: client request :return: the user's data and profile :rtype: JSONObject """ mutable = request.POST._mutable request.POST._mutable = True # make the request mutable so that I can add extra fields user_serializer = UserSerializer(data=request.data) if user_serializer.is_valid(): user = user_serializer.save() request.data['user'] = user.id request.data['username'] = user.username profile_serializer = ProfileSerializer(data=request.data) if profile_serializer.is_valid(): profile_serializer.save() data = { "user": user_serializer.data, "profile": profile_serializer.data } return Response(data, status=status.HTTP_201_CREATED) else: return Response(profile_serializer.errors, status=status.HTTP_400_BAD_REQUEST) else: request.POST._mutable = mutable # leave as you wish to find :) return Response(user_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def changePassword(req): """ change user password """ try: oldPassword = req.POST['old_password'] newPassword = req.POST['new_password'] if req.user.check_password(oldPassword): user = req.user user.set_password(newPassword) serializer = UserSerializer(user) if serializer.is_valid(): serializer.save() return HttpResponse(status=status.HTTP_204_NO_CONTENT) else: return Response(user.errors, status=status.HTTP_400_BAD_REQUEST) else: return HttpResponse(status=status.HTTP_401_UNAUTHORIZED) except MultiValueDictKeyError as e: return HttpResponse() except PermissionError as e: return HttpResponse(status=status.HTTP_401_UNAUTHORIZED)
def post(self, request, api_version, format=None): serializer = UserSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)