From 01a563b9814b44d9d9e020de07f68f1a6bd166a9 Mon Sep 17 00:00:00 2001 From: kj1352 Date: Mon, 18 Jul 2022 16:59:16 +0530 Subject: [PATCH] Added employee API --- client/views.py | 12 ++++++------ employee/admin.py | 3 ++- employee/models.py | 2 +- employee/serializers.py | 8 ++++++++ employee/urls.py | 6 ++++++ employee/views.py | 17 +++++++++++++++-- project/admin.py | 3 ++- workx_backend/urls.py | 4 +++- 8 files changed, 43 insertions(+), 12 deletions(-) create mode 100644 employee/serializers.py create mode 100644 employee/urls.py diff --git a/client/views.py b/client/views.py index edbc216..ceb3e9b 100644 --- a/client/views.py +++ b/client/views.py @@ -1,13 +1,13 @@ from rest_framework.response import Response from rest_framework.views import APIView -from rest_framework.authentication import TokenAuthentication +from rest_framework.authentication import TokenAuthentication, SessionAuthentication from rest_framework.permissions import IsAuthenticated from .models import TaxType, Country, State, City, Client from .serializers import TaxTypeSerializer, CountrySerializer, StateSerializer, CitySerializer, ClientSerializer class TaxTypeList(APIView): - authentication_classes = [TokenAuthentication] + authentication_classes = [TokenAuthentication, SessionAuthentication] permission_classes = [IsAuthenticated] def get(self, request): @@ -17,7 +17,7 @@ class TaxTypeList(APIView): class CountryList(APIView): - authentication_classes = [TokenAuthentication] + authentication_classes = [TokenAuthentication, SessionAuthentication] permission_classes = [IsAuthenticated] def get(self, request): @@ -27,7 +27,7 @@ class CountryList(APIView): class StateList(APIView): - authentication_classes = [TokenAuthentication] + authentication_classes = [TokenAuthentication, SessionAuthentication] permission_classes = [IsAuthenticated] def get(self, request): @@ -37,7 +37,7 @@ class StateList(APIView): class CityList(APIView): - authentication_classes = [TokenAuthentication] + authentication_classes = [TokenAuthentication, SessionAuthentication] permission_classes = [IsAuthenticated] def get(self, request): @@ -47,7 +47,7 @@ class CityList(APIView): class ClientList(APIView): - authentication_classes = [TokenAuthentication] + authentication_classes = [TokenAuthentication, SessionAuthentication] permission_classes = [IsAuthenticated] def get(self, request): diff --git a/employee/admin.py b/employee/admin.py index 8c38f3f..2164881 100644 --- a/employee/admin.py +++ b/employee/admin.py @@ -1,3 +1,4 @@ from django.contrib import admin +from .models import Employee -# Register your models here. +admin.site.register([Employee]) diff --git a/employee/models.py b/employee/models.py index fd0fb66..a367373 100644 --- a/employee/models.py +++ b/employee/models.py @@ -14,4 +14,4 @@ class Employee(models.Model): projects = models.ManyToManyField(Project) def __str__(self): - return self.user.name + return self.employee_id diff --git a/employee/serializers.py b/employee/serializers.py new file mode 100644 index 0000000..58b8415 --- /dev/null +++ b/employee/serializers.py @@ -0,0 +1,8 @@ +from rest_framework import serializers +from .models import Employee + + +class EmployeeSerializer(serializers.ModelSerializer): + class Meta: + model = Employee + fields = ['id', 'user', 'employee_id', 'phone', 'salary', 'start_date', 'end_date', 'projects'] diff --git a/employee/urls.py b/employee/urls.py new file mode 100644 index 0000000..68bcaf6 --- /dev/null +++ b/employee/urls.py @@ -0,0 +1,6 @@ +from django.urls import path +from . import views + +urlpatterns = [ + path('employees/', views.EmployeeList.as_view()), +] diff --git a/employee/views.py b/employee/views.py index 91ea44a..4944149 100644 --- a/employee/views.py +++ b/employee/views.py @@ -1,3 +1,16 @@ -from django.shortcuts import render +from rest_framework.response import Response +from rest_framework.views import APIView +from rest_framework.authentication import TokenAuthentication, SessionAuthentication +from rest_framework.permissions import IsAuthenticated +from .models import Employee +from .serializers import EmployeeSerializer -# Create your views here. + +class EmployeeList(APIView): + authentication_classes = [TokenAuthentication, SessionAuthentication] + permission_classes = [IsAuthenticated] + + def get(self, request): + employees = Employee.objects.all() + serializer = EmployeeSerializer(employees, many=True) + return Response(serializer.data) diff --git a/project/admin.py b/project/admin.py index 8c38f3f..247c46b 100644 --- a/project/admin.py +++ b/project/admin.py @@ -1,3 +1,4 @@ from django.contrib import admin +from .models import Project -# Register your models here. +admin.site.register([Project]) diff --git a/workx_backend/urls.py b/workx_backend/urls.py index fd89be1..8b70c66 100644 --- a/workx_backend/urls.py +++ b/workx_backend/urls.py @@ -4,6 +4,7 @@ from rest_framework import permissions from drf_yasg.views import get_schema_view from drf_yasg import openapi from client import urls as clientUrls +from employee import urls as employeeUrls schema_view = get_schema_view( @@ -27,7 +28,8 @@ urlpatterns = [ path('swagger/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'), path('redoc/', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'), path('api/', include([ - path('', include(clientUrls)) + path('', include(clientUrls)), + path('', include(employeeUrls)) ])) ])), ]