Skip to content

Commit f2c7f44

Browse files
committed
feat(deps): remove drfaddons dependency
1 parent 765c522 commit f2c7f44

File tree

14 files changed

+400
-292
lines changed

14 files changed

+400
-292
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@ repos:
99
rev: 5.0.4
1010
hooks:
1111
- id: flake8
12-
args: [--max-line-length=88]
13-
14-
- repo: https://github.com/asottile/reorder_python_imports
15-
rev: v3.8.3
16-
hooks:
17-
- id: reorder-python-imports
12+
args: [--max-line-length=100]
1813

1914
- repo: https://github.com/econchick/interrogate
2015
rev: 1.5.0

drf_user/__init__.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,16 @@ def update_user_settings() -> dict:
5555
user_settings[key] = value
5656
elif key == "OTP":
5757
if not isinstance(value, dict):
58-
raise TypeError("USER_SETTING attribute OTP must be a" " dict.")
58+
raise TypeError("USER_SETTING attribute OTP must be a dict.")
5959
for otp_key, otp_value in value.items():
6060
user_settings["OTP"][otp_key] = otp_value
6161
elif key == "REGISTRATION":
62-
if isinstance(value, dict):
63-
for reg_key, reg_value in value.items():
64-
user_settings["REGISTRATION"][reg_key] = reg_value
65-
else:
62+
if not isinstance(value, dict):
6663
raise TypeError(
67-
"USER_SETTING attribute REGISTRATION" " must be a dict."
64+
"USER_SETTING attribute REGISTRATION must be a dict."
6865
)
66+
for reg_key, reg_value in value.items():
67+
user_settings["REGISTRATION"][reg_key] = reg_value
6968
if user_settings["REGISTRATION"]["SEND_MAIL"]:
7069
if not getattr(settings, "EMAIL_HOST", None):
7170
raise ValueError(

drf_user/constants.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
All constants used in the system.
3+
"""
4+
5+
EMAIL: str = "E"
6+
MOBILE: str = "M"
7+
DESTINATION_CHOICES: list = [(EMAIL, "EMail Address"), (MOBILE, "Mobile Number")]
8+
9+
10+
class CoreConstants:
11+
"""Core Constants"""
12+
13+
EMAIL_PROP: str = "E"
14+
MOBILE_PROP: str = "M"
15+
EMAIL_STR: str = "email"
16+
MOBILE_STR: str = "mobile"

drf_user/models.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
from django.utils.text import gettext_lazy as _
77

88
from drf_user.managers import UserManager
9-
from drf_user.variables import DESTINATION_CHOICES
10-
from drf_user.variables import EMAIL
9+
from drf_user.constants import DESTINATION_CHOICES, EMAIL
1110

1211

1312
class Role(Group):
@@ -86,7 +85,7 @@ def get_full_name(self) -> str:
8685
def __str__(self):
8786
"""String representation of model"""
8887

89-
return str(self.name) + " | " + str(self.username)
88+
return f"{str(self.name)} | {str(self.username)}"
9089

9190

9291
class AuthTransaction(models.Model):
@@ -118,7 +117,7 @@ class AuthTransaction(models.Model):
118117
def __str__(self):
119118
"""String representation of model"""
120119

121-
return str(self.created_by.name) + " | " + str(self.created_by.username)
120+
return f"{str(self.created_by.name)} | {str(self.created_by.username)}"
122121

123122
class Meta:
124123
"""Passing model metadata"""

drf_user/serializers.py

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
from drf_user import user_settings
1111
from drf_user.models import User
1212
from drf_user.utils import check_validation
13-
from drf_user.variables import EMAIL
14-
from drf_user.variables import MOBILE
13+
from drf_user.constants import EMAIL, MOBILE
1514

1615

1716
class UserSerializer(serializers.ModelSerializer):
@@ -203,8 +202,7 @@ def validate(self, attrs: dict) -> dict:
203202
if "email" not in attrs.keys() and "verify_otp" not in attrs.keys():
204203
raise serializers.ValidationError(
205204
_(
206-
"email field is compulsory while verifying a"
207-
" non-existing user's OTP."
205+
"Email field is compulsory while verifying a non-existing user's OTP."
208206
)
209207
)
210208
else:
@@ -250,26 +248,27 @@ def get_user(email: str, mobile: str):
250248
except User.DoesNotExist:
251249
try:
252250
user = User.objects.get(mobile=mobile)
253-
except User.DoesNotExist:
254-
user = None
251+
except User.DoesNotExist as e:
252+
raise NotFound(
253+
_(f"No user exists either for email={email} or mobile={mobile}")
254+
) from e
255255

256-
if user:
257-
if user.email != email:
258-
raise serializers.ValidationError(
259-
_(
260-
"Your account is registered with {mobile} does not has "
261-
"{email} as registered email. Please login directly via "
262-
"OTP with your mobile.".format(mobile=mobile, email=email)
263-
)
256+
if user.email != email:
257+
raise serializers.ValidationError(
258+
_(
259+
"Your account is registered with {mobile} does not has "
260+
"{email} as registered email. Please login directly via "
261+
"OTP with your mobile.".format(mobile=mobile, email=email)
264262
)
265-
if user.mobile != mobile:
266-
raise serializers.ValidationError(
267-
_(
268-
"Your account is registered with {email} does not has "
269-
"{mobile} as registered mobile. Please login directly via "
270-
"OTP with your email.".format(mobile=mobile, email=email)
271-
)
263+
)
264+
if user.mobile != mobile:
265+
raise serializers.ValidationError(
266+
_(
267+
"Your account is registered with {email} does not has "
268+
"{mobile} as registered mobile. Please login directly via "
269+
"OTP with your email.".format(mobile=mobile, email=email)
272270
)
271+
)
273272
return user
274273

275274
def validate(self, attrs: dict) -> dict:

drf_user/signals/handlers.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
from django.db.models.signals import post_save
44
from django.dispatch import receiver
55

6+
User = get_user_model()
67

7-
@receiver(post_save, sender=get_user_model())
8-
def post_register(sender, instance: get_user_model(), created, **kwargs):
8+
9+
@receiver(post_save, sender=User)
10+
def post_register(sender, instance: User, created: bool, **kwargs):
911
"""Sends mail/message to users after registeration
1012
1113
Parameters
@@ -19,21 +21,20 @@ def post_register(sender, instance: get_user_model(), created, **kwargs):
1921

2022
from drf_user import user_settings
2123

22-
from drfaddons.utils import send_message
24+
from drf_user.utils import send_message
2325

2426
if created:
2527
if user_settings["REGISTRATION"]["SEND_MAIL"]:
2628
send_message(
2729
message=user_settings["REGISTRATION"]["TEXT_MAIL_BODY"],
2830
subject=user_settings["REGISTRATION"]["MAIL_SUBJECT"],
29-
recip=[instance.email],
30-
recip_email=[instance.email],
31+
recip_email=instance.email,
3132
html_message=user_settings["REGISTRATION"]["HTML_MAIL_BODY"],
3233
)
3334
if user_settings["REGISTRATION"]["SEND_MESSAGE"]:
3435
send_message(
3536
message=user_settings["REGISTRATION"]["SMS_BODY"],
3637
subject=user_settings["REGISTRATION"]["MAIL_SUBJECT"],
37-
recip=[instance.mobile],
38-
recip_email=[instance.mobile],
38+
recip_email=instance.email,
39+
recip_mobile=instance.mobile,
3940
)

0 commit comments

Comments
 (0)