Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change password functionality added #49

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions AlgoPhantomBackend/core/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 3.1.5 on 2021-04-06 07:35

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
('quiz', '0003_auto_20210129_0400'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name='quizScore',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('score', models.IntegerField(default=0)),
('maximum_score', models.IntegerField(default=0)),
('is_cleared', models.BooleanField(default=False)),
('quiz', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='quizscore', to='quiz.quizzes')),
('user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='quizscore', to=settings.AUTH_USER_MODEL)),
],
),
]
11 changes: 8 additions & 3 deletions AlgoPhantomBackend/core/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,23 @@ def save(self,request):
adapter.save_user(request, user, self)
return user


class UserSerializer(serializers.ModelSerializer):

class Meta:
model = User
fields = ('email', 'username', 'password',)


class TokenSerializer(serializers.ModelSerializer):

class Meta:
model=Token
fields = ('key','user',)



class ChangePasswordSerializer(serializers.Serializer):
model = User

old_password = serializers.CharField(required=True)
new_password = serializers.CharField(required=True)
3 changes: 3 additions & 0 deletions AlgoPhantomBackend/core/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from django.urls import path
from .views import ChangePasswordView
from . import views

urlpatterns = [
path('',views.home,name='home'),
# Change Password
path('change-password/', ChangePasswordView.as_view(), name='change-password'),
]
39 changes: 39 additions & 0 deletions AlgoPhantomBackend/core/views.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,47 @@
from django.shortcuts import render, redirect, HttpResponse, get_object_or_404
from django.http import HttpResponseBadRequest, JsonResponse,HttpResponseRedirect
from django.contrib.auth.models import User
from rest_framework import status
from rest_framework import generics
from rest_framework.response import Response
from .serializers import ChangePasswordSerializer
from rest_framework.permissions import IsAuthenticated
# Create your views here.

def home(request):
return render(request, 'index.html')


class ChangePasswordView(generics.UpdateAPIView):
"""
An endpoint for changing password.
"""
serializer_class = ChangePasswordSerializer
model = User
permission_classes = (IsAuthenticated,)

def get_object(self, queryset=None):
obj = self.request.user
return obj

def update(self, request, *args, **kwargs):
self.object = self.get_object()
serializer = self.get_serializer(data=request.data)

if serializer.is_valid():
# Check old password
if not self.object.check_password(serializer.data.get("old_password")):
return Response({"old_password": ["Wrong password.Try again"]}, status=status.HTTP_400_BAD_REQUEST)

self.object.set_password(serializer.data.get("new_password"))
self.object.save()
response = {
'status': 'success',
'code': status.HTTP_200_OK,
'message': 'Password updated successfully',
'data': []
}

return Response(response)

return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Binary file modified AlgoPhantomBackend/db.sqlite3
Binary file not shown.