mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-11-04 07:05:41 +00:00 
			
		
		
		
	Merge pull request #2520 from SchrodingersGat/build-comple-fix-2
Second bug fix for "build complete" form
This commit is contained in:
		@@ -582,7 +582,7 @@ class Build(MPTTModel, ReferenceIndexingMixin):
 | 
				
			|||||||
        self.subtractUntrackedStock(user)
 | 
					        self.subtractUntrackedStock(user)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # Ensure that there are no longer any BuildItem objects
 | 
					        # Ensure that there are no longer any BuildItem objects
 | 
				
			||||||
        # which point to thie Build Order
 | 
					        # which point to thisFcan Build Order
 | 
				
			||||||
        self.allocated_stock.all().delete()
 | 
					        self.allocated_stock.all().delete()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @transaction.atomic
 | 
					    @transaction.atomic
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -248,6 +248,8 @@ class BuildCompleteSerializer(serializers.Serializer):
 | 
				
			|||||||
    accept_unallocated = serializers.BooleanField(
 | 
					    accept_unallocated = serializers.BooleanField(
 | 
				
			||||||
        label=_('Accept Unallocated'),
 | 
					        label=_('Accept Unallocated'),
 | 
				
			||||||
        help_text=_('Accept that stock items have not been fully allocated to this build order'),
 | 
					        help_text=_('Accept that stock items have not been fully allocated to this build order'),
 | 
				
			||||||
 | 
					        required=False,
 | 
				
			||||||
 | 
					        default=False,
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def validate_accept_unallocated(self, value):
 | 
					    def validate_accept_unallocated(self, value):
 | 
				
			||||||
@@ -262,6 +264,8 @@ class BuildCompleteSerializer(serializers.Serializer):
 | 
				
			|||||||
    accept_incomplete = serializers.BooleanField(
 | 
					    accept_incomplete = serializers.BooleanField(
 | 
				
			||||||
        label=_('Accept Incomplete'),
 | 
					        label=_('Accept Incomplete'),
 | 
				
			||||||
        help_text=_('Accept that the required number of build outputs have not been completed'),
 | 
					        help_text=_('Accept that the required number of build outputs have not been completed'),
 | 
				
			||||||
 | 
					        required=False,
 | 
				
			||||||
 | 
					        default=False,
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def validate_accept_incomplete(self, value):
 | 
					    def validate_accept_incomplete(self, value):
 | 
				
			||||||
@@ -273,6 +277,15 @@ class BuildCompleteSerializer(serializers.Serializer):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        return value
 | 
					        return value
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def validate(self, data):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        build = self.context['build']
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if build.incomplete_count > 0:
 | 
				
			||||||
 | 
					            raise ValidationError(_("Build order has incomplete outputs"))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return data
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def save(self):
 | 
					    def save(self):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        request = self.context['request']
 | 
					        request = self.context['request']
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -38,7 +38,7 @@ class BuildAPITest(InvenTreeAPITestCase):
 | 
				
			|||||||
        super().setUp()
 | 
					        super().setUp()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class BuildCompleteTest(BuildAPITest):
 | 
					class BuildOutputCompleteTest(BuildAPITest):
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
    Unit testing for the build complete API endpoint
 | 
					    Unit testing for the build complete API endpoint
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
@@ -140,6 +140,9 @@ class BuildCompleteTest(BuildAPITest):
 | 
				
			|||||||
        Test build order completion
 | 
					        Test build order completion
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # Initially, build should not be able to be completed
 | 
				
			||||||
 | 
					        self.assertFalse(self.build.can_complete)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # We start without any outputs assigned against the build
 | 
					        # We start without any outputs assigned against the build
 | 
				
			||||||
        self.assertEqual(self.build.incomplete_outputs.count(), 0)
 | 
					        self.assertEqual(self.build.incomplete_outputs.count(), 0)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -153,7 +156,7 @@ class BuildCompleteTest(BuildAPITest):
 | 
				
			|||||||
        self.assertEqual(self.build.completed, 0)
 | 
					        self.assertEqual(self.build.completed, 0)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # We shall complete 4 of these outputs
 | 
					        # We shall complete 4 of these outputs
 | 
				
			||||||
        outputs = self.build.incomplete_outputs[0:4]
 | 
					        outputs = self.build.incomplete_outputs.all()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        self.post(
 | 
					        self.post(
 | 
				
			||||||
            self.url,
 | 
					            self.url,
 | 
				
			||||||
@@ -165,19 +168,43 @@ class BuildCompleteTest(BuildAPITest):
 | 
				
			|||||||
            expected_code=201
 | 
					            expected_code=201
 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # There now should be 6 incomplete build outputs remaining
 | 
					        self.assertEqual(self.build.incomplete_outputs.count(), 0)
 | 
				
			||||||
        self.assertEqual(self.build.incomplete_outputs.count(), 6)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # And there should be 4 completed outputs
 | 
					        # And there should be 10 completed outputs
 | 
				
			||||||
        outputs = self.build.complete_outputs
 | 
					        outputs = self.build.complete_outputs
 | 
				
			||||||
        self.assertEqual(outputs.count(), 4)
 | 
					        self.assertEqual(outputs.count(), 10)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        for output in outputs:
 | 
					        for output in outputs:
 | 
				
			||||||
            self.assertFalse(output.is_building)
 | 
					            self.assertFalse(output.is_building)
 | 
				
			||||||
            self.assertEqual(output.build, self.build)
 | 
					            self.assertEqual(output.build, self.build)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        self.build.refresh_from_db()
 | 
					        self.build.refresh_from_db()
 | 
				
			||||||
        self.assertEqual(self.build.completed, 40)
 | 
					        self.assertEqual(self.build.completed, 100)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # Try to complete the build (it should fail)
 | 
				
			||||||
 | 
					        finish_url = reverse('api-build-finish', kwargs={'pk': self.build.pk})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        response = self.post(
 | 
				
			||||||
 | 
					            finish_url,
 | 
				
			||||||
 | 
					            {},
 | 
				
			||||||
 | 
					            expected_code=400
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        self.assertTrue('accept_unallocated' in response.data)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # Accept unallocated stock
 | 
				
			||||||
 | 
					        response = self.post(
 | 
				
			||||||
 | 
					            finish_url,
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                'accept_unallocated': True,
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            expected_code=201,
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        self.build.refresh_from_db()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # Build should have been marked as complete
 | 
				
			||||||
 | 
					        self.assertTrue(self.build.is_complete)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class BuildAllocationTest(BuildAPITest):
 | 
					class BuildAllocationTest(BuildAPITest):
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user