Added Part Templates

This commit is contained in:
Oliver Walters 2017-03-29 15:02:59 +11:00
parent 774bd75e49
commit 11b790c3ec
2 changed files with 74 additions and 2 deletions

View File

@ -1,6 +1,6 @@
from django.contrib import admin
from .models import PartCategory, Part
from .models import PartCategory, Part, PartParameter, PartParameterTemplate
class PartAdmin(admin.ModelAdmin):
@ -12,5 +12,17 @@ class PartCategoryAdmin(admin.ModelAdmin):
list_display = ('name', 'path', 'description')
class ParameterTemplateAdmin(admin.ModelAdmin):
list_display = ('name', 'units', 'category')
class ParameterAdmin(admin.ModelAdmin):
list_display = ('part', 'template', 'value')
admin.site.register(Part, PartAdmin)
admin.site.register(PartCategory, PartCategoryAdmin)
admin.site.register(PartParameter, ParameterAdmin)
admin.site.register(PartParameterTemplate, ParameterTemplateAdmin)

View File

@ -2,7 +2,7 @@ from __future__ import unicode_literals
from django.db import models
from django.db.models import Sum
from django.core.exceptions import ObjectDoesNotExist
from django.core.exceptions import ObjectDoesNotExist, ValidationError
from InvenTree.models import InvenTreeTree
@ -76,6 +76,66 @@ class Part(models.Model):
return projects
class PartParameterTemplate(models.Model):
""" A PartParameterTemplate pre-defines a parameter field,
ready to be copied for use with a given Part.
A PartParameterTemplate can be optionally associated with a PartCategory
"""
category = models.ForeignKey(PartCategory, on_delete=models.CASCADE, blank=True, null=True)
name = models.CharField(max_length=20)
description = models.CharField(max_length=100, blank=True)
units = models.CharField(max_length=10, blank=True)
default_value = models.CharField(max_length=50, blank=True)
default_min = models.CharField(max_length=50, blank=True)
default_max = models.CharField(max_length=50, blank=True)
def __str__(self):
return "{name} ({units})".format(
name=self.name,
units=self.units)
class Meta:
verbose_name = "Parameter Template"
verbose_name_plural = "Parameter Templates"
class PartParameter(models.Model):
""" PartParameter is associated with a single part
"""
part = models.ForeignKey(Part, on_delete=models.CASCADE)
template = models.ForeignKey(PartParameterTemplate)
# Value data
value = models.CharField(max_length=50, blank=True)
min_value = models.CharField(max_length=50, blank=True)
max_value = models.CharField(max_length=50, blank=True)
# Prevent multiple parameters of the same template
# from being added to the same part
def save(self, *args, **kwargs):
params = PartParameter.objects.filter(part=self.part, template=self.template)
if len(params) > 0:
raise ValidationError("Parameter '{param}' already exists for {part}".format(
param=self.template.name,
part=self.part.name))
super(PartParameter, self).save(*args, **kwargs)
def __str__(self):
return "{name} : {val}{units}".format(
name=self.template.name,
val=self.value,
units=self.template.units)
class Meta:
verbose_name = "Part Parameter"
verbose_name_plural = "Part Parameters"
class PartRevision(models.Model):
""" A PartRevision represents a change-notification to a Part