Improved tests, fixed admin, improved naming

This commit is contained in:
eeintech 2020-11-04 12:06:07 -05:00
parent 1c14c2237a
commit e401bb8e3c
5 changed files with 42 additions and 15 deletions

View File

@ -277,14 +277,7 @@ class ParameterAdmin(ImportExportModelAdmin):
class PartCategoryParameterAdmin(admin.ModelAdmin): class PartCategoryParameterAdmin(admin.ModelAdmin):
def get_form(self, request, obj=None, **kwargs): pass
""" 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
class PartSellPriceBreakAdmin(admin.ModelAdmin): class PartSellPriceBreakAdmin(admin.ModelAdmin):

View File

@ -42,7 +42,7 @@
default_value: '2.8' default_value: '2.8'
- model: part.PartCategoryParameterTemplate - model: part.PartCategoryParameterTemplate
pk: 3 pk: 2
fields: fields:
category: 7 category: 7
parameter_template: 3 parameter_template: 3

View File

@ -207,7 +207,7 @@ class EditPartForm(HelperForm):
selected_category_templates = forms.BooleanField(required=False, selected_category_templates = forms.BooleanField(required=False,
initial=False, initial=False,
label=_('Include selected category parameter templates'), label=_('Include category parameter templates'),
widget=forms.HiddenInput()) widget=forms.HiddenInput())
parent_category_templates = forms.BooleanField(required=False, parent_category_templates = forms.BooleanField(required=False,

View File

@ -180,7 +180,9 @@ class PartCategory(InvenTreeTree):
def get_parameter_templates(self): def get_parameter_templates(self):
""" Return parameter templates associated to category """ """ 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') @receiver(pre_delete, sender=PartCategory, dispatch_uid='partcategory_delete_log')
@ -1701,6 +1703,7 @@ class PartCategoryParameterTemplate(models.Model):
def __str__(self): def __str__(self):
""" String representation of a PartCategoryParameterTemplate (admin interface) """ """ String representation of a PartCategoryParameterTemplate (admin interface) """
if self.default_value: if self.default_value:
return f'{self.category.name} | {self.parameter_template.name} | {self.default_value}' return f'{self.category.name} | {self.parameter_template.name} | {self.default_value}'
else: else:

View File

@ -3,10 +3,10 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals
from django.test import TestCase from django.test import TestCase, TransactionTestCase
import django.core.exceptions as django_exceptions import django.core.exceptions as django_exceptions
from .models import PartCategory from .models import Part, PartCategory
from .models import PartParameter, PartParameterTemplate from .models import PartParameter, PartParameterTemplate
from .models import PartCategoryParameterTemplate from .models import PartCategoryParameterTemplate
@ -46,15 +46,46 @@ class TestParams(TestCase):
t3.full_clean() t3.full_clean()
t3.save() t3.save()
class TestCategoryTemplates(TransactionTestCase):
fixtures = [
'location',
'category',
'part',
'params'
]
def test_validate(self):
# Category templates # Category templates
n = PartCategoryParameterTemplate.objects.all().count() n = PartCategoryParameterTemplate.objects.all().count()
self.assertEqual(n, 2) 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, c1 = PartCategoryParameterTemplate(category=category,
parameter_template=t1, parameter_template=t1,
default_value='xyz') default_value='xyz')
c1.save() 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())