Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
lib-py-b2b / order_status.py
Size: Mime:
from enum import Enum

import logging
from os import environ

log_level = logging.getLevelName(environ['LOG_LEVEL']) if 'LOG_LEVEL' in environ else logging.INFO
logging.basicConfig(format='%(asctime)s,%(msecs)d %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s',
                    datefmt='%d-%m-%Y:%H:%M:%S',
                    level=log_level)
logger = logging.getLogger('lib-b2b-order-status')


class SimpleOrderStatus(Enum):
    NONE = ('NONE', -1)
    RECEIVED = ('RECEIVED', 0)
    PENDING = ('PENDING', 1)
    REJECTED = ('REJECTED', 2)
    COMPLETE = ('COMPLETE', 3)

    @staticmethod
    def for_(simple_order_status):
        """
        Convert variable to an SimpleOrderStatus type. Will pass an actual SimpleOrderStatus through.
        :param simple_order_status: string or SimpleOrderStatus object
        :return: SimpleOrderStatus
        """
        if isinstance(simple_order_status, str):
            _s = [s for s in SimpleOrderStatus if s.value == simple_order_status]
            if _s:
                return _s[0]
            else:
                return SimpleOrderStatus.NONE
        else:
            _status = simple_order_status
        return _status

    @staticmethod
    def all():
        return [s for s in SimpleOrderStatus]

    @property
    def order_statuses(self) -> ['OrderStatus']:
        return [status for status in OrderStatus if status.simple_status.value == self.value]

    def __new__(cls, value, rank: int):
        obj = object.__new__(cls)
        obj._value_ = value
        obj.rank = rank
        return obj

    def __eq__(self, other):
        if self.__class__ is other.__class__:
            return self.value == other.value
        return NotImplemented

    def __ne__(self, other):
        if self.__class__ is other.__class__:
            return self.value != other.value
        return NotImplemented

    def __ge__(self, other):
        if self.__class__ is other.__class__:
            return self.rank >= other.rank
        return NotImplemented

    def __gt__(self, other):
        if self.__class__ is other.__class__:
            return self.rank > other.rank
        return NotImplemented

    def __le__(self, other):
        if self.__class__ is other.__class__:
            return self.rank <= other.rank
        return NotImplemented

    def __lt__(self, other):
        if self.__class__ is other.__class__:
            return self.rank < other.rank
        return NotImplemented


class OrderStatus(Enum):
    NONE = ("NONE", SimpleOrderStatus.NONE, -1)
    ANY = ("ANY", SimpleOrderStatus.NONE, -1)
    RECEIVED = ('RECEIVED', SimpleOrderStatus.RECEIVED, 0)
    VALID = ('VALID', SimpleOrderStatus.RECEIVED, 1)
    UNPAID = ('UNPAID', SimpleOrderStatus.RECEIVED, 2)
    READY = ('READY', SimpleOrderStatus.RECEIVED, 3)
    ACCEPTED = ('ACCEPTED', SimpleOrderStatus.RECEIVED, 4)
    REJECTED = ('REJECTED', SimpleOrderStatus.REJECTED, 4)
    ENTERED = ('ENTERED', SimpleOrderStatus.PENDING, 5)
    PARTIALLY_SHIPPED = ('PARTIALLY_SHIPPED', SimpleOrderStatus.COMPLETE, 6)
    SHIPPED = ('SHIPPED', SimpleOrderStatus.COMPLETE, 6)
    CANCELLED = ('CANCELLED', SimpleOrderStatus.COMPLETE, 7)

    @staticmethod
    def for_(order_status):
        """
        Convert variable to an OrderStatus type. Will pass an actual OrderStatus through.
        :param order_status: string or OrderStatus object
        :return: OrderStatus
        """
        if isinstance(order_status, str):
            _s = [s for s in OrderStatus if s.value == order_status]
            if _s:
                return _s[0]
            else:
                return OrderStatus.NONE
            # _status = OrderStatus(order_status)
        else:
            _status = order_status
        return _status

    @staticmethod
    def all():
        return [s for s in OrderStatus]

    def __new__(cls, value, simple_status: SimpleOrderStatus, rank: int):
        obj = object.__new__(cls)
        obj._value_ = value
        obj.simple_status = simple_status
        obj.rank = rank
        return obj

    def __eq__(self, other):
        if self.__class__ is other.__class__:
            return self.value == other.value
        return NotImplemented

    def __ne__(self, other):
        if self.__class__ is other.__class__:
            return self.value != other.value
        return NotImplemented

    def __ge__(self, other):
        if self.__class__ is other.__class__:
            return self.rank >= other.rank
        return NotImplemented

    def __gt__(self, other):
        if self.__class__ is other.__class__:
            return self.rank > other.rank
        return NotImplemented

    def __le__(self, other):
        if self.__class__ is other.__class__:
            return self.rank <= other.rank
        return NotImplemented

    def __lt__(self, other):
        if self.__class__ is other.__class__:
            return self.rank < other.rank
        return NotImplemented