Added supplier API

This commit is contained in:
Oliver Walters 2017-04-14 14:45:17 +10:00
parent f80918554d
commit e8ec11e3cc
4 changed files with 138 additions and 21 deletions

View File

@ -61,10 +61,7 @@ class PartFilter(django_filters.rest_framework.FilterSet):
class PartList(generics.ListCreateAPIView):
""" Display a list of parts, with optional filters
Filters are specified in the url, e.g.
/part/?category=127
/part/?min_stock=100
""" List of parts, with optional filters
"""
def get_queryset(self):

View File

@ -0,0 +1,42 @@
from rest_framework import serializers
from .models import Supplier, SupplierPart, SupplierPriceBreak
class SupplierSerializer(serializers.ModelSerializer):
class Meta:
model = Supplier
fields = '__all__'
class SupplierPartSerializer(serializers.ModelSerializer):
price_breaks = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
class Meta:
model = SupplierPart
fields = ['pk',
'part',
'supplier',
'SKU',
'manufacturer',
'MPN',
'URL',
'description',
'single_price',
'packaging',
'multiple',
'minimum',
'price_breaks',
'lead_time']
class SupplierPriceBreakSerializer(serializers.ModelSerializer):
class Meta:
model = SupplierPriceBreak
fields = ['pk',
'part',
'quantity',
'cost']

View File

@ -1,11 +1,30 @@
from django.conf.urls import url
from django.conf.urls import url, include
from . import views
urlpatterns = [
# Display details of a supplier
url(r'^(?P<supplier_id>[0-9]+)/$', views.supplierDetail, name='detail'),
url(r'^$', views.index, name='index')
partpatterns = [
url(r'^(?P<pk>[0-9]+)/?$', views.SupplierPartDetail.as_view()),
url(r'^\?*[^/]*/?$', views.SupplierPartList.as_view())
]
pricepatterns = [
url(r'^(?P<pk>[0-9]+)/?$', views.SupplierPriceBreakDetail.as_view()),
url(r'^\?*[^/]*/?$', views.SupplierPriceBreakList.as_view())
]
urlpatterns = [
# Supplier part information
url(r'part/?', include(partpatterns)),
# Supplier price information
url(r'price/?', include(pricepatterns)),
# Display details of a supplier
url(r'^(?P<pk>[0-9]+)/?$', views.SupplierDetail.as_view()),
# List suppliers
url(r'^\?*[^/]*/?$', views.SupplierList.as_view())
]

View File

@ -1,15 +1,74 @@
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse
from rest_framework import generics, permissions
from .models import Supplier
from .models import Supplier, SupplierPart, SupplierPriceBreak
from .serializers import SupplierSerializer
from .serializers import SupplierPartSerializer
from .serializers import SupplierPriceBreakSerializer
def index(request):
return HttpResponse("This is the suppliers page")
class SupplierDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = Supplier.objects.all()
serializer_class = SupplierSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
def supplierDetail(request, supplier_id):
supplier = get_object_or_404(Supplier, pk=supplier_id)
return render(request, 'supplier/detail.html',
{'supplier': supplier})
class SupplierList(generics.ListCreateAPIView):
queryset = Supplier.objects.all()
serializer_class = SupplierSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
class SupplierPartDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = SupplierPart.objects.all()
serializer_class = SupplierPartSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
class SupplierPartList(generics.ListCreateAPIView):
serializer_class = SupplierPartSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
def get_queryset(self):
parts = SupplierPart.objects.all()
params = self.request.query_params
supplier_id = params.get('supplier', None)
if supplier_id:
parts = parts.filter(supplier=supplier_id)
part_id = params.get('part', None)
if part_id:
parts = parts.filter(part=part_id)
manu_id = params.get('manufacturer', None)
if manu_id:
parts = parts.filter(manufacturer=manu_id)
return parts
class SupplierPriceBreakDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = SupplierPriceBreak.objects.all()
serializer_class = SupplierPriceBreakSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
class SupplierPriceBreakList(generics.ListCreateAPIView):
def get_queryset(self):
prices = SupplierPriceBreak.objects.all()
params = self.request.query_params
part_id = params.get('part', None)
if part_id:
prices = prices.filter(part=part_id)
return prices
serializer_class = SupplierPriceBreakSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)