コード例 #1
0
def validate(number):
    """Checks to see if the number provided is a valid GTIN code.  Will
    validate GTIN-14, GTIN-13 (EAN-13), GTIN-12 (UPC-A), and GTIN-8 (EAN-8)
    codes. This checks the length and the check bit but does not check whether
    a known GS1 Prefix and company identifier are referenced."""
    number = compact(number)
    if not number.isdigit():
        raise InvalidFormat()
    if len(number) not in (14, 13, 12, 8):
        raise InvalidLength()
    if calc_check_digit(number[:-1]) != number[-1]:
        raise InvalidChecksum()
    return number
コード例 #2
0
ファイル: isbn.py プロジェクト: benmao/python-stdnum
def to_isbn13(number):
    """Convert the number to ISBN-13 format."""
    number = number.strip()
    min_number = compact(number, convert=False)
    if len(min_number) == 13:
        return number  # nothing to do, already ISBN-13
    # put new check digit in place
    number = number[:-1] + ean.calc_check_digit('978' + min_number[:-1])
    # add prefix
    if ' ' in number:
        return '978 ' + number
    elif '-' in number:
        return '978-' + number
    else:
        return '978' + number
コード例 #3
0
ファイル: isbn.py プロジェクト: xrg/python-stdnum
def to_isbn13(number):
    """Convert the number to ISBN-13 format."""
    number = number.strip()
    min_number = compact(number, convert=False)
    if len(min_number) == 13:
        return number  # nothing to do, already ISBN-13
    # put new check digit in place
    number = number[:-1] + ean.calc_check_digit('978' + min_number[:-1])
    # add prefix
    if ' ' in number:
        return '978 ' + number
    elif '-' in number:
        return '978-' + number
    else:
        return '978' + number
コード例 #4
0
def to_isbn13(number):
    """Convert the number to ISBN-13 format."""
    number = number.strip()
    min_number = clean(number, ' -')
    if len(min_number) == 13:
        return number  # nothing to do, already ISBN-13
    if len(min_number) == 9:
        number = '0' + number  # convert from 9 to 10 digits
    # put new check digit in place
    number = number[:-1] + ean.calc_check_digit('978' + min_number[:-1])
    # add prefix
    if ' ' in number:
        return '978 ' + number
    elif '-' in number:
        return '978-' + number
    else:
        return '978' + number
コード例 #5
0
ファイル: isbn.py プロジェクト: arthurdejong/python-stdnum
def to_isbn13(number):
    """Convert the number to ISBN-13 format."""
    number = number.strip()
    min_number = clean(number, ' -')
    if len(min_number) == 13:
        return number  # nothing to do, already ISBN-13
    if len(min_number) == 9:
        number = '0' + number  # convert from 9 to 10 digits
    # put new check digit in place
    number = number[:-1] + ean.calc_check_digit('978' + min_number[:-1])
    # add prefix
    if ' ' in number:
        return '978 ' + number
    elif '-' in number:
        return '978-' + number
    else:
        return '978' + number
コード例 #6
0
ファイル: validators.py プロジェクト: boxine/bx_django_utils
def validate_gtin(number, accepted_length=DEFAULT_ACCEPT_LENGTH):
    """
    It's the same as stdnum.ean.validate() but also accept ISBN-10
    """
    number = compact(number)  # Convert to the minimal representation
    if not isdigits(number):
        raise InvalidFormat()

    if len(number) not in accepted_length:
        raise InvalidLength()

    if len(number) == 10:
        # Maybe a ISBN-10 number:
        return isbn.validate(number)

    if calc_check_digit(number[:-1]) != number[-1]:
        raise InvalidChecksum()

    return number
