Esempio n. 1
0
 def post(self, request, *args, **kwargs):
     try:
         data = request.data
         if "token" not in data:
             raise ValidationError(" token field required ")
         serializer = self.get_serializer(data=data)
         serializer.is_valid()
         attrs = serializer.validate(data)
         serializer.save(attrs)
         return Response(
             create_response(
                 True, data={"message": "Email is verified successfully"}),
             status=status.HTTP_200_OK,
         )
     except ValidationError as e:
         return Response(
             create_response(
                 False,
                 err_name="BAD_REQUEST",
                 err_message=e.detail,
             ),
             status=status.HTTP_400_BAD_REQUEST,
         )
     except Exception as e:
         print(e)
         return Response(
             create_response(
                 False,
                 err_name="INTERNAL_SERVER_ERROR",
                 err_message="Something went wrong.",
             ),
             status=status.HTTP_500_INTERNAL_SERVER_ERROR,
         )
Esempio n. 2
0
 def post(self, request, *args, **kwargs):
     try:
         serializer: ResetPasswordSerializer = self.get_serializer(
             data=request.data)
         if not serializer.is_valid():
             return Response(
                 create_response(
                     False,
                     err_name="BAD_REQUEST",
                     err_message=serializer.errors,
                 ),
                 status=status.HTTP_400_BAD_REQUEST,
             )
         validate_data = serializer.validate(request.data)
         serializer.save(validate_data)
         return Response(
             create_response(
                 True,
                 data={"message": "Password has been successfully changed"},
             ),
             status=status.HTTP_200_OK,
         )
     except Exception as e:
         print(e)
         return Response(
             create_response(
                 False,
                 err_name="INTERNAL_SERVER_ERROR",
                 err_message="Something went wrong.",
             ),
             status=status.HTTP_500_INTERNAL_SERVER_ERROR,
         )
    def post(self, request, *args, **kwargs):
        data = request.data
        if "email" not in data:
            raise ValidationError("email field is required")
        try:

            serializer: ResetPasswordEmailSerializer = self.get_serializer(data=data)
            validate_data = serializer.validate(data)
            serializer.save(validate_data)
            return Response(
                create_response(
                    True,
                    data={
                        "message": "Mail has been sent successfully",
                    },
                ),
                status=status.HTTP_200_OK,
            )
        except Exception as e:
            print(e)
            return Response(
                create_response(
                    False,
                    err_name="INTERNAL_SERVER_ERROR",
                    err_message="Something went wrong.",
                ),
                status=status.HTTP_500_INTERNAL_SERVER_ERROR,
            )
Esempio n. 4
0
 def update(self, request, *args, **kwargs):
     try:
         response = super().update(request, *args, **kwargs)
         response.data = create_response(
             True,
             data={"message": "Password has been successfully changed"},
         )
         return response
     except ValidationError as e:
         return Response(
             create_response(False,
                             err_name="BAD_REQUEST",
                             err_message=e.detail),
             status=status.HTTP_400_BAD_REQUEST,
         )
     except Exception as e:
         print(e)
         return Response(
             create_response(
                 False,
                 err_name="INTERNAL_SERVER_ERROR",
                 err_message="Something went wrong.",
             ),
             status=status.HTTP_500_INTERNAL_SERVER_ERROR,
         )
Esempio n. 5
0
    def post(self, request):
        try:
            data = request.data
            if not data:
                raise ValidationError("either email or mobile_number field is required")
            user = self.get_user_by_email_auth(request=request)
        except (ValidationError, InvalidCredentials) as e:
            return Response(
                create_response(False, err_name="BAD_REQUEST", err_message=e.detail),
                status=status.HTTP_400_BAD_REQUEST,
            )
        auth_token = generate_jwt_auth_token_for_user(user)
        refresh_token = generate_jwt_refresh_token_for_user(user)

        response_data = {
            "type": "bearer",
            "auth_token": auth_token,
            "auth_expires_in": getattr(settings, "AUTH_TOKEN_EXPIRATION"),
            "refresh_token": refresh_token,
            "refresh_expires_in": getattr(settings, "REFRESH_TOKEN_EXPIRATION"),
        }
        response = Response(
            create_response(True, data=response_data),
            status=status.HTTP_200_OK,
        )
        return response
