Skip to content

Commit 634c789

Browse files
author
Sourcery AI
committed
'Refactored by Sourcery'
1 parent c5bd09e commit 634c789

File tree

3 files changed

+10
-17
lines changed

3 files changed

+10
-17
lines changed

drf_user/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ def update_user_settings() -> dict:
4444
4545
Author: Himanshu Shankar (https://himanshus.com)
4646
"""
47-
custom_settings = getattr(settings, "USER_SETTINGS", None)
48-
49-
if custom_settings:
47+
if custom_settings := getattr(settings, "USER_SETTINGS", None):
5048
if not isinstance(custom_settings, dict):
5149
raise TypeError("USER_SETTING must be a dict.")
5250

drf_user/serializers.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -190,19 +190,17 @@ def validate(self, attrs: dict) -> dict:
190190
else:
191191
attrs["prop"] = EMAIL
192192

193-
user = self.get_user(attrs.get("prop"), attrs.get("destination"))
193+
if user := self.get_user(attrs.get("prop"), attrs.get("destination")):
194+
attrs["email"] = user.email
195+
attrs["user"] = user
194196

195-
if not user:
197+
else:
196198
if attrs["is_login"]:
197199
raise NotFound(_("No user exists with provided details"))
198200
if "email" not in attrs.keys() and "verify_otp" not in attrs.keys():
199201
raise serializers.ValidationError(
200202
_("Email field is compulsory while verifying a non-existing user's OTP.")
201203
)
202-
else:
203-
attrs["email"] = user.email
204-
attrs["user"] = user
205-
206204
return attrs
207205

208206

@@ -328,13 +326,11 @@ def validate(self, attrs: dict) -> dict:
328326
"""
329327
validator = EmailValidator()
330328
validator(attrs.get("email"))
331-
user = self.get_user(attrs.get("email"))
332-
333-
if not user:
329+
if user := self.get_user(attrs.get("email")):
330+
return attrs
331+
else:
334332
raise NotFound(_("User with the provided email does not exist."))
335333

336-
return attrs
337-
338334

339335
class ImageSerializer(serializers.ModelSerializer):
340336
"""This serializer is for Image Upload API.

drf_user/utils.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ def get_client_ip(request: HttpRequest) -> Optional[str]:
4040
-------
4141
ip: str or None
4242
"""
43-
x_forwarded_for = request.META.get("HTTP_X_FORWARDED_FOR")
44-
if x_forwarded_for:
43+
if x_forwarded_for := request.META.get("HTTP_X_FORWARDED_FOR"):
4544
return x_forwarded_for.split(",")[0]
4645
else:
4746
return request.META.get("REMOTE_ADDR")
@@ -251,7 +250,7 @@ def is_mobile_valid(mobile: str) -> bool:
251250
>>> print(is_mobile_valid('9999999999'))
252251
True
253252
"""
254-
match = re.match(r"^[6-9]\d{9}$", str(mobile))
253+
match = re.match(r"^[6-9]\d{9}$", mobile)
255254
if match is None:
256255
raise ValidationError("Enter a valid mobile number.")
257256
return True

0 commit comments

Comments
 (0)