2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-12-16 09:18:10 +00:00

Enhancements

This commit is contained in:
Oliver Walters
2025-11-26 06:18:16 +00:00
parent 5408898145
commit e9042d3684
2 changed files with 29 additions and 5 deletions

View File

@@ -451,7 +451,17 @@ class ReferenceIndexingMixin(models.Model):
reference_int = models.BigIntegerField(default=0) reference_int = models.BigIntegerField(default=0)
class InvenTreeModel(PluginValidationMixin, models.Model): class ContentTypeMixin:
"""Mixin class which supports retrieval of the ContentType for a model instance."""
def get_content_type(self):
"""Return the ContentType object associated with this model."""
from django.contrib.contenttypes.models import ContentType
return ContentType.objects.get_for_model(self.__class__)
class InvenTreeModel(ContentTypeMixin, PluginValidationMixin, models.Model):
"""Base class for InvenTree models, which provides some common functionality. """Base class for InvenTree models, which provides some common functionality.
Includes the following mixins by default: Includes the following mixins by default:
@@ -658,7 +668,7 @@ class InvenTreeAttachmentMixin(InvenTreePermissionCheckMixin):
Attachment.objects.create(**kwargs) Attachment.objects.create(**kwargs)
class InvenTreeTree(MPTTModel): class InvenTreeTree(ContentTypeMixin, MPTTModel):
"""Provides an abstracted self-referencing tree model, based on the MPTTModel class. """Provides an abstracted self-referencing tree model, based on the MPTTModel class.
Our implementation provides the following key improvements: Our implementation provides the following key improvements:

View File

@@ -369,13 +369,21 @@ class PartParameterTest(InvenTreeAPITestCase):
# test that having non unique part/template combinations fails # test that having non unique part/template combinations fails
res = self.post(url, data, expected_code=400) res = self.post(url, data, expected_code=400)
self.assertEqual(len(res.data), 3) self.assertEqual(len(res.data), 3)
self.assertEqual(len(res.data[1]), 0) self.assertEqual(len(res.data[1]), 0)
for err in [res.data[0], res.data[2]]: for err in [res.data[0], res.data[2]]:
self.assertEqual(len(err), 2) self.assertEqual(len(err), 3)
self.assertEqual(str(err['model_id'][0]), 'This field must be unique.') self.assertEqual(str(err['model_id'][0]), 'This field must be unique.')
self.assertEqual(str(err['model_type'][0]), 'This field must be unique.')
self.assertEqual(str(err['template'][0]), 'This field must be unique.') self.assertEqual(str(err['template'][0]), 'This field must be unique.')
self.assertEqual(Parameter.objects.filter(content_object=part4).count(), 0)
self.assertEqual(
Parameter.objects.filter(
model_type=part4.get_content_type(), model_id=part4.pk
).count(),
0,
)
# Now, create a valid set of parameters # Now, create a valid set of parameters
data = [ data = [
@@ -384,7 +392,13 @@ class PartParameterTest(InvenTreeAPITestCase):
] ]
res = self.post(url, data, expected_code=201) res = self.post(url, data, expected_code=201)
self.assertEqual(len(res.data), 2) self.assertEqual(len(res.data), 2)
self.assertEqual(Parameter.objects.filter(content_object=part4).count(), 2)
self.assertEqual(
Parameter.objects.filter(
model_type=part4.get_content_type(), model_id=part4.pk
).count(),
2,
)
def test_param_detail(self): def test_param_detail(self):
"""Tests for the Parameter detail endpoint.""" """Tests for the Parameter detail endpoint."""