Add more information to the Barcode

- API endpoint URL
- Add barcode generation for StockLocation
This commit is contained in:
Oliver Walters 2019-05-02 20:50:20 +10:00
parent d49ce465e5
commit c901294a48
4 changed files with 32 additions and 9 deletions

View File

@ -46,11 +46,13 @@ def WrapWithQuotes(text, quote='"'):
return text return text
def MakeBarcode(object_type, data={}): def MakeBarcode(object_type, object_url, data={}):
""" Generate a string for a barcode. Adds some global InvenTree parameters. """ Generate a string for a barcode. Adds some global InvenTree parameters.
Args: Args:
data: Python dict obejct which will be rendered to string (must only contain stringable values) object_type: string describing the object type e.g. 'StockItem'
object_url: url for JSON API detail view of the object
data: Python dict object containing extra datawhich will be rendered to string (must only contain stringable values)
Returns: Returns:
json string of the supplied data plus some other data json string of the supplied data plus some other data
@ -58,6 +60,7 @@ def MakeBarcode(object_type, data={}):
# Add in some generic InvenTree data # Add in some generic InvenTree data
data['type'] = object_type data['type'] = object_type
data['url'] = object_url
data['tool'] = 'InvenTree' data['tool'] = 'InvenTree'
data['generated'] = str(datetime.now().date()) data['generated'] = str(datetime.now().date())

View File

@ -345,11 +345,11 @@ class LocationDetail(generics.RetrieveUpdateDestroyAPIView):
stock_endpoints = [ stock_endpoints = [
url(r'^$', StockDetail.as_view(), name='stockitem-detail'), url(r'^$', StockDetail.as_view(), name='api-stock-detail'),
] ]
location_endpoints = [ location_endpoints = [
url(r'^$', LocationDetail.as_view(), name='stocklocation-detail'), url(r'^$', LocationDetail.as_view(), name='api-location-detail'),
] ]
stock_api_urls = [ stock_api_urls = [

View File

@ -36,6 +36,19 @@ class StockLocation(InvenTreeTree):
def has_items(self): def has_items(self):
return self.stock_items.count() > 0 return self.stock_items.count() > 0
@property
def format_barcode(self):
""" Return a JSON string for formatting a barcode for this StockLocation object """
return helpers.MakeBarcode(
'StockLocation',
reverse('api-location-detail', kwargs={'pk': self.id}),
{
'id': self.id,
'name': self.name,
}
)
@receiver(pre_delete, sender=StockLocation, dispatch_uid='stocklocation_delete_log') @receiver(pre_delete, sender=StockLocation, dispatch_uid='stocklocation_delete_log')
def before_delete_stock_location(sender, instance, using, **kwargs): def before_delete_stock_location(sender, instance, using, **kwargs):
@ -135,13 +148,18 @@ class StockItem(models.Model):
{ type: 'StockItem', stock_id: <pk>, part_id: <part_pk> } { type: 'StockItem', stock_id: <pk>, part_id: <part_pk> }
Any other data should be looked up using the InvenTree API (as it may change) Voltagile data (e.g. stock quantity) should be looked up using the InvenTree API (as it may change)
""" """
return helpers.MakeBarcode('StockItem', { return helpers.MakeBarcode(
'stock_id': self.id, 'StockItem',
'part_id': self.part.id reverse('api-stock-detail', kwargs={'pk': self.id}),
}) {
'id': self.id,
'part_id': self.part.id,
'part_name': self.part.name
}
)
# The 'master' copy of the part of which this stock item is an instance # The 'master' copy of the part of which this stock item is an instance
part = models.ForeignKey('part.Part', on_delete=models.CASCADE, related_name='locations', help_text='Base part') part = models.ForeignKey('part.Part', on_delete=models.CASCADE, related_name='locations', help_text='Base part')

View File

@ -1,5 +1,6 @@
{% extends "stock/stock_app_base.html" %} {% extends "stock/stock_app_base.html" %}
{% load static %} {% load static %}
{% load qr_code %}
{% block content %} {% block content %}
<div class='row'> <div class='row'>
@ -25,6 +26,7 @@
<li><a href="#" id='location-delete' title='Delete stock location'>Delete</a></li> <li><a href="#" id='location-delete' title='Delete stock location'>Delete</a></li>
</ul> </ul>
</div> </div>
{% qr_from_text location.format_barcode size="s" image_format="png" error_correction="L" %}
{% endif %} {% endif %}
</div> </div>
</h3> </h3>