Added REST framework

- /part/ URL is now a JSON api
This commit is contained in:
Oliver Walters 2017-03-28 22:12:02 +11:00
parent 457d72a3e4
commit 7bcea2f3ac
4 changed files with 51 additions and 29 deletions

View File

@ -31,6 +31,9 @@ ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'rest_framework',
# Core django modules
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
@ -38,6 +41,7 @@ INSTALLED_APPS = [
'django.contrib.messages',
'django.contrib.staticfiles',
# InvenTree apps
'part.apps.PartConfig',
'project.apps.ProjectConfig',
'stock.apps.StockConfig',

View File

@ -0,0 +1,20 @@
from rest_framework import serializers
from .models import Part, PartCategory
class PartSerializer(serializers.ModelSerializer):
class Meta:
model = Part
fields = ('pk',
'IPN',
'description',
'category',
'quantity')
class PartCategorySerializer(serializers.ModelSerializer):
class Meta:
model = PartCategory
fields = ('pk',
'name',
'description',
'path')

View File

@ -4,12 +4,14 @@ from . import views
urlpatterns = [
# Display part detail
url(r'^(?P<part_id>[0-9]+)/$', views.partDetail, name='detail'),
# Display a list of top-level categories
url(r'^category/$', views.categoryList, name='categorylist'),
url(r'^(?P<pk>[0-9]+)/$', views.PartDetail.as_view()),
# Display a single part category
url(r'^category/(?P<category_id>[0-9]+)/$', views.category, name='category'),
url(r'^$', views.index, name='index')
url(r'^category/(?P<pk>[0-9]+)/$', views.PartCategoryDetail.as_view()),
# Display a list of top-level categories
url(r'^category/$', views.PartCategoryList.as_view()),
# Display list of parts
url(r'^$', views.PartList.as_view())
]

View File

@ -1,34 +1,30 @@
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse
from django.http import HttpResponse, Http404
from rest_framework import generics
from .models import PartCategory, Part
from .serializers import PartSerializer, PartCategorySerializer
def index(request):
return HttpResponse("Hello world. This is the parts page")
def partDetail(request, part_id):
part = get_object_or_404(Part, pk=part_id)
return render(request, 'part/detail.html',
{'part': part})
class PartDetail(generics.RetrieveAPIView):
def categoryList(request):
categories = PartCategory.objects.filter(parent = None)
return render(request, 'part/categorylist.html',
{'categories': categories
})
queryset = Part.objects.all()
serializer_class = PartSerializer
def category(request, category_id):
class PartList(generics.ListAPIView):
queryset = Part.objects.all()
serializer_class = PartSerializer
class PartCategoryDetail(generics.RetrieveAPIView):
# Find the category
cat = get_object_or_404(PartCategory, pk=category_id)
queryset = PartCategory.objects.all()
serializer_class = PartCategorySerializer
# Child categories
childs = PartCategory.objects.filter(parent = cat.pk)
return render(request, 'part/category.html',
{'category': cat,
'children': childs
})
class PartCategoryList(generics.ListAPIView):
queryset = PartCategory.objects.all()
serializer_class = PartCategorySerializer