Add "brief" version of QR codes

- Use this to render to labels (as it contains much less information)
This commit is contained in:
Oliver Walters 2020-08-16 13:29:38 +10:00
parent 2f5e3efada
commit 2bbc65cc59
4 changed files with 37 additions and 25 deletions

View File

@ -242,7 +242,7 @@ def WrapWithQuotes(text, quote='"'):
return text
def MakeBarcode(object_name, object_data):
def MakeBarcode(object_name, object_pk, object_data, **kwargs):
""" Generate a string for a barcode. Adds some global InvenTree parameters.
Args:
@ -255,12 +255,17 @@ def MakeBarcode(object_name, object_data):
json string of the supplied data plus some other data
"""
data = {
'tool': 'InvenTree',
'version': inventreeVersion(),
'instance': inventreeInstanceName(),
object_name: object_data
}
brief = kwargs.get('brief', False)
data = {}
if brief:
data[object_name] = object_pk
else:
data['tool'] = 'InvenTree'
data['version'] = inventreeVersion()
data['instance'] = inventreeInstanceName()
data[object_name] = object_data
return json.dumps(data, sort_keys=True)

View File

@ -47,12 +47,12 @@ class InvenTreeBarcodePlugin(BarcodePlugin):
else:
return False
for key in ['tool', 'version']:
if key not in self.data.keys():
return False
# If any of the following keys are in the JSON data,
# let's go ahead and assume that the code is a valid InvenTree one...
if not self.data['tool'] == 'InvenTree':
return False
for key in ['tool', 'version', 'InvenTree', 'stockitem', 'location', 'part']:
if key in self.data.keys():
return True
return True
@ -60,10 +60,22 @@ class InvenTreeBarcodePlugin(BarcodePlugin):
for k in self.data.keys():
if k.lower() == 'stockitem':
data = self.data[k]
pk = None
# Initially try casting to an integer
try:
pk = self.data[k]['id']
except (AttributeError, KeyError):
raise ValidationError({k: "id parameter not supplied"})
pk = int(data)
except (ValueError):
pk = None
if pk is None:
try:
pk = self.data[k]['id']
except (AttributeError, KeyError):
raise ValidationError({k: "id parameter not supplied"})
try:
item = StockItem.objects.get(pk=pk)

View File

@ -142,7 +142,7 @@ class StockItemLabel(LabelTemplate):
'serial': item.serial,
'uid': item.uid,
'pk': item.pk,
'qr_data': item.format_short_barcode(),
'qr_data': item.format_barcode(brief=True),
'tests': item.testResultMap()
})

View File

@ -283,7 +283,7 @@ class StockItem(MPTTModel):
def get_part_name(self):
return self.part.full_name
def format_barcode(self):
def format_barcode(self, **kwargs):
""" Return a JSON string for formatting a barcode for this StockItem.
Can be used to perform lookup of a stockitem using barcode
@ -296,19 +296,14 @@ class StockItem(MPTTModel):
return helpers.MakeBarcode(
"stockitem",
self.id,
{
"id": self.id,
"url": reverse('api-stock-detail', kwargs={'pk': self.id}),
}
},
**kwargs
)
def format_short_barcode(self):
"""
Return a short barcode
"""
return "stockid={pk}".format(pk=self.pk)
uid = models.CharField(blank=True, max_length=128, help_text=("Unique identifier field"))
parent = TreeForeignKey(