mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Merge branch 'api'
This commit is contained in:
commit
24ece1b916
17
InvenTree/InvenTree/serializers.py
Normal file
17
InvenTree/InvenTree/serializers.py
Normal file
@ -0,0 +1,17 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from rest_framework import serializers
|
||||
from rest_framework import generics
|
||||
from rest_framework import mixins
|
||||
|
||||
class DraftRUDView(generics.RetrieveAPIView, generics.UpdateAPIView, generics.DestroyAPIView):
|
||||
|
||||
def perform_update(self, serializer):
|
||||
|
||||
ctx_data = serializer._context['request'].data
|
||||
|
||||
if ctx_data.get('_is_final', False) in [True, u'true', u'True', 1]:
|
||||
super(generics.UpdateAPIView, self).perform_update(serializer)
|
||||
else:
|
||||
pass
|
@ -92,7 +92,8 @@ TEMPLATES = [
|
||||
]
|
||||
|
||||
REST_FRAMEWORK = {
|
||||
'EXCEPTION_HANDLER': 'InvenTree.utils.api_exception_handler',
|
||||
'EXCEPTION_HANDLER': 'rest_framework.views.exception_handler'
|
||||
# 'EXCEPTION_HANDLER': 'InvenTree.utils.api_exception_handler',
|
||||
# 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
|
||||
# 'PAGE_SIZE': 50,
|
||||
}
|
||||
|
@ -3,17 +3,19 @@ from __future__ import unicode_literals
|
||||
|
||||
from django_filters.rest_framework import DjangoFilterBackend
|
||||
from rest_framework import filters
|
||||
from rest_framework import generics, permissions
|
||||
from rest_framework import generics, permissions, mixins
|
||||
|
||||
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
|
||||
from InvenTree.serializers import DraftRUDView
|
||||
|
||||
class PartCategoryTree(TreeSerializer):
|
||||
|
||||
@ -21,6 +23,44 @@ 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 PartDetail(DraftRUDView):
|
||||
queryset = Part.objects.all()
|
||||
serializer_class = PartSerializer
|
||||
|
||||
permission_classes = [
|
||||
permissions.IsAuthenticatedOrReadOnly,
|
||||
]
|
||||
|
||||
class PartList(generics.ListCreateAPIView):
|
||||
|
||||
queryset = Part.objects.all()
|
||||
@ -93,12 +133,21 @@ 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'^(?P<pk>\d+)/', PartDetail.as_view(), name='api-part-detail'),
|
||||
|
||||
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
|
||||
|
88
InvenTree/static/script/inventree/api.js
Normal file
88
InvenTree/static/script/inventree/api.js
Normal file
@ -0,0 +1,88 @@
|
||||
var jQuery = window.$;
|
||||
|
||||
// using jQuery
|
||||
function getCookie(name) {
|
||||
var cookieValue = null;
|
||||
if (document.cookie && document.cookie != '') {
|
||||
var cookies = document.cookie.split(';');
|
||||
for (var i = 0; i < cookies.length; i++) {
|
||||
var cookie = jQuery.trim(cookies[i]);
|
||||
// Does this cookie string begin with the name we want?
|
||||
if (cookie.substring(0, name.length + 1) == (name + '=')) {
|
||||
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return cookieValue;
|
||||
}
|
||||
|
||||
function inventreeGet(url, filters={}) {
|
||||
$.ajax({
|
||||
url: url,
|
||||
type: 'GET',
|
||||
data: filters,
|
||||
dataType: 'json',
|
||||
success: function(response) {
|
||||
console.log('Success GET data at ' + url);
|
||||
return response;
|
||||
},
|
||||
error: function(xhr, ajaxOptions, thrownError) {
|
||||
console.error('Error on GET at ' + url);
|
||||
console.error(thrownError);
|
||||
return {error: thrownError};
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function inventreeUpdate(url, data, final=false) {
|
||||
if (final) {
|
||||
data["_is_final"] = true;
|
||||
}
|
||||
|
||||
// Middleware token required for data update
|
||||
//var csrftoken = jQuery("[name=csrfmiddlewaretoken]").val();
|
||||
var csrftoken = getCookie('csrftoken');
|
||||
|
||||
$.ajax({
|
||||
beforeSend: function(xhr, settings) {
|
||||
xhr.setRequestHeader('X-CSRFToken', csrftoken);
|
||||
},
|
||||
url: url,
|
||||
type: 'put',
|
||||
data: data,
|
||||
dataType: 'json',
|
||||
success: function(response, status) {
|
||||
response['_status_code'] = status;
|
||||
console.log('UPDATE object to ' + url + ' - result = ' + status);
|
||||
return response;
|
||||
},
|
||||
error: function(xhr, ajaxOptions, thrownError) {
|
||||
console.error('Error on UPDATE to ' + url);
|
||||
console.error(thrownError);
|
||||
return {error: thrownError};
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
function getStock(filters={}) {
|
||||
return inventreeGet('/api/stock/', filters);
|
||||
}
|
||||
|
||||
function getStockLocations(filters={}) {
|
||||
return inventreeGet('/api/stock/location/', filters)
|
||||
}
|
||||
|
||||
function getCompanies(filters={}) {
|
||||
return inventreeGet('/api/company/', filters);
|
||||
}
|
@ -11,14 +11,14 @@ from .serializers import StockItemSerializer, StockQuantitySerializer
|
||||
from .serializers import LocationSerializer
|
||||
|
||||
from InvenTree.views import TreeSerializer
|
||||
|
||||
from InvenTree.serializers import DraftRUDView
|
||||
|
||||
class StockCategoryTree(TreeSerializer):
|
||||
title = 'Stock'
|
||||
model = StockLocation
|
||||
|
||||
|
||||
class StockDetail(generics.RetrieveUpdateDestroyAPIView):
|
||||
class StockDetail(DraftRUDView):
|
||||
"""
|
||||
|
||||
get:
|
||||
|
@ -56,7 +56,7 @@ class StockItemSerializer(serializers.ModelSerializer):
|
||||
'stocktake_date',
|
||||
'stocktake_user',
|
||||
'updated',
|
||||
'quantity',
|
||||
#'quantity',
|
||||
]
|
||||
|
||||
|
||||
|
@ -59,6 +59,8 @@
|
||||
{% endblock %}
|
||||
|
||||
{% block js_load %}
|
||||
{{ block.super }}
|
||||
<script type='text/javascript' src="{% static 'script/inventree/api.js' %}"></script>
|
||||
<script type='text/javascript' src="{% static 'script/modal_form.js' %}"></script>
|
||||
{% endblock %}
|
||||
{% block js_ready %}
|
||||
|
Loading…
Reference in New Issue
Block a user