From 70a7caa21ae49b3f52f292900595173cefc42f7a Mon Sep 17 00:00:00 2001 From: Ahtesham Quraish Date: Wed, 13 Nov 2024 13:54:49 +0500 Subject: [PATCH] fix: create util of get_line_item_attribute Description Create util for get_line_item_attribute SONIC --- .../commercetools/catalog_info/edx_utils.py | 20 ++++++++++++++++--- .../apps/commercetools/sub_messages/tasks.py | 20 ++++--------------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/commerce_coordinator/apps/commercetools/catalog_info/edx_utils.py b/commerce_coordinator/apps/commercetools/catalog_info/edx_utils.py index ef4f74eff..ab442c97f 100644 --- a/commerce_coordinator/apps/commercetools/catalog_info/edx_utils.py +++ b/commerce_coordinator/apps/commercetools/catalog_info/edx_utils.py @@ -40,11 +40,25 @@ def is_edx_lms_order(order: CTOrder) -> bool: return len(get_edx_items(order)) >= 1 +def get_line_item_attribute(in_line_item, in_attribute_name): # pragma no cover + """Utility to get line item's attribute's value.""" + attribute_value = None + for attribute in in_line_item.variant.attributes: + if attribute.name == in_attribute_name and hasattr(attribute, 'value'): + if isinstance(attribute.value, dict): + attribute_value = attribute.value.get('label', None) + elif isinstance(attribute.value, str): + attribute_value = attribute.value + break + + return attribute_value + + def get_course_mode_from_ct_order(line_item: CTLineItem) -> str: + course_mode = get_line_item_attribute(line_item, 'mode') mode = 'verified' - for attribute in line_item.variant.attributes: - if attribute.name == 'mode': - mode = attribute.value + if course_mode is not None: + mode = course_mode return mode diff --git a/commerce_coordinator/apps/commercetools/sub_messages/tasks.py b/commerce_coordinator/apps/commercetools/sub_messages/tasks.py index 2ba60a7d7..d08f8b163 100644 --- a/commerce_coordinator/apps/commercetools/sub_messages/tasks.py +++ b/commerce_coordinator/apps/commercetools/sub_messages/tasks.py @@ -17,6 +17,7 @@ get_edx_order_workflow_state_key, get_edx_payment_intent_id, get_edx_product_course_run_key, + get_line_item_attribute, is_edx_lms_order ) from commerce_coordinator.apps.commercetools.clients import CommercetoolsAPIClient @@ -203,19 +204,6 @@ def fulfill_order_returned_signal_task( ): """Celery task for an order return (and refunded) message.""" - def _get_line_item_attribute(in_line_item, in_attribute_name): # pragma no cover - """Utility to get line item's attribute's value.""" - attribute_value = None - for attribute in in_line_item.variant.attributes: - if attribute.name == in_attribute_name and hasattr(attribute, 'value'): - if isinstance(attribute.value, dict): - attribute_value = attribute.value.get('label', None) - elif isinstance(attribute.value, str): - attribute_value = attribute.value - break - - return attribute_value - def _cents_to_dollars(in_amount): return in_amount.cent_amount / pow( 10, in_amount.fraction_digits @@ -300,10 +288,10 @@ def _prepare_segment_event_properties(in_order, return_line_item_return_id): 'name': line_item.name['en-US'], 'price': _cents_to_dollars(line_item.price.value), 'quantity': line_item.quantity, - 'category': _get_line_item_attribute(line_item, 'primary-subject-area'), + 'category': get_line_item_attribute(line_item, 'primary-subject-area'), 'image_url': line_item.variant.images[0].url if line_item.variant.images else None, - 'brand': _get_line_item_attribute(line_item, 'brand-text'), - 'url': _get_line_item_attribute(line_item, 'url-course'), + 'brand': get_line_item_attribute(line_item, 'brand-text'), + 'url': get_line_item_attribute(line_item, 'url-course'), 'lob': 'edX', # TODO: Decision was made to hardcode this value for phase 1. 'product_type': line_item.product_type.obj.name if hasattr(line_item.product_type.obj, 'name') else None