Properly format the amount

pull/2041/head
dgtlmoon 4 months ago
parent 52eb5609c0
commit 685624c23b

@ -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')

@ -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

@ -167,7 +167,7 @@
{% if watch.get('restock') and watch['restock']['price'] != None %}
{% if watch['restock']['price'] != None %}
<span class="restock-label price" title="Price">
{{ watch['restock']['price'] }} {{ watch['restock']['currency'] }}
{{ watch['restock']['price']|format_number_locale }} {{ watch['restock']['currency'] }}
</span>
{% endif %}
{% elif not watch.has_restock_info %}

@ -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

Loading…
Cancel
Save