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 / config.py
Size: Mime:
from pprint import pformat
from enum import Enum
from .fedex import FedexServiceType, FedexWeightType
from aws_xray_sdk.core import xray_recorder
from lib_b2b.order_flow import OrderFlowType


class Config:
    default_config={}

    def __init__(self, *initial_data, **kwargs):
        for key in self.default_config.keys():
            setattr(self, key, self.default_config[key])
        for dictionary in initial_data:
            if dictionary:
                for key in dictionary:
                    setattr(self, key, dictionary[key])
        for key in kwargs:
            setattr(self, key, kwargs[key])

    @xray_recorder.capture()
    def update(self, *initial_data, **kwargs):
        for dictionary in initial_data:
            for key in dictionary:
                setattr(self, key, dictionary[key])
        for key in kwargs:
            setattr(self, key, kwargs[key])

    def get(self, attr, default):
        try:
            return getattr(self, attr, default)
        except Exception as e:
            return None

    def __getattr__(self, item):
        return None

    def __repr__(self):
        return "<" + type(self).__name__ + "> " + pformat(vars(self), indent=2, width=1)

    def __str__(self):
        return pformat(vars(self), indent=2, width=1)


class IntegrationType(Enum):
    SHOPIFY = 'shopify'
    STANDARD = 'standard'


class IntegrationConfig(Config):
    default_config = {
        'send_ack_to_url_from_order': True,
        'send_notice_to_url_from_order': True,
        'send_fulfillment_on_ship': False,
        'capture_payment_on_ship': False,
        "requires_pricing": False,
        'cancel_order_on_reject': False,
        'order_flow_type': OrderFlowType.MANUAL.value
    }

    @staticmethod
    @xray_recorder.capture()
    def __get_default_config_for(integration_type: IntegrationType):
        if integration_type == IntegrationType.SHOPIFY:
            return IntegrationConfig({
                'send_ack_to_url_from_order': False,
                'send_notice_to_url_from_order': False,
                'send_fulfillment_on_ship': True,
                'capture_payment_on_ship': False,
                'cancel_order_on_reject': False,
                'order_flow_type': OrderFlowType.MANUAL.value
            })
        else:
            return IntegrationConfig({
                'send_ack_to_url_from_order': True,
                'send_notice_to_url_from_order': True,
                'send_fulfillment_on_ship': False,
                'capture_payment_on_ship': False,
                'cancel_order_on_reject': False,
                'order_flow_type': OrderFlowType.MANUAL.value
            })

    @staticmethod
    @xray_recorder.capture()
    def for_(customer, integration_type: IntegrationType=None):
        if integration_type:
            customer_integration_type = IntegrationType(customer.get('integration_type', 'standard'))
            if integration_type == customer_integration_type:
                customer_integration_config = customer.get('integration_config')
                if customer_integration_config:
                    return IntegrationConfig(customer_integration_config)
                else:
                    return IntegrationConfig.__get_default_config_for(customer_integration_type)
            else:
                return IntegrationConfig.__get_default_config_for(integration_type)
        else:
            customer_integration_config = customer.get('integration_config')
            if customer_integration_config:
                return IntegrationConfig(customer_integration_config)
            else:
                customer_integration_type = IntegrationType(customer.get('integration_type', 'standard'))
                return IntegrationConfig.__get_default_config_for(customer_integration_type)


class SenderConfig(Config):
    default_config = {
        'company': '',
        'address1': '199 Woltz St',
        'city': 'Mt. Airy',
        'state': 'NC',
        'zip': '27030',
        'country': 'US',
        'phone': '(336)789-9161'
    }


class FulfillmentConfig(Config):
    default_config = {
        'is_serialized': True,
        'is_lotted': False,
        'reject_if_order_is_shipped': True,
        'reject_if_serial_is_shipped': True,
        'reject_if_cus_po_is_shipped': True,
        'reject_if_exceeds_max_package_size': True,
        'shipment_request_update_allowed': True,
        'max_package_size': 165,
        'minimum_package_weight': 1,
        'maximum_package_weight': 150,
        'dimensional_weight_divisor': 175,
        'use_standard_weight': False,
        'carrier_type': 'FEDEX_SHIP_MGR',
        'free_shipping': False,
        'validate_address': False
    }

    def __init__(self, *initial_data, **kwargs):
        super().__init__(*initial_data, **kwargs)
        fac = getattr(self, 'fedex_api_config', None)
        if fac:
            _fac = FedexAPIConfig(fac)
            setattr(self, 'fedex_api_config', _fac)

    def get_carrier_type(self):
        from .carrier import CarrierType
        ct = getattr(self, 'carrier_type', 'FEDEX_SHIP_MGR')
        return CarrierType(ct)

    def get_service_type(self, weight: float):
        return FedexServiceType.FEDEX_GROUND if weight > 70 else FedexServiceType.GROUND_HOME_DELIVERY

    def get_weight_type(self, units):
        return FedexWeightType[units]


class FedexAPIConfig(Config):
    # This config contains the appropriate credentials for using the test service
    default_config = {
        'key': 'LCEVKDlFszwcb9X5',
        'password': 'OV84AL2eSvqB4IJSzanLWO3SL',
        'meter_number': '119075998',
        'account_number': '510087720',
        'use_test_mode': True,
        'label_type': 'STOCK_4X6',
        'label_orientation': 'BOTTOM_EDGE_OF_TEXT_FIRST',
        'physical_packaging': 'BOX',
        'request_pickup': False,
        'printer_name': 'test-printer',
        'printer_ip_address': '10.11.4.104',
        'printer_port': '9100',
        'storage_bucket': 'bmc-ecommerce-labels',
        'storage_path': 'TestAPIProfile',
        'print_immediately': True
    }


class ShopifyConfig(Config):
    default_config = {
        "example_url": None,
        "key": None,
        "password": None,
        "shared_secret": None,
        "default_location": None,
        "domain": None,
        "secret": None
    }