コード例 #7
0
 def generate_barcode_from_product_product(self):
     self.ensure_one()
     if self.barcode:
         raise UserError(
             _("The product '%s' already has a barcode.") %
             self.display_name)
     barcode_without_checksum = self.env['ir.sequence'].next_by_code(
         'private.product.barcode')
     if len(barcode_without_checksum) not in (7, 12):
         raise UserError(
             _("The sequence 'private.product.barcode' is not properly "
               "configured. The generated sequence should have 7 digits "
               "(for EAN-8) or 12 digits (for EAN-13). "
               "It currently has %d digits." %
               len(barcode_without_checksum)))
     checksum = calc_check_digit(barcode_without_checksum)
     barcode = barcode_without_checksum + str(checksum)
     self.write({
         'barcode': barcode,
         'must_print_barcode': True,
     })
コード例 #8
0
ファイル: issn.py プロジェクト: Manexware/python-stdnum
def to_ean(number, issue_code='00'):
    """Convert the number to EAN-13 format."""
    number = '977' + validate(number)[:-1] + issue_code
    return number + ean.calc_check_digit(number)
コード例 #9
0
def to_ean(number, issue_code='00'):
    """Convert the number to EAN-13 format."""
    number = '977' + validate(number)[:-1] + issue_code
    return number + ean.calc_check_digit(number)
コード例 #10
0
def upce_to_upca(upce, validate=True):
    """ Converts an UPC-E code to a 12 digit UPC-A code.

    If code is 7 digits and first digit is not zero then will form 8 digit
    code by pading left with a zero (assumes the left zero was lost somewhere
    in conversion to int).

    See:
    https://stackoverflow.com/questions/31539005/how-to-convert-a-upc-e-barcode-to-a-upc-a-barcode
    http://www.taltech.com/barcodesoftware/symbologies/upc

    Args:
      upce (str or int): The upce code.  Will properly handle a 6 or 8 digit
          code or an 8 digit code that lost its leading zero.
      validate: Will validate the length of input, the 'S' leader digit if an 8
          digit code, and the checksum (if given an 8 digit code).

    Returns:
      (str): The UPC-A code.
    """
    given_upce = str(upce)

    # can safely zero pad left a seven digit upce (is missing its left zero)
    if (len(given_upce) == 7) and (given_upce[0] != '0'):
        pad = '0'
    else:
        pad = ''

    padded_upce = pad + given_upce

    if validate:
        if len(padded_upce) not in (6, 8):
            raise ValueError("Provided UPC-E {} must be 6 or 8 digits!".format(
                repr(given_upce)))

        if (len(padded_upce) == 8 and padded_upce[0] not in ('0', '1')):
            raise ValueError("8 digit UPC-E must begin with 0 or 1")

    given_6_digit_upce = (len(padded_upce) == 6)
    upce6 = padded_upce if given_6_digit_upce else padded_upce[1:7]

    # upce6 digit names: abcdeN
    map_id = int(upce6[-1])  # N
    ab_prefix = upce6[0:2]  # ab

    if map_id >= 5:
        core = upce6[2:5], '0' * 4, map_id  # cde0000N
    elif map_id <= 2:
        core = map_id, '0' * 4, upce6[2:5]  # N0000cde
    elif map_id == 3:
        core = upce6[2], '0' * 5, upce6[3:5]  # c00000de
    elif map_id == 4:
        core = upce6[2:4], '0' * 5, upce6[4]  # cd00000e

    # ab + core
    no_leader_no_checksum = ab_prefix + ''.join(map(str, core))

    # add leader, also called 'S' digit
    leader = '0' if given_6_digit_upce else padded_upce[0]
    with_leader_no_checksum = leader + no_leader_no_checksum

    if given_6_digit_upce:
        with_checksum = with_leader_no_checksum\
            + ean.calc_check_digit(with_leader_no_checksum)
    else:
        with_checksum = with_leader_no_checksum + given_upce[-1]
        if validate:
            ean.validate(with_checksum)

    return with_checksum
コード例 #11
0
ファイル: genelfonks.py プロジェクト: milisarge/giom
 def barkod_tamamla(self, barkode):
     ek = ean.calc_check_digit(str(barkode))
     return str(barkode) + ek