mirror of
https://github.com/inventree/InvenTree.git
synced 2025-04-28 11:36:44 +00:00
Added REST framework
- /part/ URL is now a JSON api
This commit is contained in:
parent
457d72a3e4
commit
7bcea2f3ac
@ -31,6 +31,9 @@ ALLOWED_HOSTS = []
|
|||||||
# Application definition
|
# Application definition
|
||||||
|
|
||||||
INSTALLED_APPS = [
|
INSTALLED_APPS = [
|
||||||
|
'rest_framework',
|
||||||
|
|
||||||
|
# Core django modules
|
||||||
'django.contrib.admin',
|
'django.contrib.admin',
|
||||||
'django.contrib.auth',
|
'django.contrib.auth',
|
||||||
'django.contrib.contenttypes',
|
'django.contrib.contenttypes',
|
||||||
@ -38,6 +41,7 @@ INSTALLED_APPS = [
|
|||||||
'django.contrib.messages',
|
'django.contrib.messages',
|
||||||
'django.contrib.staticfiles',
|
'django.contrib.staticfiles',
|
||||||
|
|
||||||
|
# InvenTree apps
|
||||||
'part.apps.PartConfig',
|
'part.apps.PartConfig',
|
||||||
'project.apps.ProjectConfig',
|
'project.apps.ProjectConfig',
|
||||||
'stock.apps.StockConfig',
|
'stock.apps.StockConfig',
|
||||||
|
20
InvenTree/part/serializers.py
Normal file
20
InvenTree/part/serializers.py
Normal 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')
|
@ -4,12 +4,14 @@ from . import views
|
|||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
# Display part detail
|
# Display part detail
|
||||||
url(r'^(?P<part_id>[0-9]+)/$', views.partDetail, name='detail'),
|
url(r'^(?P<pk>[0-9]+)/$', views.PartDetail.as_view()),
|
||||||
|
|
||||||
# Display a list of top-level categories
|
|
||||||
url(r'^category/$', views.categoryList, name='categorylist'),
|
|
||||||
|
|
||||||
# Display a single part category
|
# Display a single part category
|
||||||
url(r'^category/(?P<category_id>[0-9]+)/$', views.category, name='category'),
|
url(r'^category/(?P<pk>[0-9]+)/$', views.PartCategoryDetail.as_view()),
|
||||||
url(r'^$', views.index, name='index')
|
|
||||||
|
# Display a list of top-level categories
|
||||||
|
url(r'^category/$', views.PartCategoryList.as_view()),
|
||||||
|
|
||||||
|
# Display list of parts
|
||||||
|
url(r'^$', views.PartList.as_view())
|
||||||
]
|
]
|
@ -1,34 +1,30 @@
|
|||||||
from django.shortcuts import render, get_object_or_404
|
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 .models import PartCategory, Part
|
||||||
|
from .serializers import PartSerializer, PartCategorySerializer
|
||||||
|
|
||||||
def index(request):
|
def index(request):
|
||||||
return HttpResponse("Hello world. This is the parts page")
|
return HttpResponse("Hello world. This is the parts page")
|
||||||
|
|
||||||
def partDetail(request, part_id):
|
class PartDetail(generics.RetrieveAPIView):
|
||||||
|
|
||||||
part = get_object_or_404(Part, pk=part_id)
|
|
||||||
|
|
||||||
return render(request, 'part/detail.html',
|
|
||||||
{'part': part})
|
|
||||||
|
|
||||||
def categoryList(request):
|
queryset = Part.objects.all()
|
||||||
categories = PartCategory.objects.filter(parent = None)
|
serializer_class = PartSerializer
|
||||||
|
|
||||||
return render(request, 'part/categorylist.html',
|
|
||||||
{'categories': categories
|
|
||||||
})
|
|
||||||
|
|
||||||
def category(request, category_id):
|
class PartList(generics.ListAPIView):
|
||||||
|
|
||||||
|
queryset = Part.objects.all()
|
||||||
|
serializer_class = PartSerializer
|
||||||
|
|
||||||
|
class PartCategoryDetail(generics.RetrieveAPIView):
|
||||||
|
|
||||||
# Find the category
|
queryset = PartCategory.objects.all()
|
||||||
cat = get_object_or_404(PartCategory, pk=category_id)
|
serializer_class = PartCategorySerializer
|
||||||
|
|
||||||
# Child categories
|
class PartCategoryList(generics.ListAPIView):
|
||||||
childs = PartCategory.objects.filter(parent = cat.pk)
|
|
||||||
|
queryset = PartCategory.objects.all()
|
||||||
return render(request, 'part/category.html',
|
serializer_class = PartCategorySerializer
|
||||||
{'category': cat,
|
|
||||||
'children': childs
|
|
||||||
})
|
|
Loading…
x
Reference in New Issue
Block a user