Notes
![]() ![]() Notes - notes.io |
class PasswordResetSerializer(serializers.Serializer):
email = serializers.EmailField()
def validate_email(self, value):
if not User.objects.filter(email=value).exists():
raise serializers.ValidationError("No user is associated with this email address.")
return value
def save(self):
email = self.validated_data['email']
user = User.objects.get(email=email)
# Generate a password reset token
token = get_random_string(length=32)
PasswordResetToken.objects.create(user=user, token=token)
# Send email (you need to configure your email settings)
send_mail(
'Password Reset Request',
f'Use this token to reset your password: {token}',
'[email protected]',
[email],
fail_silently=False,
)
class PasswordResetConfirmSerializer(serializers.Serializer):
token = serializers.CharField()
new_password = serializers.CharField(write_only=True)
confirm_password = serializers.CharField(write_only=True)
def validate(self, data):
if data['new_password'] != data['confirm_password']:
raise serializers.ValidationError("Passwords do not match.")
# Validate the token and retrieve the user
try:
password_reset_token = PasswordResetToken.objects.get(token=data['token'])
user = password_reset_token.user
except PasswordResetToken.DoesNotExist:
raise serializers.ValidationError("Invalid token or user not found.")
# Optionally, you can also check if the token has expired
return data
def save(self):
# Retrieve the user using the token
token = self.validated_data['token']
password_reset_token = PasswordResetToken.objects.get(token=token)
user = password_reset_token.user
user.set_password(self.validated_data['new_password'])
user.save()
# Optionally, delete the token after use
password_reset_token.delete()
views.py
class PasswordResetView(generics.GenericAPIView):
serializer_class = PasswordResetSerializer
def post(self, request):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
serializer.save()
return Response({"message": "Password reset email sent."}, status=200)
class PasswordResetConfirmView(generics.GenericAPIView):
serializer_class = PasswordResetConfirmSerializer
def post(self, request):
serializer = self.get_serializer(data=request.data)
try:
serializer.is_valid(raise_exception=True)
serializer.save()
return Response({"message": "Password has been reset successfully."}, status=status.HTTP_200_OK)
except Exception as e:
return Response({"error": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team