diff --git a/invoice/urls.py b/invoice/urls.py index c1c4dc4..b3bed2a 100644 --- a/invoice/urls.py +++ b/invoice/urls.py @@ -1,7 +1,13 @@ -from django.urls import path +from django.urls import path, include from . import views +from rest_framework import routers + + +router = routers.DefaultRouter() +router.register(r'items', views.ItemViewSet) +router.register(r'invoices', views.InvoiceViewSet) + urlpatterns = [ - path('invoices/', views.InvoiceList.as_view()), - path('invoice-items//', views.ItemList.as_view()), + path('', include(router.urls)), ] diff --git a/invoice/views.py b/invoice/views.py index 473123c..22453ff 100644 --- a/invoice/views.py +++ b/invoice/views.py @@ -1,27 +1,19 @@ -from rest_framework.response import Response -from rest_framework.views import APIView +from rest_framework import viewsets from rest_framework.authentication import TokenAuthentication, SessionAuthentication from rest_framework.permissions import IsAuthenticated from .models import Item, Invoice from .serializers import ItemSerializer, InvoiceSerializer -class ItemList(APIView): +class ItemViewSet(viewsets.ModelViewSet): + queryset = Item.objects.all() + serializer_class = ItemSerializer authentication_classes = [TokenAuthentication, SessionAuthentication] permission_classes = [IsAuthenticated] - def get(self, request, invoice_key): - items = Item.objects.filter(invoice=invoice_key) - serializer = ItemSerializer(items, many=True) - return Response(serializer.data) - -class InvoiceList(APIView): +class InvoiceViewSet(viewsets.ModelViewSet): + queryset = Invoice.objects.all() + serializer_class = InvoiceSerializer authentication_classes = [TokenAuthentication, SessionAuthentication] permission_classes = [IsAuthenticated] - - def get(self, request): - invoices = Invoice.objects.all() - invoice_serializer = InvoiceSerializer(invoices, many=True) - - return Response(invoice_serializer.data) diff --git a/project/urls.py b/project/urls.py index e85a5ea..842fbb1 100644 --- a/project/urls.py +++ b/project/urls.py @@ -1,6 +1,12 @@ -from django.urls import path +from django.urls import path, include from . import views +from rest_framework import routers + + +router = routers.DefaultRouter() +router.register(r'projects', views.ProjectViewSet) + urlpatterns = [ - path('projects/', views.ProjectList.as_view()), + path('', include(router.urls)), ] diff --git a/project/views.py b/project/views.py index d7e048c..e8ce760 100644 --- a/project/views.py +++ b/project/views.py @@ -1,16 +1,12 @@ -from rest_framework.response import Response -from rest_framework.views import APIView +from rest_framework import viewsets from rest_framework.authentication import TokenAuthentication, SessionAuthentication from rest_framework.permissions import IsAuthenticated from .models import Project from .serializers import ProjectSerializer -class ProjectList(APIView): +class ProjectViewSet(viewsets.ReadOnlyModelViewSet): + queryset = Project.objects.all() + serializer_class = ProjectSerializer authentication_classes = [TokenAuthentication, SessionAuthentication] permission_classes = [IsAuthenticated] - - def get(self, request): - projects = Project.objects.all() - serializer = ProjectSerializer(projects, many=True) - return Response(serializer.data) diff --git a/representative/urls.py b/representative/urls.py index 98c1621..2fa0d67 100644 --- a/representative/urls.py +++ b/representative/urls.py @@ -1,7 +1,12 @@ -from django.urls import path +from django.urls import path, include from . import views +from rest_framework import routers + + +router = routers.DefaultRouter() +router.register(r'representatives', views.RepresentativeViewSet) + urlpatterns = [ - path('representative-roles/', views.RoleList.as_view()), - path('representatives/', views.RepresentativeList.as_view()), + path('', include(router.urls)), ] diff --git a/representative/views.py b/representative/views.py index 7df7397..02cbf51 100644 --- a/representative/views.py +++ b/representative/views.py @@ -1,26 +1,20 @@ -from rest_framework.response import Response -from rest_framework.views import APIView +from rest_framework import viewsets from rest_framework.authentication import TokenAuthentication, SessionAuthentication from rest_framework.permissions import IsAuthenticated from .models import Representative, Role from .serializers import RoleSerializer, RepresentativeSerializer -class RoleList(APIView): +class RoleViewSet(viewsets.ReadOnlyModelViewSet): + queryset = Role.objects.all() + serializer_class = RoleSerializer authentication_classes = [TokenAuthentication, SessionAuthentication] permission_classes = [IsAuthenticated] - def get(self, request): - roles = Role.objects.all() - serializer = RoleSerializer(roles, many=True) - return Response(serializer.data) - -class RepresentativeList(APIView): +class RepresentativeViewSet(viewsets.ReadOnlyModelViewSet): + queryset = Representative.objects.all() + serializer_class = RepresentativeSerializer authentication_classes = [TokenAuthentication, SessionAuthentication] permission_classes = [IsAuthenticated] - def get(self, request): - representatives = Representative.objects.all() - serializer = RepresentativeSerializer(representatives, many=True) - return Response(serializer.data)