From 685624c23bfa98832987601592624b0689777ca1 Mon Sep 17 00:00:00 2001 From: dgtlmoon Date: Thu, 11 Jul 2024 15:23:39 +0200 Subject: [PATCH] Properly format the amount --- changedetectionio/flask_app.py | 20 ++++++++++++---- .../processors/restock_diff/__init__.py | 23 ++++++++++++++++++- .../templates/watch-overview.html | 2 +- requirements.txt | 3 +++ 4 files changed, 41 insertions(+), 7 deletions(-) diff --git a/changedetectionio/flask_app.py b/changedetectionio/flask_app.py index 35799c42..35099d7f 100644 --- a/changedetectionio/flask_app.py +++ b/changedetectionio/flask_app.py @@ -1,12 +1,14 @@ #!/usr/bin/python3 import datetime +import flask_login +import locale import os +import pytz import queue import threading import time - -from jinja2 import Template +import timeago from .processors import find_processors, get_parent_module, get_custom_watch_obj_for_processor from .safe_jinja import render as jinja_render @@ -14,9 +16,7 @@ from changedetectionio.strtobool import strtobool from copy import deepcopy from functools import wraps from threading import Event -import flask_login -import pytz -import timeago + from feedgen.feed import FeedGenerator from flask import ( Flask, @@ -112,6 +112,16 @@ def get_darkmode_state(): def get_css_version(): return __version__ +@app.template_filter('format_number_locale') +def _jinja2_filter_format_number_locale(value: float) -> str: + "Formats for example 4000.10 to the local locale default of 4,000.10" + default_locale = locale.getdefaultlocale() + locale.setlocale(locale.LC_ALL, default_locale) + # Format the number with two decimal places + formatted_value = locale.format_string("%.2f", value, grouping=True) + + return formatted_value + # We use the whole watch object from the store/JSON so we can see if there's some related status in terms of a thread # running or something similar. @app.template_filter('format_last_checked_time') diff --git a/changedetectionio/processors/restock_diff/__init__.py b/changedetectionio/processors/restock_diff/__init__.py index 7ea6f2e7..49e375b1 100644 --- a/changedetectionio/processors/restock_diff/__init__.py +++ b/changedetectionio/processors/restock_diff/__init__.py @@ -1,8 +1,29 @@ from changedetectionio.model.Watch import model as BaseWatch import re +from babel.numbers import parse_decimal class Restock(dict): + + def parse_currency(self, raw_value: str) -> float: + # Clean and standardize the value + standardized_value = raw_value + + if ',' in standardized_value and '.' in standardized_value: + # Identify the correct decimal separator + if standardized_value.rfind('.') > standardized_value.rfind(','): + standardized_value = standardized_value.replace(',', '') + else: + standardized_value = standardized_value.replace('.', '').replace(',', '.') + else: + standardized_value = standardized_value.replace(',', '.') + + # Remove any non-numeric characters except for the decimal point + standardized_value = re.sub(r'[^\d.-]', '', standardized_value) + + # Convert to float + return float(parse_decimal(standardized_value, locale='en')) + def __init__(self, *args, **kwargs): # Define default values default_values = { @@ -26,7 +47,7 @@ class Restock(dict): # Custom logic to handle setting price and original_price if key == 'price': if isinstance(value, str): - value = re.sub(r'[^0-9.]', '', value.strip()) + value = self.parse_currency(raw_value=value) if value and not self.get('original_price'): self['original_price'] = value diff --git a/changedetectionio/templates/watch-overview.html b/changedetectionio/templates/watch-overview.html index 24f0f3a0..fac6179b 100644 --- a/changedetectionio/templates/watch-overview.html +++ b/changedetectionio/templates/watch-overview.html @@ -167,7 +167,7 @@ {% if watch.get('restock') and watch['restock']['price'] != None %} {% if watch['restock']['price'] != None %} - {{ watch['restock']['price'] }} {{ watch['restock']['currency'] }} + {{ watch['restock']['price']|format_number_locale }} {{ watch['restock']['currency'] }} {% endif %} {% elif not watch.has_restock_info %} diff --git a/requirements.txt b/requirements.txt index 2671f1a0..fcfdf774 100644 --- a/requirements.txt +++ b/requirements.txt @@ -86,5 +86,8 @@ loguru # For scraping all possible metadata relating to products so we can do better restock detection extruct +# For cleaning up unknown currency formats +babel + # Needed for > 3.10, https://github.com/microsoft/playwright-python/issues/2096 greenlet >= 3.0.3