From 8b515571cabd36cdabf756674ef19531107da4cf Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 21 Jun 2021 23:33:27 +0200 Subject: [PATCH 01/26] I think a fix for #1663 Closes #1663 --- InvenTree/templates/js/build.js | 1 + InvenTree/templates/js/order.js | 1 + 2 files changed, 2 insertions(+) diff --git a/InvenTree/templates/js/build.js b/InvenTree/templates/js/build.js index e8af981817..31917aab26 100644 --- a/InvenTree/templates/js/build.js +++ b/InvenTree/templates/js/build.js @@ -231,6 +231,7 @@ function loadBuildOrderAllocationTable(table, options={}) { { field: 'quantity', title: '{% trans "Quantity" %}', + sortable: true, } ] }); diff --git a/InvenTree/templates/js/order.js b/InvenTree/templates/js/order.js index 649357b083..0af54fa43c 100644 --- a/InvenTree/templates/js/order.js +++ b/InvenTree/templates/js/order.js @@ -391,6 +391,7 @@ function loadSalesOrderAllocationTable(table, options={}) { { field: 'quantity', title: '{% trans "Quantity" %}', + sortable: true, } ] }); From c8defae5752e182984dd1a0af2f95ec8998929ec Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 22 Jun 2021 00:03:54 +0200 Subject: [PATCH 02/26] fixing allocation sorting --- InvenTree/templates/js/build.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/InvenTree/templates/js/build.js b/InvenTree/templates/js/build.js index 31917aab26..398796773c 100644 --- a/InvenTree/templates/js/build.js +++ b/InvenTree/templates/js/build.js @@ -693,9 +693,6 @@ function loadBuildOutputAllocationTable(buildInfo, output, options={}) { var qA = rowA.quantity; var qB = rowB.quantity; - qA *= output.quantity; - qB *= output.quantity; - // Handle the case where both numerators are zero if ((aA == 0) && (aB == 0)) { return (qA > qB) ? 1 : -1; From 3c1f0637dcddfa6f6b4782e64e81883f113c0f30 Mon Sep 17 00:00:00 2001 From: Oliver Date: Mon, 28 Jun 2021 20:42:37 +1000 Subject: [PATCH 03/26] Adds unit tests for HTML API endpoints --- InvenTree/InvenTree/test_api.py | 87 +++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/InvenTree/InvenTree/test_api.py b/InvenTree/InvenTree/test_api.py index f196006df9..bc1157cf45 100644 --- a/InvenTree/InvenTree/test_api.py +++ b/InvenTree/InvenTree/test_api.py @@ -1,7 +1,13 @@ """ Low level tests for the InvenTree API """ +from django.http import response from rest_framework import status +from django.test import TestCase + +from django.contrib.auth import get_user_model +from django.contrib.auth.models import Group + from django.urls import reverse from InvenTree.api_tester import InvenTreeAPITestCase @@ -11,6 +17,87 @@ from users.models import RuleSet from base64 import b64encode +class HTMLAPITests(TestCase): + """ + Test that we can access the REST API endpoints via the HTML interface. + + History: Discovered on 2021-06-28 a bug in InvenTreeModelSerializer, + which raised an AssertionError when using the HTML API interface, + while the regular JSON interface continued to work as expected. + """ + + def setUp(self): + super().setUp() + + # Create a user + user = get_user_model() + + self.user = user.objects.create_user( + username='username', + email='user@email.com', + password='password' + ) + + # Put the user into a group with the correct permissions + group = Group.objects.create(name='mygroup') + self.user.groups.add(group) + + # Give the group *all* the permissions! + for rule in group.rule_sets.all(): + rule.can_view = True + rule.can_change = True + rule.can_add = True + rule.can_delete = True + + rule.save() + + self.client.login(username='username', password='password') + + def test_part_api(self): + url = reverse('api-part-list') + + # Check JSON response + response = self.client.get(url, HTTP_ACCEPT='application/json') + self.assertEqual(response.status_code, 200) + + # Check HTTP response + response = self.client.get(url, HTTP_ACCEPT='text/html') + self.assertEqual(response.status_code, 200) + + def test_build_api(self): + url = reverse('api-build-list') + + # Check JSON response + response = self.client.get(url, HTTP_ACCEPT='application/json') + self.assertEqual(response.status_code, 200) + + # Check HTTP response + response = self.client.get(url, HTTP_ACCEPT='text/html') + self.assertEqual(response.status_code, 200) + + def test_stock_api(self): + url = reverse('api-stock-list') + + # Check JSON response + response = self.client.get(url, HTTP_ACCEPT='application/json') + self.assertEqual(response.status_code, 200) + + # Check HTTP response + response = self.client.get(url, HTTP_ACCEPT='text/html') + self.assertEqual(response.status_code, 200) + + def test_company_list(self): + url = reverse('api-company-list') + + # Check JSON response + response = self.client.get(url, HTTP_ACCEPT='application/json') + self.assertEqual(response.status_code, 200) + + # Check HTTP response + response = self.client.get(url, HTTP_ACCEPT='text/html') + self.assertEqual(response.status_code, 200) + + class APITests(InvenTreeAPITestCase): """ Tests for the InvenTree API """ From 4dbd770f2d4f3bf65acef7d43cd048ef2956c482 Mon Sep 17 00:00:00 2001 From: Oliver Date: Mon, 28 Jun 2021 21:08:50 +1000 Subject: [PATCH 04/26] Fixed (I think?) --- InvenTree/InvenTree/serializers.py | 18 +++++++----------- InvenTree/InvenTree/test_api.py | 1 - 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/InvenTree/InvenTree/serializers.py b/InvenTree/InvenTree/serializers.py index 50a37d8cba..16db21ca37 100644 --- a/InvenTree/InvenTree/serializers.py +++ b/InvenTree/InvenTree/serializers.py @@ -7,8 +7,6 @@ from __future__ import unicode_literals import os -from collections import OrderedDict - from django.conf import settings from django.contrib.auth.models import User from django.core.exceptions import ValidationError as DjangoValidationError @@ -46,16 +44,13 @@ class InvenTreeModelSerializer(serializers.ModelSerializer): def __init__(self, instance=None, data=empty, **kwargs): - self.instance = instance + # self.instance = instance # If instance is None, we are creating a new instance - if instance is None: + if instance is None and data is not empty: - if data is empty: - data = OrderedDict() - else: - # Required to side-step immutability of a QueryDict - data = data.copy() + # Required to side-step immutability of a QueryDict + data = data.copy() # Add missing fields which have default values ModelClass = self.Meta.model @@ -85,7 +80,7 @@ class InvenTreeModelSerializer(serializers.ModelSerializer): Use the 'default' values specified by the django model definition """ - initials = super().get_initial() + initials = super().get_initial().copy() # Are we creating a new instance? if self.instance is None: @@ -111,7 +106,8 @@ class InvenTreeModelSerializer(serializers.ModelSerializer): return initials def run_validation(self, data=empty): - """ Perform serializer validation. + """ + Perform serializer validation. In addition to running validators on the serializer fields, this class ensures that the underlying model is also validated. """ diff --git a/InvenTree/InvenTree/test_api.py b/InvenTree/InvenTree/test_api.py index bc1157cf45..18f9319624 100644 --- a/InvenTree/InvenTree/test_api.py +++ b/InvenTree/InvenTree/test_api.py @@ -1,6 +1,5 @@ """ Low level tests for the InvenTree API """ -from django.http import response from rest_framework import status from django.test import TestCase From f0f6c7d186f0062f4a3b3d80062dbdf00acde83c Mon Sep 17 00:00:00 2001 From: Oliver Date: Mon, 28 Jun 2021 21:09:48 +1000 Subject: [PATCH 05/26] Add a comment --- InvenTree/InvenTree/serializers.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/InvenTree/InvenTree/serializers.py b/InvenTree/InvenTree/serializers.py index 16db21ca37..772daa06ab 100644 --- a/InvenTree/InvenTree/serializers.py +++ b/InvenTree/InvenTree/serializers.py @@ -59,6 +59,11 @@ class InvenTreeModelSerializer(serializers.ModelSerializer): for field_name, field in fields.fields.items(): + """ + Update the field IF (and ONLY IF): + - The field has a specified default value + - The field does not already have a value set + """ if field.has_default() and field_name not in data: value = field.default From d10169932d635baa5acfb90e3e340ea7108da378 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 29 Jun 2021 12:33:54 +0200 Subject: [PATCH 06/26] option to hide related parts #1733 --- InvenTree/common/models.py | 7 +++++++ InvenTree/part/templates/part/navbar.html | 3 +++ InvenTree/templates/InvenTree/settings/part.html | 1 + 3 files changed, 11 insertions(+) diff --git a/InvenTree/common/models.py b/InvenTree/common/models.py index ddd27b734e..7e56429c51 100644 --- a/InvenTree/common/models.py +++ b/InvenTree/common/models.py @@ -219,6 +219,13 @@ class InvenTreeSetting(models.Model): 'validator': bool, }, + 'PART_SHOW_RELATED': { + 'name': _('Show related parts'), + 'description': _('Display related parts for a part'), + 'default': True, + 'validator': bool, + }, + 'PART_INTERNAL_PRICE': { 'name': _('Internal Prices'), 'description': _('Enable internal prices for parts'), diff --git a/InvenTree/part/templates/part/navbar.html b/InvenTree/part/templates/part/navbar.html index c0bc4c96a3..1fae6aaec2 100644 --- a/InvenTree/part/templates/part/navbar.html +++ b/InvenTree/part/templates/part/navbar.html @@ -3,6 +3,7 @@ {% load inventree_extras %} {% settings_value "PART_INTERNAL_PRICE" as show_internal_price %} +{% settings_value 'PART_SHOW_RELATED' as show_related %}