Files
@ eab4034b3e03
Branch filter:
Location: rattail-project/rattail/rattail/gpc.py
eab4034b3e03
3.3 KiB
text/x-python
fix filemon in linux; improve batch provider and sil column discovery
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2012 Lance Edgar
#
# This file is part of Rattail.
#
# Rattail is free software: you can redistribute it and/or modify it under the
# terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
# more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
``rattail.gpc`` -- Global Product Code
"""
from sqlalchemy import types
from rattail import barcodes
class GPC(object):
"""
Class to abstract the details of Global Product Code data. Examples of
this would be UPC or EAN barcodes.
The initial motivation for this class was to provide better SIL support.
To that end, the instances are assumed to always be comprised of only
numeric digits, and must include a check digit. If you do not know the
check digit, provide a ``calc_check_digit`` value to the constructor.
"""
def __init__(self, value, calc_check_digit=False):
"""
Constructor. ``value`` must be either an integer or a long value, or a
string containing only digits.
If ``calc_check_digit`` is ``False``, then ``value`` is assumed to
include the check digit. If the value does not include a check digit
and needs one to be calculated, then ``calc_check_digit`` should be a
keyword signifying the algorithm to be used.
Currently the only check digit algorithm keyword supported is
``'upc'``. As that is likely to always be the default, a
``calc_check_digit`` value of ``True`` will be perceived as equivalent
to ``'upc'``.
"""
value = str(value)
if calc_check_digit is True or calc_check_digit == 'upc':
value += str(barcodes.upc_check_digit(value))
self.value = int(value)
def __cmp__(self, other):
if int(self) < int(other):
return -1
if int(self) > int(other):
return 1
if int(self) == int(other):
return 0
assert False
def __hash__(self):
return hash(self.value)
def __int__(self):
return int(self.value)
def __long__(self):
return long(self.value)
def __repr__(self):
return "GPC('%014d')" % self.value
def __str__(self):
return str(unicode(self))
def __unicode__(self):
return u'%014d' % self.value
class GPCType(types.TypeDecorator):
"""
SQLAlchemy type engine for GPC data.
"""
impl = types.BigInteger
def process_bind_param(self, value, dialect):
if value is None:
return None
return int(value)
def process_result_value(self, value, dialect):
if value is None:
return None
return GPC(value)
|