Repository URL to install this package:
|
Version:
0.4.190 ▾
|
lib-py-b2b
/
profile.py
|
|---|
from .customer import Customer
import logging
from os import environ
from .errors import ConfigurationError
from .address import Address
from .config import FulfillmentConfig, IntegrationConfig, SenderConfig, IntegrationType, ShopifyConfig
from pprint import pformat
from aws_xray_sdk.core import xray_recorder
logger = logging.getLogger(__name__)
class Profile:
@staticmethod
@xray_recorder.capture()
def profile_for(customer, integration_type=None):
"""
Get a Profile object for the customer and optionally the integration_type. The IntegrationConfig returned in the profile
will match the integration_type if passed. Otherwise, it will match the integration_type for the customer. This enables
a transition period when changing a customer's integration type by allowing fulfillments to have a different integration_type
than the customer.
:param customer: The customer object retrieved using the Customer static methods
:param integration_type: either a str or IntegrationType, not required
:return: a profile object that contains relevant configurations for a customer, carrier, etc.
"""
if isinstance(customer, str):
_customer = Customer.fetch_by_edi_id(customer)
else:
_customer = customer
if integration_type and isinstance(integration_type, str):
_integration_type = IntegrationType(integration_type)
else:
_integration_type = integration_type
return Profile(customer=_customer, integration_type=_integration_type)
def __init__(self, customer, integration_type=None):
if isinstance(customer, str):
self.customer = Customer.fetch_by_edi_id(customer)
else:
self.customer = customer
if integration_type and isinstance(integration_type, str):
_integration_type = IntegrationType(integration_type)
else:
_integration_type = integration_type
self.config = {}
for key in self.customer:
self.config[key] = self.customer[key]
self.config['integration_config'] = IntegrationConfig.for_(self.customer, _integration_type)
self.config['sender'] = SenderConfig(self.customer.get('sender'))
self.config['fulfillment_config'] = FulfillmentConfig(self.customer.get('fulfillment_config'))
self.config['department'] = self.customer.get('department') or 'BLD-D'
if self.customer.get('shopify_config'):
self.config['shopify_config'] = ShopifyConfig(self.customer.get('shopify_config'))
self.config['customer_service_center'] = 'MTAD'
self.config['fulfillment_center'] = 'MTAD'
def __get_config_property(self, property_name):
if property_name in self.config:
return self.config[property_name]
else:
raise ConfigurationError(f"Required configuration field '{property_name}' was not found in the configuration for customer [{self.customer['customer_edi_id']}].")
def __resolve(self, property_path):
"""
resolves a property with dot notation into a descending series of properties
:param property_path: the '.' seperated property string
:return:
"""
obj = None
for attr in property_path.split("."):
obj = getattr(self, attr)
return obj
def __getattr__(self, item):
"""
Checks for property on the customer record. If not there, uses default values.
Will resolve dot notation for paths
"""
obj = self.config
for attr in item.split("."):
try:
obj = obj[attr]
except (TypeError, KeyError):
obj = getattr(obj, attr)
return obj
@property
def carrier_type(self):
from .carrier import CarrierType
ct = self.config.get('carrier_type')
return CarrierType[ct]
@property
def sender_address(self):
s = self.config.get('sender')
a = Address.from_dict(s)
return a
def get_department(self):
return self.config.get('department')
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)