Skip to content

Commit

Permalink
Issue #50: renaming get_user to get_user_or_none
Browse files Browse the repository at this point in the history
also added comments
  • Loading branch information
apragacz committed May 10, 2019
1 parent 3b9b981 commit 1d2835d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
9 changes: 5 additions & 4 deletions rest_registration/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from rest_framework import serializers
from rest_framework.exceptions import ValidationError

from rest_registration.exceptions import UserNotFound
from rest_registration.settings import registration_settings
from rest_registration.utils.users import (
authenticate_by_login_and_password_or_none,
Expand Down Expand Up @@ -52,7 +51,11 @@ def get_email(self):
class DefaultSendResetPasswordLinkSerializer(serializers.Serializer):
login = serializers.CharField(required=True)

def get_user(self):
def get_user_or_none(self):
"""
Return user if login matches.
Return ``None`` otherwise.
"""
login = self.validated_data['login']
user = None
for login_field in get_user_login_fields():
Expand All @@ -61,8 +64,6 @@ def get_user(self):
if user:
break

if not user:
raise UserNotFound()
return user


Expand Down
5 changes: 4 additions & 1 deletion rest_registration/api/views/reset_password.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
api_view_serializer_class,
api_view_serializer_class_getter
)
from rest_registration.exceptions import UserNotFound
from rest_registration.notifications import send_verification_notification
from rest_registration.settings import registration_settings
from rest_registration.utils.responses import get_ok_response
Expand Down Expand Up @@ -56,7 +57,9 @@ def send_reset_password_link(request):
serializer_class = registration_settings.SEND_RESET_PASSWORD_LINK_SERIALIZER_CLASS # noqa: E501
serializer = serializer_class(data=request.data)
serializer.is_valid(raise_exception=True)
user = serializer.get_user()
user = serializer.get_user_or_none()
if not user:
raise UserNotFound()
signer = ResetPasswordSigner({
'user_id': user.pk,
}, request=request)
Expand Down
5 changes: 5 additions & 0 deletions rest_registration/settings_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ def __new__(cls, name, *, default=None, help=None, import_string=False):
default='rest_registration.api.serializers.DefaultSendResetPasswordLinkSerializer', # noqa: E501,
import_string=True,
help=dedent("""\
The serializer used by :ref:`send-reset-password-link-view`
endpoint. You can use your custom serializer
to customise validation logic and perform additonal checks.
Please remember that it should implement ``get_user_or_none``
method which is used to obtain the user matching the criteria.
""")
),
Field('RESET_PASSWORD_VERIFICATION_ENABLED', default=True),
Expand Down

0 comments on commit 1d2835d

Please sign in to comment.