Skip to content

Commit

Permalink
fix: move the get order util to util
Browse files Browse the repository at this point in the history
Description:
Move the get order util to util folder
SONIC
  • Loading branch information
Ahtesham Quraish authored and Ahtesham Quraish committed Nov 14, 2024
1 parent 70a7caa commit 299ca29
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 33 deletions.
23 changes: 0 additions & 23 deletions commerce_coordinator/apps/commercetools/catalog_info/edx_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,6 @@ 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'
if course_mode is not None:
mode = course_mode

return mode


def get_edx_lms_user_id(customer: CTCustomer):
return customer.custom.fields[EdXFieldNames.LMS_USER_ID]

Expand Down
24 changes: 24 additions & 0 deletions commerce_coordinator/apps/commercetools/catalog_info/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from commercetools.platform.models import Attribute as CTAttribute
from commercetools.platform.models import CentPrecisionMoney as CTCentPrecisionMoney
from commercetools.platform.models import HighPrecisionMoney as CTHighPrecisionMoney
from commercetools.platform.models import LineItem as CTLineItem
from commercetools.platform.models import LocalizedString as CTLocalizedString
from commercetools.platform.models import MoneyType as CTMoneyType
from commercetools.platform.models import Price as CTPrice
Expand Down Expand Up @@ -32,6 +33,29 @@ def ls(string_dict: LSLike) -> CTLocalizedString:
return string_dict


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'
if course_mode is not None:
mode = course_mode

return mode


def ls_eq(a: LSLike, b: LSLike) -> bool:
if a is None or b is None:
return False
Expand Down
6 changes: 4 additions & 2 deletions commerce_coordinator/apps/commercetools/sub_messages/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@

from commerce_coordinator.apps.commercetools.catalog_info.constants import TwoUKeys
from commerce_coordinator.apps.commercetools.catalog_info.edx_utils import (
get_course_mode_from_ct_order,
get_edx_is_sanctioned,
get_edx_items,
get_edx_lms_user_id,
get_edx_lms_user_name,
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.catalog_info.utils import (
get_course_mode_from_ct_order,
get_line_item_attribute
)
from commerce_coordinator.apps.commercetools.clients import CommercetoolsAPIClient
from commerce_coordinator.apps.commercetools.constants import EMAIL_NOTIFICATION_CACHE_TTL_SECS
from commerce_coordinator.apps.commercetools.filters import OrderRefundRequested
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from commercetools.platform.models import Order as CTOrder

from commerce_coordinator.apps.commercetools.catalog_info.edx_utils import (
get_course_mode_from_ct_order,
get_edx_items,
get_edx_lms_user_id,
get_edx_lms_user_name,
Expand Down Expand Up @@ -59,9 +58,6 @@ def test_get_edx_items(self):
def test_is_edx_lms_order(self):
self.assertTrue(is_edx_lms_order(self.order))

def test_get_course_mode_from_ct_order(self):
self.assertEqual(get_course_mode_from_ct_order(get_edx_items(self.order)[0]), 'certified')

def test_get_edx_lms_user_id(self):
self.assertEqual(get_edx_lms_user_id(self.user), DEFAULT_EDX_LMS_USER_ID)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
""" Commcercetools API Utilities """
from typing import Union
from unittest import TestCase

import ddt
import pytest
from commercetools.platform.models import CentPrecisionMoney, HighPrecisionMoney, Price
from commercetools.platform.models import CentPrecisionMoney, HighPrecisionMoney
from commercetools.platform.models import Order as CTOrder
from commercetools.platform.models import Price
from currencies import Currency

from commerce_coordinator.apps.commercetools.catalog_info.constants import Languages
from commerce_coordinator.apps.commercetools.catalog_info.edx_utils import get_edx_items
from commerce_coordinator.apps.commercetools.catalog_info.utils import (
attribute_dict,
get_course_mode_from_ct_order,
ls,
ls_eq,
price_to_string,
typed_money_add,
typed_money_to_string,
un_ls
)
from commerce_coordinator.apps.core.tests.utils import name_test
from commerce_coordinator.apps.commercetools.tests.conftest import gen_order
from commerce_coordinator.apps.core.tests.utils import name_test, uuid4_str

# <Country>_<Currency>
JAPAN_YEN = "JPY" # 0 fractional digits
Expand All @@ -29,12 +34,25 @@
class LocalizedStringsTests(TestCase):
""" Localized String Utility Tests"""

order: Union[CTOrder, None]

def setUp(self):
self.order = gen_order(uuid4_str())
super().setUp()

def tearDown(self):
self.order = None
super().tearDown()

# ls()
def test_single_unknown_key_ls_creation(self):
string = "test"
result = ls({'ZZ': string})
self.assertEqual(result, {'ZZ': string, Languages.ENGLISH: string, Languages.US_ENGLISH: string})

def test_get_course_mode_from_ct_order(self):
self.assertEqual(get_course_mode_from_ct_order(get_edx_items(self.order)[0]), 'professional')

def test_single_key_ls_creation(self):
string = "test-2"
result = ls({Languages.ENGLISH: string})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
},
{
"name": "mode",
"value": "certified"
"value": "professional"
},
{
"name": "date-created",
Expand Down

0 comments on commit 299ca29

Please sign in to comment.