mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-11-03 22:55:43 +00:00 
			
		
		
		
	Part category API
This commit is contained in:
		@@ -5,13 +5,14 @@ from django_filters.rest_framework import DjangoFilterBackend
 | 
			
		||||
from rest_framework import filters
 | 
			
		||||
from rest_framework import generics, permissions
 | 
			
		||||
 | 
			
		||||
from django.conf.urls import url
 | 
			
		||||
from django.conf.urls import url, include
 | 
			
		||||
 | 
			
		||||
from .models import Part, PartCategory, BomItem
 | 
			
		||||
from .models import SupplierPart
 | 
			
		||||
 | 
			
		||||
from .serializers import PartSerializer, BomItemSerializer
 | 
			
		||||
from .serializers import SupplierPartSerializer
 | 
			
		||||
from .serializers import CategorySerializer
 | 
			
		||||
 | 
			
		||||
from InvenTree.views import TreeSerializer
 | 
			
		||||
 | 
			
		||||
@@ -21,6 +22,36 @@ class PartCategoryTree(TreeSerializer):
 | 
			
		||||
    model = PartCategory
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class CategoryList(generics.ListCreateAPIView):
 | 
			
		||||
    queryset = PartCategory.objects.all()
 | 
			
		||||
    serializer_class = CategorySerializer
 | 
			
		||||
 | 
			
		||||
    permission_classes = [
 | 
			
		||||
        permissions.IsAuthenticatedOrReadOnly,
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
    filter_backends = [
 | 
			
		||||
        DjangoFilterBackend,
 | 
			
		||||
        #filters.SearchFilter,
 | 
			
		||||
        filters.OrderingFilter,
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
    filter_fields = [
 | 
			
		||||
        'parent',
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
    ordering_fields = [
 | 
			
		||||
        'name',
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
    ordering = 'name'
 | 
			
		||||
 | 
			
		||||
    search_fields = [
 | 
			
		||||
        'name',
 | 
			
		||||
        'description',
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class PartList(generics.ListCreateAPIView):
 | 
			
		||||
 | 
			
		||||
    queryset = Part.objects.all()
 | 
			
		||||
@@ -93,11 +124,17 @@ class SupplierPartList(generics.ListAPIView):
 | 
			
		||||
        'supplier'
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
cat_api_urls = [
 | 
			
		||||
 | 
			
		||||
    url(r'^$', CategoryList.as_view(), name='api-part-category-list'),
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
part_api_urls = [
 | 
			
		||||
 | 
			
		||||
    url(r'^tree/?', PartCategoryTree.as_view(), name='api-part-tree'),
 | 
			
		||||
 | 
			
		||||
    url(r'^category/', include(cat_api_urls)),
 | 
			
		||||
 | 
			
		||||
    url(r'^supplier/?', SupplierPartList.as_view(), name='api-part-supplier-list'),
 | 
			
		||||
    url(r'^bom/?', BomList.as_view(), name='api-bom-list'),
 | 
			
		||||
    url(r'^.*$', PartList.as_view(), name='api-part-list'),
 | 
			
		||||
 
 | 
			
		||||
@@ -5,7 +5,7 @@ from .models import SupplierPart
 | 
			
		||||
 | 
			
		||||
from company.serializers import CompanyBriefSerializer
 | 
			
		||||
 | 
			
		||||
class CategoryBriefSerializer(serializers.ModelSerializer):
 | 
			
		||||
class CategorySerializer(serializers.ModelSerializer):
 | 
			
		||||
 | 
			
		||||
    url = serializers.CharField(source='get_absolute_url', read_only=True)
 | 
			
		||||
 | 
			
		||||
@@ -17,6 +17,7 @@ class CategoryBriefSerializer(serializers.ModelSerializer):
 | 
			
		||||
            'description',
 | 
			
		||||
            'pathstring',
 | 
			
		||||
            'url',
 | 
			
		||||
            'parent',
 | 
			
		||||
        ]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -40,7 +41,7 @@ class PartSerializer(serializers.ModelSerializer):
 | 
			
		||||
    """
 | 
			
		||||
 | 
			
		||||
    url = serializers.CharField(source='get_absolute_url', read_only=True)
 | 
			
		||||
    category = CategoryBriefSerializer(many=False, read_only=True)
 | 
			
		||||
    category = CategorySerializer(many=False, read_only=True)
 | 
			
		||||
 | 
			
		||||
    class Meta:
 | 
			
		||||
        model = Part
 | 
			
		||||
 
 | 
			
		||||
@@ -16,6 +16,12 @@ function inventreeGet(url, filters={}) {
 | 
			
		||||
    })
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Return list of parts with optional filters
 | 
			
		||||
function getParts(filters={}) {
 | 
			
		||||
    return inventreeGet('/api/part/', filters);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Return list of part categories with optional filters
 | 
			
		||||
function getPartCategories(filters={}) {
 | 
			
		||||
    return inventreeGet('/api/part/category/', filters);
 | 
			
		||||
}
 | 
			
		||||
@@ -210,4 +210,6 @@
 | 
			
		||||
 | 
			
		||||
    getParts();
 | 
			
		||||
 | 
			
		||||
    getPartCategories({parent: 2});
 | 
			
		||||
 | 
			
		||||
{% endblock %}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user