Skip to content

Commit

Permalink
update knox api class and add serializers
Browse files Browse the repository at this point in the history
  • Loading branch information
msyoki committed Nov 24, 2020
1 parent 411a620 commit afca904
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 25 deletions.
Binary file modified DRESSUP/__pycache__/settings.cpython-38.pyc
Binary file not shown.
Binary file modified DRESSUP/__pycache__/urls.cpython-38.pyc
Binary file not shown.
Binary file modified dressup/__pycache__/models.cpython-38.pyc
Binary file not shown.
Binary file modified dressup/__pycache__/serializers.cpython-38.pyc
Binary file not shown.
Binary file modified dressup/__pycache__/urls.cpython-38.pyc
Binary file not shown.
Binary file modified dressup/__pycache__/views.cpython-38.pyc
Binary file not shown.
53 changes: 44 additions & 9 deletions dressup/serializers.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,61 @@
from rest_framework import serializers
from django.contrib.auth.models import User
from django.contrib.auth import authenticate
from .models import Profile,Product


# User Serializer
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('id', 'username', 'email')

# Register Serializer
class RegisterSerializer(serializers.ModelSerializer):



class CreateUserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('id', 'username', 'email', 'password')
fields = ('id', 'username', 'password')
extra_kwargs = {'password': {'write_only': True}}

def create(self, validated_data):
user = User.objects.create_user(validated_data['username'], validated_data['email'], validated_data['password'])
user = User.objects.create_user(validated_data['username'],

None,
validated_data['password'])
profile=Profile.objects.create(user=user,email=user.email,username=user)
return user

class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('id', 'username')
# User Serializer
# class UserSerializer(serializers.ModelSerializer):
# class Meta:
# model = User
# fields = ('id', 'username', 'email')

# Register Serializer
# class RegisterSerializer(serializers.ModelSerializer):
# class Meta:
# model = User
# fields = ('id', 'username', 'email', 'password')
# extra_kwargs = {'password': {'write_only': True}}

# def create(self, validated_data):
# user = User.objects.create_user(validated_data['username'], validated_data['email'], validated_data['password'])
# profile=Profile.objects.create(user=user,email=user.email,username=user)
# return user

#Login serializer
class LoginUserSerializer(serializers.Serializer):
username = serializers.CharField()
password = serializers.CharField()

def validate(self, data):
user = authenticate(**data)
if user and user.is_active:
return user
raise serializers.ValidationError("Invalid Details.")


# Change Password
class ChangePasswordSerializer(serializers.Serializer):
model = User
Expand Down
7 changes: 4 additions & 3 deletions dressup/urls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .views import RegisterAPI,LoginAPI,ChangePasswordView,ProfilesAPI,ProfileAPI,ProductsAPI,search_categoryAPI,search_productAPI,productpostAPI
from .views import RegistrationAPI, LoginAPI,UserAPI,ChangePasswordView,ProfilesAPI,ProfileAPI,ProductsAPI,search_categoryAPI,search_productAPI,productpostAPI
from knox import views as knox_views
from django.urls import path, include

Expand All @@ -13,9 +13,10 @@
path('product/<int:pk>/',views.search_productAPI ,name='profile'),
path('newproduct/', productpostAPI.as_view(), name='newproduct'),
path('search/<str:search_term>/',views.search_categoryAPI ,name='search_category'),
path('register/', RegisterAPI.as_view(), name='register'),
path('register/', RegistrationAPI.as_view(), name='register'),
path('login/', LoginAPI.as_view(), name='login'),
path('logout/', knox_views.LogoutView.as_view(), name='logout'),
path('user/', UserAPI.as_view()),
path('logout/', knox_views.LogoutView.as_view(), name='knox_logout'),
path('change-password/', ChangePasswordView.as_view(), name='change-password'),
path('password_reset/', include('django_rest_passwordreset.urls', namespace='password_reset')),
]
Expand Down
64 changes: 51 additions & 13 deletions dressup/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from rest_framework import generics, permissions, status
from rest_framework.response import Response
from knox.models import AuthToken
from .serializers import UserSerializer, RegisterSerializer, ChangePasswordSerializer,ProfileSerializer,ProductSerializer,PostSerializer
from rest_framework.authtoken.serializers import AuthTokenSerializer
from .serializers import CreateUserSerializer, UserSerializer,LoginUserSerializer, ChangePasswordSerializer,ProfileSerializer,ProductSerializer,PostSerializer
# from rest_framework.authtoken.serializers import AuthTokenSerializer
from knox.views import LoginView as KnoxLoginView
from django.contrib.auth import login
from django.contrib.auth.models import User
Expand All @@ -14,30 +14,68 @@

from django.db.models import Q

from rest_framework import viewsets, permissions, generics
# Create your views here.
# Register API
class RegisterAPI(generics.GenericAPIView):
serializer_class = RegisterSerializer

class RegistrationAPI(generics.GenericAPIView):
serializer_class = CreateUserSerializer

def post(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
user = serializer.save()
return Response({
"user": UserSerializer(user, context=self.get_serializer_context()).data,
"token": AuthToken.objects.create(user)[1]
"user": UserSerializer(user, context=self.get_serializer_context()).data,
"token": AuthToken.objects.create(user)[1]
})
# class RegisterAPI(generics.GenericAPIView):
# serializer_class = RegisterSerializer

# def post(self, request, *args, **kwargs):
# serializer = self.get_serializer(data=request.data)
# serializer.is_valid(raise_exception=True)
# user = serializer.save()
# return Response({
# "user": UserSerializer(user, context=self.get_serializer_context()).data,
# "token": AuthToken.objects.create(user)[1]
# })



# Login Api
class LoginAPI(KnoxLoginView):
permission_classes = (permissions.AllowAny,)
class LoginAPI(generics.GenericAPIView):
serializer_class = LoginUserSerializer

def post(self, request, format=None):
serializer = AuthTokenSerializer(data=request.data)
def post(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
user = serializer.validated_data['user']
login(request, user)
return super(LoginAPI, self).post(request, format=None)
user = serializer.validated_data
return Response({
"user": UserSerializer(user, context=self.get_serializer_context()).data,
"token": AuthToken.objects.create(user)[1]
})


#API to shows the user data when the user successfully logs in.
class UserAPI(generics.RetrieveAPIView):
permission_classes = [permissions.IsAuthenticated, ]
serializer_class = UserSerializer

def get_object(self):
return self.request.user



# class LoginAPI(KnoxLoginView):
# permission_classes = (permissions.AllowAny,)

# def post(self, request, format=None):
# serializer = AuthTokenSerializer(data=request.data)
# serializer.is_valid(raise_exception=True)
# user = serializer.validated_data['user']
# login(request, user)
# return super(LoginAPI, self).post(request, format=None)

# Change password
class ChangePasswordView(generics.UpdateAPIView):
Expand Down

0 comments on commit afca904

Please sign in to comment.