Skip to content

Commit

Permalink
Added with fallback to get_formatted_value
Browse files Browse the repository at this point in the history
  • Loading branch information
bakeable committed May 30, 2024
1 parent ccea387 commit b170930
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
14 changes: 7 additions & 7 deletions akeneo_connector/akeneo_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def get_values(self, attribute: str) -> list[Value]:
"""
return self.values.get(attribute, [])

def get_value(self, attribute: str, locale: str | None = None, scope: str | None = None) -> any:
def get_value(self, attribute: str, locale: str | None = None, scope: str | None = None, with_fallback = False) -> any:
"""
Gets the value for the given attribute.
Expand All @@ -142,13 +142,13 @@ def get_value(self, attribute: str, locale: str | None = None, scope: str | None
return value.get('data')

# Return first value if locale is None and scope is None
if locale is None and scope is None:
if locale is None and scope is None or with_fallback:
return self.values[attribute][0].get('data')

# Return None if locale and scope are not found
return None

def get_linked_data(self, attribute: str, locale: str | None = None, scope: str | None = None) -> dict | None:
def get_linked_data(self, attribute: str, locale: str | None = None, scope: str | None = None, with_fallback = False) -> dict | None:
"""
Gets the linked data for the given attribute.
Expand All @@ -170,7 +170,7 @@ def get_linked_data(self, attribute: str, locale: str | None = None, scope: str
return value.get('linked_data', None)

# Return first value if locale is None and scope is None
if locale is None and scope is None:
if locale is None and scope is None or with_fallback:
return self.values[attribute][0].get('linked_data', None)

# Return None if locale and scope are not found
Expand All @@ -189,10 +189,10 @@ def get_formatted_value(self, attribute: str, locale: str | None = None, scope:
str: The value of the attribute. "N/A" if not found.
"""
# Get value
value = self.get_value(attribute, locale, scope)
value = self.get_value(attribute, locale, scope, with_fallback=True)

# Get linked data
linked_data = self.get_linked_data(attribute, locale, scope)
linked_data = self.get_linked_data(attribute, locale, scope, with_fallback=True)

# Return formatted value
return format_value(value, locale, linked_data)
Expand Down
5 changes: 4 additions & 1 deletion akeneo_connector/akeneo_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,10 @@ def format_number(number: int | float, locale_name: str | None = None) -> str:

# Format the number
if isinstance(number, int):
formatted_number = locale.format_string("%d", number, grouping=True)
if number > 1000000000: # Prevent barcodes from being formatted wrong
formatted_number = str(number)
else:
formatted_number = locale.format_string("%d", number, grouping=True)
else:
formatted_number = locale.format_string("%f", number, grouping=True)

Expand Down

0 comments on commit b170930

Please sign in to comment.