def clean_barcode(self): barcode = self.cleaned_data.get('barcode') if barcode: try: barcode = verify_barcode(self._barcode_system, barcode) except BarcodeError, e: self.field_error('barcode', e.msg)
def clean(self): d = super(ProductFormBase, self).clean() barcode_system = d.get('barcode_system') barcode = d.get('barcode') if barcode_system is None: barcode_system = self._request.settings.barcode_system # verify that the barcode is correct... if barcode and barcode_system: try: d['barcode'] = verify_barcode(barcode_system, barcode) except BarcodeError, e: self.field_error('barcode', e.msg)
def test_should_raise_error_if_given_barcode_system_is_not_valid(self): with self.assertRaisesRegexp( BarcodeError, 'The barcode system \'foo\' is not known or invalid'): verify_barcode('foo', '9780596809485')
def test_should_raise_error_if_barcode_decoder_cannot_decode_barcode(self): with self.assertRaisesRegexp(BarcodeError, 'ISBN must start with 978 or 979'): verify_barcode('isbn', '123456789')
def test_should_raise_error_if_checksum_digit_is_missing_with_formatting( self): with self.assertRaisesRegexp( BarcodeError, 'The barcode checksum is invalid\. Missing digits: 5\?'): verify_barcode('isbn', '978-0-596-80948')
def test_should_raise_error_if_checksum_digit_is_incorrect(self): with self.assertRaisesRegexp( BarcodeError, 'The barcode checksum is invalid\. Missing digits: 5\?'): verify_barcode('isbn', '9780596809480')
def test_should_raise_error_if_barcode_is_too_short(self): with self.assertRaisesRegexp( BarcodeError, 'The barcode must have at least 6 digits'): verify_barcode('isbn', '978')
def test_should_raise_error_if_barcode_is_empty(self): with self.assertRaisesRegexp( BarcodeError, 'The barcode must have at least 6 digits'): verify_barcode('ean', '')
def test_should_return_full_unformatted_barcode_if_valid_formatted_barcode_was_given( self): bc = verify_barcode('isbn', '978-0-596-809485') self.assertEqual('9780596809485', bc)