Add function to calculate BOM hash

- Uses hashlib.md5
This commit is contained in:
Oliver Walters 2019-05-12 12:42:06 +10:00
parent c9ecb10078
commit 4c3032e2f0
2 changed files with 28 additions and 0 deletions

View File

@ -25,6 +25,7 @@ from django.db.models.signals import pre_delete
from django.dispatch import receiver from django.dispatch import receiver
from fuzzywuzzy import fuzz from fuzzywuzzy import fuzz
import hashlib
from InvenTree import helpers from InvenTree import helpers
from InvenTree import validators from InvenTree import validators
@ -473,6 +474,29 @@ 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):
""" Return a checksum hash for the BOM for this part.
Used to determine if the BOM has changed (and needs to be signed off!)
For hash is calculated from the following fields of each BOM item:
- Part.full_name (if the part name changes, the BOM checksum is invalidated)
- quantity
- Note field
returns a string representation of a hash object which can be compared with a stored value
"""
hash = hashlib.md5('bom seed'.encode())
for item in self.bom_items.all():
hash.update(str(item.sub_part.full_name).encode())
hash.update(str(item.quantity).encode())
hash.update(str(item.notes).encode())
return str(hash.digest())
def required_parts(self): def required_parts(self):
parts = [] parts = []
for bom in self.bom_items.all(): for bom in self.bom_items.all():

View File

@ -51,6 +51,10 @@
<td><a href="{{ part.URL }}">{{ part.URL }}</a></td> <td><a href="{{ part.URL }}">{{ part.URL }}</a></td>
</tr> </tr>
{% endif %} {% endif %}
<tr>
<td>Hash</td>
<td>{{ part.get_bom_hash }}</td>
</tr>
<tr> <tr>
<td>Category</td> <td>Category</td>
<td> <td>