Fix annotations for Company serializers

This commit is contained in:
Oliver Walters 2020-09-05 23:03:38 +10:00
parent db214dfd73
commit 598e15af46
4 changed files with 84 additions and 51 deletions

View File

@ -4,7 +4,7 @@ JSON serializers for Company app
from rest_framework import serializers
from django.db.models import Count
from sql_util.utils import SubqueryCount
from .models import Company
from .models import SupplierPart, SupplierPriceBreak
@ -38,11 +38,17 @@ class CompanySerializer(InvenTreeModelSerializer):
@staticmethod
def annotate_queryset(queryset):
return queryset.annotate(
parts_supplied=Count('supplied_parts'),
parts_manufactured=Count('manufactured_parts')
# Add count of parts manufactured
queryset = queryset.annotate(
parts_manufactured=SubqueryCount('manufactured_parts')
)
queryset = queryset.annotate(
parts_supplied=SubqueryCount('supplied_parts')
)
return queryset
url = serializers.CharField(source='get_absolute_url', read_only=True)
image = serializers.CharField(source='get_thumbnail_url', read_only=True)

View File

@ -33,6 +33,7 @@ InvenTree | {% trans "Supplier List" %}
loadCompanyTable("#company-table", "{% url 'api-company-list' %}",
{
pagetype: '{{ pagetype }}',
params: {
{% for key,value in filters.items %}{{ key }}: "{{ value }}",{% endfor %}
}

View File

@ -51,18 +51,21 @@ class CompanyIndex(ListView):
'button_text': _('New Supplier'),
'filters': {'is_supplier': 'true'},
'create_url': reverse('supplier-create'),
'pagetype': 'suppliers',
},
reverse('manufacturer-index'): {
'title': _('Manufacturers'),
'button_text': _('New Manufacturer'),
'filters': {'is_manufacturer': 'true'},
'create_url': reverse('manufacturer-create'),
'pagetype': 'manufacturers',
},
reverse('customer-index'): {
'title': _('Customers'),
'button_text': _('New Customer'),
'filters': {'is_customer': 'true'},
'create_url': reverse('customer-create'),
'pagetype': 'customers',
}
}
@ -71,6 +74,7 @@ class CompanyIndex(ListView):
'button_text': _('New Company'),
'filters': {},
'create_url': reverse('company-create'),
'pagetype': 'companies'
}
context = None

View File

@ -21,6 +21,73 @@ function loadCompanyTable(table, url, options={}) {
setupFilterList("company", $(table));
var columns = [
{
field: 'pk',
title: 'ID',
visible: false,
switchable: false,
},
{
field: 'name',
title: '{% trans "Company" %}',
sortable: true,
switchable: false,
formatter: function(value, row, index, field) {
var html = imageHoverIcon(row.image) + renderLink(value, row.url);
if (row.is_customer) {
html += `<span title='{% trans "Customer" %}' class='fas fa-user-tie label-right'></span>`;
}
if (row.is_manufacturer) {
html += `<span title='{% trans "Manufacturer" %}' class='fas fa-industry label-right'></span>`;
}
if (row.is_supplier) {
html += `<span title='{% trans "Supplier" %}' class='fas fa-building label-right'></span>`;
}
return html;
}
},
{
field: 'description',
title: '{% trans "Description" %}',
sortable: true,
},
{
field: 'website',
title: '{% trans "Website" %}',
formatter: function(value, row, index, field) {
if (value) {
return renderLink(value, value);
}
return '';
}
},
];
if (options.pagetype == 'suppliers') {
columns.push({
sortable: true,
field: 'parts_supplied',
title: '{% trans "Parts Supplied" %}',
formatter: function(value, row) {
return renderLink(value, `/company/${row.pk}/parts/`);
}
});
} else if (options.pagetype == 'manufacturers') {
columns.push({
sortable: true,
field: 'parts_manufactured',
title: '{% trans "Parts Manufactured" %}',
formatter: function(value, row) {
return renderLink(value, `/company/${row.pk}/parts/`);
}
});
}
$(table).inventreeTable({
url: url,
method: 'get',
@ -28,53 +95,8 @@ function loadCompanyTable(table, url, options={}) {
groupBy: false,
formatNoMatches: function() { return "{% trans "No company information found" %}"; },
showColumns: true,
name: 'company',
columns: [
{
field: 'pk',
title: 'ID',
visible: false,
switchable: false,
},
{
field: 'name',
title: '{% trans "Company" %}',
sortable: true,
switchable: false,
formatter: function(value, row, index, field) {
var html = imageHoverIcon(row.image) + renderLink(value, row.url);
if (row.is_customer) {
html += `<span title='{% trans "Customer" %}' class='fas fa-user-tie label-right'></span>`;
}
if (row.is_manufacturer) {
html += `<span title='{% trans "Manufacturer" %}' class='fas fa-industry label-right'></span>`;
}
if (row.is_supplier) {
html += `<span title='{% trans "Supplier" %}' class='fas fa-building label-right'></span>`;
}
return html;
}
},
{
field: 'description',
title: '{% trans "Description" %}',
sortable: true,
},
{
field: 'website',
title: '{% trans "Website" %}',
formatter: function(value, row, index, field) {
if (value) {
return renderLink(value, value);
}
return '';
}
},
],
name: options.pagetype || 'company',
columns: columns,
});
}