Changeset f97c452 in rattail


Ignore:
Timestamp:
07/19/2022 01:18:53 PM (7 months ago)
Author:
Lance Edgar <lance@…>
Branches:
master
Children:
dfadd3f
Parents:
27d18c42
Message:

Add upc_check_digits_needed() method for products handler

File:
1 edited

Legend:

Unmodified
Added
Removed
  • rattail/products.py

    r27d18c42 rf97c452  
    4040from rattail.app import GenericHandler
    4141from rattail.gpc import GPC
    42 from rattail.barcodes import upce_to_upca
     42from rattail.barcodes import upce_to_upca, upc_check_digit
    4343
    4444
     
    123123
    124124        return make_full_description(brand_name, description, size)
     125
     126    def upc_check_digits_needed(self, values):
     127        """
     128        Figure out whether the given UPC-ish values need to have check
     129        digits calculated, or if they're already present.
     130
     131        In practice what this does, is look for any values which *are*
     132        in need of a check digit.  If any are found, it will be
     133        assumed that "all" values need a check digit.
     134
     135        :param values: Sequence of UPC-ish values.  Each value is
     136           assumed to be a string.
     137
     138        :returns: Boolean; ``True`` means check digits need to be
     139           calculated for each value; ``False`` means check digits are
     140           already present.
     141        """
     142        for value in values:
     143
     144            # assuming the last char in string is the check digit, we
     145            # calculate again based on all but last char.  if *not* a
     146            # match, then this value needs a check digit!
     147            data = value[:-1]
     148            check = value[-1]
     149            calculated = upc_check_digit(data)
     150            if check != calculated:
     151                return True
     152
     153        # no check digits needed
     154        return False
    125155
    126156    # TODO: deprecate / remove (afaik it is not being used?)
Note: See TracChangeset for help on using the changeset viewer.