Add API endpoint for purchase orders

- List and create purchase orders
- List and create purchase order line items
This commit is contained in:
Oliver Walters 2019-08-02 21:44:56 +10:00
parent 8bfbb8dfbb
commit b5f2c9d311
3 changed files with 142 additions and 0 deletions

View File

@ -23,6 +23,7 @@ from part.api import part_api_urls, bom_api_urls
from company.api import company_api_urls
from stock.api import stock_api_urls
from build.api import build_api_urls
from order.api import po_api_urls
from django.conf import settings
from django.conf.urls.static import static
@ -43,6 +44,7 @@ apipatterns = [
url(r'^company/', include(company_api_urls)),
url(r'^stock/', include(stock_api_urls)),
url(r'^build/', include(build_api_urls)),
url(r'^po/', include(po_api_urls)),
# User URLs
url(r'^user/', include(user_urls)),

92
InvenTree/order/api.py Normal file
View File

@ -0,0 +1,92 @@
"""
JSON API for the Order app
"""
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import generics, permissions
from django.conf.urls import url
from .models import PurchaseOrder, PurchaseOrderLineItem
from .serializers import POSerializer, POLineItemSerializer
class POList(generics.ListCreateAPIView):
""" API endpoint for accessing a list of Order objects
- GET: Return list of PO objects (with filters)
- POST: Create a new PurchaseOrder object
"""
queryset = PurchaseOrder.objects.all()
serializer_class = POSerializer
permission_classes = [
permissions.IsAuthenticated,
]
filter_backends = [
DjangoFilterBackend,
]
filter_fields = [
'supplier',
]
class PODetail(generics.RetrieveUpdateAPIView):
""" API endpoint for detail view of a PurchaseOrder object """
queryset = PurchaseOrder.objects.all()
serializer_class = POSerializer
permission_classes = [
permissions.IsAuthenticated
]
class POLineItemList(generics.ListCreateAPIView):
""" API endpoint for accessing a list of PO Line Item objects
- GET: Return a list of PO Line Item objects
- POST: Create a new PurchaseOrderLineItem object
"""
queryset = PurchaseOrderLineItem.objects.all()
serializer_class = POLineItemSerializer
permission_classes = [
permissions.IsAuthenticated,
]
filter_backends = [
DjangoFilterBackend,
]
filter_fields = [
'order',
'part'
]
class POLineItemDetail(generics.RetrieveUpdateAPIView):
""" API endpoint for detail view of a PurchaseOrderLineItem object """
queryset = PurchaseOrderLineItem
serializer_class = POLineItemSerializer
permission_classes = [
permissions.IsAuthenticated,
]
po_api_urls = [
url(r'^order/(?P<pk>\d+)/?$', PODetail.as_view(), name='api-po-detail'),
url(r'^order/?$', POList.as_view(), name='api-po-list'),
url(r'^line/(?P<pk>\d+)/?$', POLineItemDetail.as_view(), name='api-po-line-detail'),
url(r'^line/?$', POLineItemList.as_view(), name='api-po-line-list'),
]

View File

@ -0,0 +1,48 @@
"""
JSON serializers for the Order API
"""
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from InvenTree.serializers import InvenTreeModelSerializer
from .models import PurchaseOrder, PurchaseOrderLineItem
class POSerializer(InvenTreeModelSerializer):
""" Serializes an Order object """
class Meta:
model = PurchaseOrder
fields = [
'pk',
'supplier',
'reference',
'description',
'URL',
'status',
'notes',
]
read_only_fields = [
'reference',
'status'
]
class POLineItemSerializer(InvenTreeModelSerializer):
class Meta:
model = PurchaseOrderLineItem
fields = [
'pk',
'quantity',
'reference',
'notes',
'order',
'part',
'received',
]