From 598e15af4632a6077f3838f64c6ad3b50d0da1b7 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sat, 5 Sep 2020 23:03:38 +1000 Subject: [PATCH] Fix annotations for Company serializers --- InvenTree/company/serializers.py | 14 ++- .../company/templates/company/index.html | 1 + InvenTree/company/views.py | 4 + InvenTree/templates/js/company.html | 116 +++++++++++------- 4 files changed, 84 insertions(+), 51 deletions(-) diff --git a/InvenTree/company/serializers.py b/InvenTree/company/serializers.py index 16e7911ceb..a80b3b55a9 100644 --- a/InvenTree/company/serializers.py +++ b/InvenTree/company/serializers.py @@ -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) diff --git a/InvenTree/company/templates/company/index.html b/InvenTree/company/templates/company/index.html index f337019409..253085568e 100644 --- a/InvenTree/company/templates/company/index.html +++ b/InvenTree/company/templates/company/index.html @@ -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 %} } diff --git a/InvenTree/company/views.py b/InvenTree/company/views.py index 3f8cde21d3..457cf74ec2 100644 --- a/InvenTree/company/views.py +++ b/InvenTree/company/views.py @@ -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 diff --git a/InvenTree/templates/js/company.html b/InvenTree/templates/js/company.html index ef80070d99..f904c0f506 100644 --- a/InvenTree/templates/js/company.html +++ b/InvenTree/templates/js/company.html @@ -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 += ``; + } + + if (row.is_manufacturer) { + html += ``; + } + + if (row.is_supplier) { + html += ``; + } + + 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 += ``; - } - - if (row.is_manufacturer) { - html += ``; - } - - if (row.is_supplier) { - html += ``; - } - - 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, }); }