New functions for Part model

- is_bom_valid() - Tests if bom checksums match
- check_bom() function to mark the BOM as valid
This commit is contained in:
Oliver Walters 2019-05-12 12:53:56 +10:00
parent 2431ba2a04
commit 985986a844

View File

@ -16,7 +16,7 @@ from django.core.exceptions import ValidationError
from django.urls import reverse from django.urls import reverse
from django.conf import settings from django.conf import settings
from django.db import models from django.db import models, transaction
from django.core.validators import MinValueValidator from django.core.validators import MinValueValidator
from django.contrib.staticfiles.templatetags.staticfiles import static from django.contrib.staticfiles.templatetags.staticfiles import static
@ -474,7 +474,6 @@ class Part(models.Model):
def used_in_count(self): def used_in_count(self):
return self.used_in.count() return self.used_in.count()
@property
def get_bom_hash(self): def get_bom_hash(self):
""" Return a checksum hash for the BOM for this part. """ Return a checksum hash for the BOM for this part.
Used to determine if the BOM has changed (and needs to be signed off!) Used to determine if the BOM has changed (and needs to be signed off!)
@ -497,7 +496,29 @@ class Part(models.Model):
return str(hash.digest()) return str(hash.digest())
@property
def is_bom_valid(self):
""" Check if the BOM is 'valid' - if the calculated checksum matches the stored value
"""
return self.get_bom_hash() == self.bom_checksum
@transaction.atomic
def check_bom(self, user):
""" Check the BOM (mark the BOM as validated by the given User.
- Calculates and stores the hash for the BOM
- Saves the current date and the checking user
"""
self.bom_checksum = get_bom_hash()
self.bom_checked_by = user
self.bom_checked_date = datetime.now().date()
self.save()
def required_parts(self): def required_parts(self):
""" Return a list of parts required to make this part (list of BOM items) """
parts = [] parts = []
for bom in self.bom_items.all(): for bom in self.bom_items.all():
parts.append(bom.sub_part) parts.append(bom.sub_part)
@ -505,7 +526,7 @@ class Part(models.Model):
@property @property
def supplier_count(self): def supplier_count(self):
# Return the number of supplier parts available for this part """ Return the number of supplier parts available for this part """
return self.supplier_parts.count() return self.supplier_parts.count()
def export_bom(self, **kwargs): def export_bom(self, **kwargs):