diff --git a/drf_user/__init__.py b/drf_user/__init__.py index e64bf3c..b4ce697 100644 --- a/drf_user/__init__.py +++ b/drf_user/__init__.py @@ -44,9 +44,7 @@ def update_user_settings() -> dict: Author: Himanshu Shankar (https://himanshus.com) """ - custom_settings = getattr(settings, "USER_SETTINGS", None) - - if custom_settings: + if custom_settings := getattr(settings, "USER_SETTINGS", None): if not isinstance(custom_settings, dict): raise TypeError("USER_SETTING must be a dict.") diff --git a/drf_user/serializers.py b/drf_user/serializers.py index 0a1825f..10d8acb 100644 --- a/drf_user/serializers.py +++ b/drf_user/serializers.py @@ -190,19 +190,17 @@ def validate(self, attrs: dict) -> dict: else: attrs["prop"] = EMAIL - user = self.get_user(attrs.get("prop"), attrs.get("destination")) + if user := self.get_user(attrs.get("prop"), attrs.get("destination")): + attrs["email"] = user.email + attrs["user"] = user - if not user: + else: if attrs["is_login"]: raise NotFound(_("No user exists with provided details")) if "email" not in attrs.keys() and "verify_otp" not in attrs.keys(): raise serializers.ValidationError( _("Email field is compulsory while verifying a non-existing user's OTP.") ) - else: - attrs["email"] = user.email - attrs["user"] = user - return attrs @@ -328,13 +326,11 @@ def validate(self, attrs: dict) -> dict: """ validator = EmailValidator() validator(attrs.get("email")) - user = self.get_user(attrs.get("email")) - - if not user: + if user := self.get_user(attrs.get("email")): + return attrs + else: raise NotFound(_("User with the provided email does not exist.")) - return attrs - class ImageSerializer(serializers.ModelSerializer): """This serializer is for Image Upload API. diff --git a/drf_user/utils.py b/drf_user/utils.py index 1292221..c2b57cd 100644 --- a/drf_user/utils.py +++ b/drf_user/utils.py @@ -40,8 +40,7 @@ def get_client_ip(request: HttpRequest) -> Optional[str]: ------- ip: str or None """ - x_forwarded_for = request.META.get("HTTP_X_FORWARDED_FOR") - if x_forwarded_for: + if x_forwarded_for := request.META.get("HTTP_X_FORWARDED_FOR"): return x_forwarded_for.split(",")[0] else: return request.META.get("REMOTE_ADDR") @@ -251,7 +250,7 @@ def is_mobile_valid(mobile: str) -> bool: >>> print(is_mobile_valid('9999999999')) True """ - match = re.match(r"^[6-9]\d{9}$", str(mobile)) + match = re.match(r"^[6-9]\d{9}$", mobile) if match is None: raise ValidationError("Enter a valid mobile number.") return True