mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Improved tests, fixed admin, improved naming
This commit is contained in:
parent
1c14c2237a
commit
e401bb8e3c
@ -277,14 +277,7 @@ class ParameterAdmin(ImportExportModelAdmin):
|
||||
|
||||
class PartCategoryParameterAdmin(admin.ModelAdmin):
|
||||
|
||||
def get_form(self, request, obj=None, **kwargs):
|
||||
""" Display only parent categories as choices for category field """
|
||||
|
||||
form = super().get_form(request, obj, **kwargs)
|
||||
|
||||
form.base_fields['category'].choices = PartCategory.get_parent_categories()
|
||||
|
||||
return form
|
||||
pass
|
||||
|
||||
|
||||
class PartSellPriceBreakAdmin(admin.ModelAdmin):
|
||||
|
@ -42,7 +42,7 @@
|
||||
default_value: '2.8'
|
||||
|
||||
- model: part.PartCategoryParameterTemplate
|
||||
pk: 3
|
||||
pk: 2
|
||||
fields:
|
||||
category: 7
|
||||
parameter_template: 3
|
||||
|
@ -207,7 +207,7 @@ class EditPartForm(HelperForm):
|
||||
|
||||
selected_category_templates = forms.BooleanField(required=False,
|
||||
initial=False,
|
||||
label=_('Include selected category parameter templates'),
|
||||
label=_('Include category parameter templates'),
|
||||
widget=forms.HiddenInput())
|
||||
|
||||
parent_category_templates = forms.BooleanField(required=False,
|
||||
|
@ -180,7 +180,9 @@ class PartCategory(InvenTreeTree):
|
||||
def get_parameter_templates(self):
|
||||
""" Return parameter templates associated to category """
|
||||
|
||||
return PartCategoryParameterTemplate.objects.filter(category=self.id)
|
||||
prefetch = PartCategoryParameterTemplate.objects.prefetch_related('category', 'parameter_template')
|
||||
|
||||
return prefetch.filter(category=self.id)
|
||||
|
||||
|
||||
@receiver(pre_delete, sender=PartCategory, dispatch_uid='partcategory_delete_log')
|
||||
@ -1701,6 +1703,7 @@ class PartCategoryParameterTemplate(models.Model):
|
||||
|
||||
def __str__(self):
|
||||
""" String representation of a PartCategoryParameterTemplate (admin interface) """
|
||||
|
||||
if self.default_value:
|
||||
return f'{self.category.name} | {self.parameter_template.name} | {self.default_value}'
|
||||
else:
|
||||
|
@ -3,10 +3,10 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.test import TestCase
|
||||
from django.test import TestCase, TransactionTestCase
|
||||
import django.core.exceptions as django_exceptions
|
||||
|
||||
from .models import PartCategory
|
||||
from .models import Part, PartCategory
|
||||
from .models import PartParameter, PartParameterTemplate
|
||||
from .models import PartCategoryParameterTemplate
|
||||
|
||||
@ -46,15 +46,46 @@ class TestParams(TestCase):
|
||||
t3.full_clean()
|
||||
t3.save()
|
||||
|
||||
|
||||
class TestCategoryTemplates(TransactionTestCase):
|
||||
|
||||
fixtures = [
|
||||
'location',
|
||||
'category',
|
||||
'part',
|
||||
'params'
|
||||
]
|
||||
|
||||
def test_validate(self):
|
||||
|
||||
# Category templates
|
||||
n = PartCategoryParameterTemplate.objects.all().count()
|
||||
self.assertEqual(n, 2)
|
||||
|
||||
category = PartCategory.objects.get(pk=7)
|
||||
category = PartCategory.objects.get(pk=8)
|
||||
|
||||
t1 = PartParameterTemplate.objects.get(pk=2)
|
||||
c1 = PartCategoryParameterTemplate(category=category,
|
||||
parameter_template=t1,
|
||||
default_value='xyz')
|
||||
c1.save()
|
||||
|
||||
self.assertEqual(n + 1, PartCategoryParameterTemplate.objects.filter(category=7).count())
|
||||
n = PartCategoryParameterTemplate.objects.all().count()
|
||||
self.assertEqual(n, 3)
|
||||
|
||||
# Get test part
|
||||
part = Part.objects.get(pk=1)
|
||||
|
||||
# Get part parameters count
|
||||
n_param = part.get_parameters().count()
|
||||
|
||||
add_category_templates = {
|
||||
'main': True,
|
||||
'parent': True,
|
||||
}
|
||||
# Save it with category parameters
|
||||
part.save(**{'add_category_templates': add_category_templates})
|
||||
|
||||
# Check new part parameters count
|
||||
# Only 2 parameters should be added as one already existed with same template
|
||||
self.assertEqual(n_param + 2, part.get_parameters().count())
|
||||
|
Loading…
Reference in New Issue
Block a user