Skip to content

Commit

Permalink
'Refactored by Sourcery'
Browse files Browse the repository at this point in the history
  • Loading branch information
Sourcery AI committed Oct 21, 2022
1 parent c5bd09e commit 634c789
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 17 deletions.
4 changes: 1 addition & 3 deletions drf_user/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")

Expand Down
18 changes: 7 additions & 11 deletions drf_user/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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.
Expand Down
5 changes: 2 additions & 3 deletions drf_user/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 634c789

Please sign in to comment.