Skip to content

Commit

Permalink
feat(deps): remove drfaddons dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
sumit4613 committed Oct 8, 2022
1 parent 7ea5369 commit ea24813
Show file tree
Hide file tree
Showing 14 changed files with 400 additions and 292 deletions.
7 changes: 1 addition & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@ repos:
rev: 5.0.4
hooks:
- id: flake8
args: [--max-line-length=88]

- repo: https://github.com/asottile/reorder_python_imports
rev: v3.8.3
hooks:
- id: reorder-python-imports
args: [--max-line-length=100]

- repo: https://github.com/econchick/interrogate
rev: 1.5.0
Expand Down
11 changes: 5 additions & 6 deletions drf_user/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,16 @@ def update_user_settings() -> dict:
user_settings[key] = value
elif key == "OTP":
if not isinstance(value, dict):
raise TypeError("USER_SETTING attribute OTP must be a" " dict.")
raise TypeError("USER_SETTING attribute OTP must be a dict.")
for otp_key, otp_value in value.items():
user_settings["OTP"][otp_key] = otp_value
elif key == "REGISTRATION":
if isinstance(value, dict):
for reg_key, reg_value in value.items():
user_settings["REGISTRATION"][reg_key] = reg_value
else:
if not isinstance(value, dict):
raise TypeError(
"USER_SETTING attribute REGISTRATION" " must be a dict."
"USER_SETTING attribute REGISTRATION must be a dict."
)
for reg_key, reg_value in value.items():
user_settings["REGISTRATION"][reg_key] = reg_value
if user_settings["REGISTRATION"]["SEND_MAIL"]:
if not getattr(settings, "EMAIL_HOST", None):
raise ValueError(
Expand Down
16 changes: 16 additions & 0 deletions drf_user/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
All constants used in the system.
"""

EMAIL: str = "E"
MOBILE: str = "M"
DESTINATION_CHOICES: list = [(EMAIL, "EMail Address"), (MOBILE, "Mobile Number")]


class CoreConstants:
"""Core Constants"""

EMAIL_PROP: str = "E"
MOBILE_PROP: str = "M"
EMAIL_STR: str = "email"
MOBILE_STR: str = "mobile"
7 changes: 3 additions & 4 deletions drf_user/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
from django.utils.text import gettext_lazy as _

from drf_user.managers import UserManager
from drf_user.variables import DESTINATION_CHOICES
from drf_user.variables import EMAIL
from drf_user.constants import DESTINATION_CHOICES, EMAIL


class Role(Group):
Expand Down Expand Up @@ -86,7 +85,7 @@ def get_full_name(self) -> str:
def __str__(self):
"""String representation of model"""

return str(self.name) + " | " + str(self.username)
return f"{str(self.name)} | {str(self.username)}"


class AuthTransaction(models.Model):
Expand Down Expand Up @@ -118,7 +117,7 @@ class AuthTransaction(models.Model):
def __str__(self):
"""String representation of model"""

return str(self.created_by.name) + " | " + str(self.created_by.username)
return f"{str(self.created_by.name)} | {str(self.created_by.username)}"

class Meta:
"""Passing model metadata"""
Expand Down
41 changes: 20 additions & 21 deletions drf_user/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
from drf_user import user_settings
from drf_user.models import User
from drf_user.utils import check_validation
from drf_user.variables import EMAIL
from drf_user.variables import MOBILE
from drf_user.constants import EMAIL, MOBILE


class UserSerializer(serializers.ModelSerializer):
Expand Down Expand Up @@ -203,8 +202,7 @@ def validate(self, attrs: dict) -> dict:
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."
"Email field is compulsory while verifying a non-existing user's OTP."
)
)
else:
Expand Down Expand Up @@ -250,26 +248,27 @@ def get_user(email: str, mobile: str):
except User.DoesNotExist:
try:
user = User.objects.get(mobile=mobile)
except User.DoesNotExist:
user = None
except User.DoesNotExist as e:
raise NotFound(
_(f"No user exists either for email={email} or mobile={mobile}")
) from e

if user:
if user.email != email:
raise serializers.ValidationError(
_(
"Your account is registered with {mobile} does not has "
"{email} as registered email. Please login directly via "
"OTP with your mobile.".format(mobile=mobile, email=email)
)
if user.email != email:
raise serializers.ValidationError(
_(
"Your account is registered with {mobile} does not has "
"{email} as registered email. Please login directly via "
"OTP with your mobile.".format(mobile=mobile, email=email)
)
if user.mobile != mobile:
raise serializers.ValidationError(
_(
"Your account is registered with {email} does not has "
"{mobile} as registered mobile. Please login directly via "
"OTP with your email.".format(mobile=mobile, email=email)
)
)
if user.mobile != mobile:
raise serializers.ValidationError(
_(
"Your account is registered with {email} does not has "
"{mobile} as registered mobile. Please login directly via "
"OTP with your email.".format(mobile=mobile, email=email)
)
)
return user

def validate(self, attrs: dict) -> dict:
Expand Down
15 changes: 8 additions & 7 deletions drf_user/signals/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
from django.db.models.signals import post_save
from django.dispatch import receiver

User = get_user_model()

@receiver(post_save, sender=get_user_model())
def post_register(sender, instance: get_user_model(), created, **kwargs):

@receiver(post_save, sender=User)
def post_register(sender, instance: User, created: bool, **kwargs):
"""Sends mail/message to users after registeration
Parameters
Expand All @@ -19,21 +21,20 @@ def post_register(sender, instance: get_user_model(), created, **kwargs):

from drf_user import user_settings

from drfaddons.utils import send_message
from drf_user.utils import send_message

if created:
if user_settings["REGISTRATION"]["SEND_MAIL"]:
send_message(
message=user_settings["REGISTRATION"]["TEXT_MAIL_BODY"],
subject=user_settings["REGISTRATION"]["MAIL_SUBJECT"],
recip=[instance.email],
recip_email=[instance.email],
recip_email=instance.email,
html_message=user_settings["REGISTRATION"]["HTML_MAIL_BODY"],
)
if user_settings["REGISTRATION"]["SEND_MESSAGE"]:
send_message(
message=user_settings["REGISTRATION"]["SMS_BODY"],
subject=user_settings["REGISTRATION"]["MAIL_SUBJECT"],
recip=[instance.mobile],
recip_email=[instance.mobile],
recip_email=instance.email,
recip_mobile=instance.mobile,
)
Loading

0 comments on commit ea24813

Please sign in to comment.