fixed reset password view
This commit is contained in:
parent
3c7fc7a436
commit
6d3b7b1921
|
|
@ -222,6 +222,17 @@ class ResetPasswordToken(PlatformMixin, ProjectBaseMixin):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "Password reset token for user {user}".format(user=self.user)
|
return "Password reset token for user {user}".format(user=self.user)
|
||||||
|
|
||||||
|
def save(self, *args, **kwargs):
|
||||||
|
"""Override save method"""
|
||||||
|
if not self.expiry_datetime:
|
||||||
|
self.expiry_datetime = (
|
||||||
|
timezone.now() +
|
||||||
|
timezone.timedelta(hours=self.get_resetting_token_expiration)
|
||||||
|
)
|
||||||
|
if not self.key:
|
||||||
|
self.key = self.generate_token
|
||||||
|
return super(ResetPasswordToken, self).save(*args, **kwargs)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def get_resetting_token_expiration(self):
|
def get_resetting_token_expiration(self):
|
||||||
"""Get resetting token expiration"""
|
"""Get resetting token expiration"""
|
||||||
|
|
@ -256,14 +267,3 @@ class ResetPasswordToken(PlatformMixin, ProjectBaseMixin):
|
||||||
"""Overdue instance"""
|
"""Overdue instance"""
|
||||||
self.expiry_datetime = timezone.now()
|
self.expiry_datetime = timezone.now()
|
||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
|
||||||
"""Override save method"""
|
|
||||||
if not self.expiry_datetime:
|
|
||||||
self.expiry_datetime = (
|
|
||||||
timezone.now() +
|
|
||||||
timezone.timedelta(hours=self.get_resetting_token_expiration)
|
|
||||||
)
|
|
||||||
if not self.key:
|
|
||||||
self.key = self.generate_token
|
|
||||||
return super(ResetPasswordToken, self).save(*args, **kwargs)
|
|
||||||
|
|
|
||||||
|
|
@ -118,7 +118,7 @@ class ChangeEmailSerializer(serializers.ModelSerializer):
|
||||||
def validate_email(self, value):
|
def validate_email(self, value):
|
||||||
"""Validate email value"""
|
"""Validate email value"""
|
||||||
if value == self.instance.email:
|
if value == self.instance.email:
|
||||||
# todo: added custom exception
|
# todo: add custom exception
|
||||||
raise serializers.ValidationError()
|
raise serializers.ValidationError()
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
@ -126,7 +126,7 @@ class ChangeEmailSerializer(serializers.ModelSerializer):
|
||||||
"""Override validate method"""
|
"""Override validate method"""
|
||||||
email_confirmed = self.instance.email_confirmed
|
email_confirmed = self.instance.email_confirmed
|
||||||
if not email_confirmed:
|
if not email_confirmed:
|
||||||
# todo: added custom exception
|
# todo: add custom exception
|
||||||
raise serializers.ValidationError()
|
raise serializers.ValidationError()
|
||||||
return attrs
|
return attrs
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
"""Serializers for account web"""
|
"""Serializers for account web"""
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.contrib.auth import password_validation as password_validators
|
from django.contrib.auth import password_validation as password_validators
|
||||||
|
from django.db.models import Q
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
|
|
||||||
from account import models
|
from account import models
|
||||||
|
|
@ -10,36 +11,38 @@ from utils import exceptions as utils_exceptions
|
||||||
|
|
||||||
class PasswordResetSerializer(serializers.ModelSerializer):
|
class PasswordResetSerializer(serializers.ModelSerializer):
|
||||||
"""Serializer from model PasswordReset"""
|
"""Serializer from model PasswordReset"""
|
||||||
email = serializers.EmailField(required=False,
|
username_or_email = serializers.CharField(required=False,
|
||||||
write_only=True)
|
write_only=True,)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
"""Meta class"""
|
"""Meta class"""
|
||||||
model = models.ResetPasswordToken
|
model = models.ResetPasswordToken
|
||||||
fields = (
|
fields = (
|
||||||
'email',
|
'username_or_email',
|
||||||
)
|
)
|
||||||
|
|
||||||
def validate(self, attrs):
|
def validate(self, attrs):
|
||||||
"""Override validate method"""
|
"""Override validate method"""
|
||||||
user = self.context.get('request').user
|
user = self.context.get('request').user
|
||||||
email = attrs.get('email')
|
username_or_email = attrs.pop('username_or_email')
|
||||||
|
|
||||||
if not user.is_anonymous:
|
if user.is_anonymous:
|
||||||
attrs['user'] = user
|
|
||||||
else:
|
|
||||||
# Check user in DB
|
# Check user in DB
|
||||||
user_qs = models.User.objects.filter(email=email)
|
user_qs = models.User.objects.filter(Q(email=username_or_email) |
|
||||||
|
Q(username=username_or_email))
|
||||||
if user_qs.exists():
|
if user_qs.exists():
|
||||||
attrs['user'] = user_qs.first()
|
attrs['user'] = user_qs.first()
|
||||||
else:
|
else:
|
||||||
raise utils_exceptions.UserNotFoundError()
|
raise utils_exceptions.UserNotFoundError()
|
||||||
|
else:
|
||||||
|
attrs['user'] = user
|
||||||
return attrs
|
return attrs
|
||||||
|
|
||||||
def create(self, validated_data, *args, **kwargs):
|
def create(self, validated_data, *args, **kwargs):
|
||||||
"""Override create method"""
|
"""Override create method"""
|
||||||
user = validated_data.pop('user')
|
user = validated_data.pop('user')
|
||||||
ip_address = self.context.get('request').META.get('REMOTE_ADDR')
|
ip_address = self.context.get('request').META.get('REMOTE_ADDR')
|
||||||
|
|
||||||
obj = models.ResetPasswordToken.objects.create(
|
obj = models.ResetPasswordToken.objects.create(
|
||||||
user=user,
|
user=user,
|
||||||
ip_address=ip_address,
|
ip_address=ip_address,
|
||||||
|
|
|
||||||
|
|
@ -156,7 +156,7 @@ class FormPasswordResetConfirmView(PasswordContextMixin, FormView):
|
||||||
def form_valid(self, form):
|
def form_valid(self, form):
|
||||||
# Saving form
|
# Saving form
|
||||||
form.save()
|
form.save()
|
||||||
# Pop token
|
# Pop session token
|
||||||
del self.request.session[self.INTERNAL_RESET_SESSION_TOKEN]
|
del self.request.session[self.INTERNAL_RESET_SESSION_TOKEN]
|
||||||
return super().form_valid(form)
|
return super().form_valid(form)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
|
|
||||||
{% trans "Please confirm your email address to complete the registration:" %}
|
{% trans "Please confirm your email address to complete the registration:" %}
|
||||||
{% block signup_confirm %}
|
{% block signup_confirm %}
|
||||||
http://{{ domain_uri }}{% url 'auth:signup-confirm' uidb64=uid token=token %}
|
http://{{ domain_uri }}{% url 'auth:signup-confirm' uidb64=uidb64 token=token %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% trans "Thanks for using our site!" %}
|
{% trans "Thanks for using our site!" %}
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,6 @@ geoip2==2.9.0
|
||||||
django-phonenumber-field[phonenumbers]==2.1.0
|
django-phonenumber-field[phonenumbers]==2.1.0
|
||||||
|
|
||||||
# auth socials
|
# auth socials
|
||||||
djangorestframework-oauth
|
|
||||||
django-rest-framework-social-oauth2==1.1.0
|
django-rest-framework-social-oauth2==1.1.0
|
||||||
|
|
||||||
django-extensions==2.2.1
|
django-extensions==2.2.1
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user