Add barcode function on product identifier
This commit is contained in:
1 parent
871695b912
commit
349dd77ae5
7 files changed
+50
-10
No files matched your search
@@ -1,3 +1,4 @@
|
||||
* Add barcode function on product identifier
|
||||
* Add identifier_get to product
|
||||
|
||||
Version 6.6.0 - 2022-10-31
|
||||
|
||||
@@ -24,6 +24,11 @@ from trytond.tools import is_full_text, lstrip_wildcard
|
||||
from trytond.tools.multivalue import migrate_property
|
||||
from trytond.transaction import Transaction
|
||||
|
||||
try:
|
||||
from trytond.tools import barcode
|
||||
except ImportError:
|
||||
barcode = None
|
||||
|
||||
from .exceptions import InvalidIdentifierCode
|
||||
from .ir import price_decimal
|
||||
|
||||
@@ -908,3 +913,8 @@ class ProductIdentifier(sequence_ordered(), ModelSQL, ModelView):
|
||||
type=self.type_string,
|
||||
code=self.code,
|
||||
product=product))
|
||||
|
||||
def barcode(self, format='svg', **options):
|
||||
if barcode and self.type in barcode.BARCODES:
|
||||
generator = getattr(barcode, 'generate_%s' % format)
|
||||
return generator(self.type, self.on_change_with_code(), **options)
|
||||
@@ -50,7 +50,10 @@ for dep in info.get('depends', []):
|
||||
requires.append(get_require_version('trytond_%s' % dep))
|
||||
requires.append(get_require_version('trytond'))
|
||||
|
||||
tests_require = [get_require_version('proteus')]
|
||||
tests_require = [
|
||||
get_require_version('proteus'),
|
||||
get_require_version('trytond[barcode]'),
|
||||
]
|
||||
|
||||
setup(name=name,
|
||||
version=version,
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
# This file is part of Tryton. The COPYRIGHT file at the top level of
|
||||
# this repository contains the full copyright notices and license terms.
|
||||
|
||||
import io
|
||||
import unittest
|
||||
from decimal import Decimal
|
||||
|
||||
from trytond.modules.company.tests import CompanyTestMixin
|
||||
from trytond.modules.product import round_price
|
||||
from trytond.modules.product.exceptions import UOMAccessError
|
||||
from trytond.modules.product.product import barcode
|
||||
from trytond.pool import Pool
|
||||
from trytond.tests.test_tryton import ModuleTestCase, with_transaction
|
||||
from trytond.transaction import Transaction
|
||||
@@ -585,5 +588,29 @@ class ProductTestCase(CompanyTestMixin, ModuleTestCase):
|
||||
|
||||
self.assertEqual(product.identifier_get('ean'), None)
|
||||
|
||||
@unittest.skipUnless(barcode, 'required barcode')
|
||||
@with_transaction()
|
||||
def test_product_identifier_barcode(self):
|
||||
"Test identifier barcode"
|
||||
pool = Pool()
|
||||
Identifier = pool.get('product.identifier')
|
||||
Product = pool.get('product.product')
|
||||
Template = pool.get('product.template')
|
||||
Uom = pool.get('product.uom')
|
||||
|
||||
uom, = Uom.search([], limit=1)
|
||||
template = Template(name="Product", default_uom=uom)
|
||||
template.save()
|
||||
product = Product(template=template)
|
||||
product.identifiers = [
|
||||
Identifier(type='ean', code='978-0-471-11709-4'),
|
||||
]
|
||||
product.save()
|
||||
identifier, = product.identifiers
|
||||
|
||||
image = identifier.barcode()
|
||||
self.assertIsInstance(image, io.BytesIO)
|
||||
self.assertIsNotNone(image.getvalue())
|
||||
|
||||
|
||||
del ModuleTestCase
|
||||
+4
-1
@@ -3,7 +3,10 @@ envlist = {py37,py38,py39,py310}-{sqlite,postgresql}
|
||||
|
||||
[testenv]
|
||||
usedevelop = true
|
||||
extras = test barcode qrcode
|
||||
extras =
|
||||
test
|
||||
barcode
|
||||
qrcode
|
||||
commands =
|
||||
coverage run --omit=*/tests/* -m xmlrunner discover -s trytond.tests {posargs}
|
||||
commands_post =
|
||||
|
||||
@@ -519,13 +519,13 @@ class Report(URLMixin, PoolBase):
|
||||
@classmethod
|
||||
def barcode(cls, name, code, size=(), **kwargs):
|
||||
image = barcode.generate_svg(name, code, **kwargs)
|
||||
return image, 'image/svg+xml', *size
|
||||
return (image, 'image/svg+xml', *size)
|
||||
|
||||
if qrcode:
|
||||
@classmethod
|
||||
def qrcode(cls, code, size=(), **kwargs):
|
||||
image = qrcode.generate_svg(code, **kwargs)
|
||||
return image, 'image/svg+xml', *size
|
||||
return (image, 'image/svg+xml', *size)
|
||||
|
||||
|
||||
def get_email(report, record, languages):
|
||||
|
||||
@@ -32,16 +32,12 @@ def generate_svg(
|
||||
background='white', foreground='black'):
|
||||
|
||||
class FactoryImage(SvgImage):
|
||||
def _rect(self, *args, **kwargs):
|
||||
el = super()._rect(*args, **kwargs)
|
||||
el.attrib['fill'] = foreground
|
||||
return el
|
||||
|
||||
pass
|
||||
setattr(FactoryImage, 'background', background)
|
||||
return _generate(
|
||||
code, box_size=box_size, border=border,
|
||||
error_correction=_error_correction(error_correction),
|
||||
image_factory=FactoryImage)
|
||||
image_factory=SvgImage)
|
||||
|
||||
|
||||
def generate_png(
|
||||
|
||||
Reference in new issue
Block a user