More API / JSON stuff

- SupplierPart JSON API
- Part supplier list
- Company part list
This commit is contained in:
Oliver 2018-05-03 00:47:03 +10:00
parent 49287c0c61
commit f995f54390
5 changed files with 139 additions and 48 deletions

View File

@ -3,7 +3,19 @@ from rest_framework import serializers
from .models import Company
class CompanySerializer(serializers.HyperlinkedModelSerializer):
class CompanyBriefSerializer(serializers.ModelSerializer):
url = serializers.CharField(source='get_absolute_url', read_only=True)
class Meta:
model = Company
fields = [
'pk',
'url',
'name'
]
class CompanySerializer(serializers.ModelSerializer):
url = serializers.CharField(source='get_absolute_url', read_only=True)

View File

@ -6,29 +6,7 @@
<h3>Company Parts</h3>
<table class='table table-striped' id='part-list' data-sorting='true' data-filtering='true'>
<thead>
<tr>
<th>SKU</th>
<th>Part</th>
<th>MPN</th>
<th>URL</th>
</tr>
</thead>
<tbody>
{% for part in company.parts.all %}
<tr>
<td><a href="{% url 'supplier-part-detail' part.id %}">{{ part.SKU }}</a></td>
<td>
{% if part.part %}
<a href="{% url 'part-suppliers' part.part.id %}">{{ part.part.name }}</a>
{% endif %}
</td>
<td>{{ part.manufacturer_string }}</td>
<td>{{ part.URL }}</td>
</tr>
{% endfor %}
</tbody>
<table clas='table table-striped table-condensed' id='part-table'>
</table>
<div class='container-fluid'>
@ -43,7 +21,7 @@
<script type='text/javascript' src="{% static 'script/modal_form.js' %}"></script>
{% endblock %}
{% block js_ready %}
$("#part-create").click(function () {
launchModalForm("#modal-form",
"{% url 'supplier-part-create' %}",
@ -54,4 +32,39 @@
reload: true,
});
});
$("#part-table").bootstrapTable({
sortable: true,
search: true,
queryParams: function(p) {
return {
supplier: {{ company.id }}
}
},
columns: [
{
sortable: true,
field: 'part',
title: 'Part',
formatter: function(value, row, index, field) {
return renderLink(value.name, value.url);
}
},
{
sortable: true,
field: 'SKU',
title: 'SKU',
formatter: function(value, row, index, field) {
return renderLink(value, row.url);
}
},
{
sortable: true,
field: 'manufacturer',
title: 'Manufacturer',
}
],
url: "{% url 'api-part-supplier-list' %}"
});
{% endblock %}

View File

@ -8,7 +8,10 @@ from rest_framework import generics, permissions
from django.conf.urls import url
from .models import Part, PartCategory, BomItem
from .models import SupplierPart
from .serializers import PartSerializer, BomItemSerializer
from .serializers import SupplierPartSerializer
from InvenTree.views import TreeSerializer
@ -70,10 +73,32 @@ class BomList(generics.ListAPIView):
]
class SupplierPartList(generics.ListAPIView):
queryset = SupplierPart.objects.all()
serializer_class = SupplierPartSerializer
permission_classes = [
permissions.IsAuthenticatedOrReadOnly,
]
filter_backends = [
DjangoFilterBackend,
filters.SearchFilter,
filters.OrderingFilter,
]
filter_fields = [
'part',
'supplier'
]
part_api_urls = [
url(r'^tree/?', PartCategoryTree.as_view(), name='api-part-tree'),
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'),
]

View File

@ -1,7 +1,9 @@
from rest_framework import serializers
from .models import Part, PartCategory, BomItem
from .models import SupplierPart
from company.serializers import CompanyBriefSerializer
class CategoryBriefSerializer(serializers.ModelSerializer):
@ -75,4 +77,24 @@ class BomItemSerializer(serializers.ModelSerializer):
'part',
'sub_part',
'quantity'
]
class SupplierPartSerializer(serializers.ModelSerializer):
url = serializers.CharField(source='get_absolute_url', read_only=True)
part = PartBriefSerializer(many=False, read_only=True)
supplier = CompanyBriefSerializer(many=False, read_only=True)
class Meta:
model = SupplierPart
fields = [
'pk',
'url',
'part',
'supplier',
'SKU',
'manufacturer',
'MPN',
]

View File

@ -6,30 +6,14 @@
<h3>Part Suppliers</h3>
<table class="table table-striped" id='supplier-table' data-sorting='true'>
<thead>
<tr>
<th>SKU</th>
<th>Supplier</th>
<th>MPN</th>
<th>URL</th>
</tr>
</thead>
<tbody>
{% for spart in part.supplier_parts.all %}
<tr>
<td><a href="{% url 'supplier-part-detail' spart.id %}">{{ spart.SKU }}</a></td>
<td><a href="{% url 'company-detail' spart.supplier.id %}">{{ spart.supplier.name }}</a></td>
<td>{{ spart.manufacturer_string }}</td>
<td>
{% if spart.URL %}
<a href="{{ spart.URL }}">{{ spart.URL }}</a>
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
{% if part.supplier_count > 0 %}
<p><b>{{ part.name }}</b> is available from {{ part.supplier_count }} suppliers.</p>
<table class="table table-striped table-condensed" id='supplier-table'>
</table>
{% else %}
<p><b>{{ part.name }}</b> is not available from any suppliers.</p>
{% endif %}
<div class='container-fluid'>
<button class="btn btn-success" id='supplier-create'>New Supplier Part</button>
@ -51,4 +35,39 @@
data: {part: {{ part.id }} }
});
});
$("#supplier-table").bootstrapTable({
sortable: true,
search: true,
queryParams: function(p) {
return {
part: {{ part.id }}
}
},
columns: [
{
sortable: true,
field: 'supplier',
title: 'Supplier',
formatter: function(value, row, index, field) {
return renderLink(value.name, value.url);
}
},
{
sortable: true,
field: 'SKU',
title: 'SKU',
formatter: function(value, row, index, field) {
return renderLink(value, row.url);
}
},
{
sortable: true,
field: 'manufacturer',
title: 'Manufacturer',
}
],
url: "{% url 'api-part-supplier-list' %}"
});
{% endblock %}