Esempio n. 6
0
    def post(self, request, *args, **kwargs):
        try:
            data = request.data
            serializer: RefreshTokenSerializer = self.get_serializer(data=data)
            try:
                serializer.is_valid(raise_exception=True)
            except ValidationError as e:
                return Response(
                    create_response(False,
                                    err_name="BAD_REQUEST",
                                    err_message=e.detail),
                    status=status.HTTP_400_BAD_REQUEST,
                )
            user = serializer.validated_data.get("user")
            auth_token = generate_jwt_auth_token_for_user(user)
            refresh_token = generate_jwt_refresh_token_for_user(user)

            response_data = {
                "auth_token":
                auth_token,
                "auth_expires_in":
                getattr(settings, "AUTH_TOKEN_EXPIRATION"),
                "refresh_token":
                refresh_token,
                "refresh_expires_in":
                getattr(settings, "REFRESH_TOKEN_EXPIRATION"),
            }

            # update last login refresh of user
            user.save()
            print(f"Successfully issued new auth token for user {user}")

            response = Response(
                create_response(True, data=response_data),
                status=status.HTTP_200_OK,
            )
            return response
        except Exception as e:
            print(e)
            return Response(
                create_response(
                    False,
                    err_name="INTERNAL_SERVER_ERROR",
                    err_message="Internal server error",
                ),
                status=status.HTTP_500_INTERNAL_SERVER_ERROR,
            )
Esempio n. 7
0
    def post(self, request):
        try:
            data = request.data
            serializer: SendLoginOTPSerializer = self.get_serializer(data=data)
            try:
                serializer.is_valid(raise_exception=True)
            except ValidationError as e:
                return Response(
                    create_response(False,
                                    err_name="BAD_REQUEST",
                                    err_message=e.detail),
                    status=status.HTTP_400_BAD_REQUEST,
                )
            mobile_number = serializer.validated_data.get("mobile")
            print(f"Trying to fetch user for mobile {mobile_number}")
            user = User.get_user_for_mobile_number(mobile_number,
                                                   is_active=True)
            if user is None:
                print(f"Not found any user for mobile {mobile_number}")
                return Response(
                    create_response(True,
                                    data={"message": "otp sent to mobile"}),
                    status=status.HTTP_200_OK,
                )
            otp_sender = BaseOtpNotification(context=None)
            otp_sender.send_verfication_code(user)

            print(f"Login Otp Notification sent successfully to mobile"
                  f" {mobile_number}")
            return Response(
                create_response(True, data={"message": "otp sent to mobile"}),
                status=status.HTTP_200_OK,
            )

        except Exception as e:
            print(e)
            return Response(
                create_response(
                    False,
                    err_name="BAD REQUEST",
                    err_message="Failed to sent "
                    "otp",
                ),
                status=status.HTTP_400_BAD_REQUEST,
            )
Esempio n. 8
0
 def post(self, request, *args, **kwargs):
     data = request.data
     serializer = self.get_serializer(data=data)
     serializer.is_valid(raise_exception=True)
     user = serializer.save()
     user.generate_email_verification_token(save=True)
     # user.send_verify_email_mail()
     response_data = user.get_signup_response()
     return Response(
         create_response(True, data=response_data),
         status=status.HTTP_201_CREATED,
     )
Esempio n. 9
0
    def get(self, request, pk=None):
        if pk:
            user = User.objects.filter(id=pk)
            serializer = UserListSerializer(
                user, many=True, context={"request": request}
            )
        else:
            data = list(User.objects.all().exclude(username=request.user))
            for user in data:
                if user.is_superuser == True:
                    data.remove(user)
            serializer = UserListSerializer(
                data, many=True, context={"request": request}
            )

        return Response(
            create_response(True, data=serializer.data), status=status.HTTP_200_OK
        )
Esempio n. 10
0
 def update(self, request, *args, **kwargs):
     kwargs["partial"] = True
     response = super().update(request, *args, **kwargs)
     response.data = create_response(True, data=response.data)
     return response
Esempio n. 11
0
 def retrieve(self, request, *args, **kwargs):
     response = super().retrieve(request, *args, **kwargs)
     response.data = create_response(True, data=response.data)
     return response