浏览代码

Created viewset for all the views

master
kj1352 3 年前
父节点
当前提交
6264e15faf
共有 6 个文件被更改,包括 43 次插入44 次删除
  1. +9
    -3
      invoice/urls.py
  2. +7
    -15
      invoice/views.py
  3. +8
    -2
      project/urls.py
  4. +4
    -8
      project/views.py
  5. +8
    -3
      representative/urls.py
  6. +7
    -13
      representative/views.py

+ 9
- 3
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/<int:invoice_key>/', views.ItemList.as_view()),
path('', include(router.urls)),
]

+ 7
- 15
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)

+ 8
- 2
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)),
]

+ 4
- 8
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)

+ 8
- 3
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)),
]

+ 7
- 13
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)