From 277b28a7e948a2b98ee1c9798d0ef0240f746093 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Tue, 14 Apr 2020 22:38:32 +1000 Subject: [PATCH] Create a "unique(ish)" hash for barcode data --- InvenTree/InvenTree/api.py | 1 + InvenTree/plugins/barcode/barcode.py | 12 +++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/InvenTree/InvenTree/api.py b/InvenTree/InvenTree/api.py index 427bd4efdf..2c5d3f1b0c 100644 --- a/InvenTree/InvenTree/api.py +++ b/InvenTree/InvenTree/api.py @@ -85,6 +85,7 @@ class BarcodeScanView(APIView): } response['plugin'] = plugin.get_name() + response['hash'] = plugin.hash() break diff --git a/InvenTree/plugins/barcode/barcode.py b/InvenTree/plugins/barcode/barcode.py index 9482154857..6710c0f78e 100644 --- a/InvenTree/plugins/barcode/barcode.py +++ b/InvenTree/plugins/barcode/barcode.py @@ -1,5 +1,7 @@ # -*- coding: utf-8 -*- +import hashlib + import plugins.plugin as plugin @@ -18,9 +20,17 @@ class BarcodePlugin(plugin.InvenTreePlugin): Calculate a hash for the barcode data. This is supposed to uniquely identify the barcode contents, at least within the bardcode sub-type. + + The default implementation simply returns an MD5 hash of the barcode data, + encoded to a string. + + This may be sufficient for most applications, but can obviously be overridden + by a subclass. + """ - return "" + hash = hashlib.md5(str(self.data).encode()) + return str(hash.hexdigest()) def validate(self): """