diff --git a/content.py b/content.py index 1e4bd8daf..aba27be6d 100644 --- a/content.py +++ b/content.py @@ -21,6 +21,7 @@ from utils import dangerousMarkup from utils import isPGPEncrypted from utils import containsPGPPublicKey from utils import acctDir +from utils import isfloat from petnames import getPetName @@ -1086,3 +1087,76 @@ def limitRepeatedWords(text: str, maxRepeats: int) -> str: for word, item in replacements.items(): text = text.replace(item[0], item[1]) return text + + +def getPriceFromString(priceStr: str) -> (str, str): + """Returns the item price and currency + """ + currencies = { + "J$": "JMD", + "£": "GBP", + "€": "EUR", + "؋": "AFN", + "ƒ": "AWG", + "₼": "AZN", + "Br": "BYN", + "BZ$": "BZD", + "$b": "BOB", + "KM": "BAM", + "P": "BWP", + "лв": "BGN", + "R$": "BRL", + "៛": "KHR", + "$U": "UYU", + "RD$": "DOP", + "$": "USD", + "₡": "CRC", + "kn": "HRK", + "₱": "CUP", + "Kč": "CZK", + "kr": "NOK", + "¢": "GHS", + "Q": "GTQ", + "L": "HNL", + "Ft": "HUF", + "Rp": "IDR", + "﷼": "IRR", + "₪": "ILS", + "¥": "JPY", + "₩": "KRW", + "₭": "LAK", + "ден": "MKD", + "RM": "MYR", + "₨": "MUR", + "₮": "MNT", + "MT": "MZN", + "C$": "NIO", + "₦": "NGN", + "Gs": "PYG", + "zł": "PLN", + "lei": "RON", + "₽": "RUB", + "Дин": "RSD", + "S": "SOS", + "R": "ZAR", + "CHF": "CHF", + "NT$": "TWD", + "฿": "THB", + "TT$": "TTD", + "₴": "UAH", + "Bs": "VEF", + "₫": "VND", + "Z$": "ZQD" + } + for symbol, name in currencies.items(): + if symbol in priceStr: + price = priceStr.replace(symbol, '') + if isfloat(price): + return price, name + elif name in priceStr: + price = priceStr.replace(name, '') + if isfloat(price): + return price, name + if isfloat(priceStr): + return priceStr, "EUR" + return "0.00", "EUR" diff --git a/daemon.py b/daemon.py index a7048eb1f..63d9acfb1 100644 --- a/daemon.py +++ b/daemon.py @@ -277,6 +277,7 @@ from utils import hasGroupType from manualapprove import manualDenyFollowRequest from manualapprove import manualApproveFollowRequest from announce import createAnnounce +from content import getPriceFromString from content import replaceEmojiFromTags from content import addHtmlTags from content import extractMediaInFormPOST @@ -13920,10 +13921,10 @@ class PubServer(BaseHTTPRequestHandler): if not fields.get('itemType'): print(postType + ' no itemType') return -1 - if not fields.get('itemPrice'): + if 'itemPrice' not in fields: print(postType + ' no itemPrice') return -1 - if not fields.get('itemCurrency'): + if 'itemCurrency' not in fields: print(postType + ' no itemCurrency') return -1 if not fields.get('category'): @@ -13949,10 +13950,10 @@ class PubServer(BaseHTTPRequestHandler): if isfloat(fields['itemQty']): itemQty = float(fields['itemQty']) itemPrice = "0.00" - if fields['itemPrice']: - if isfloat(fields['itemPrice']): - itemPrice = fields['itemPrice'] itemCurrency = "EUR" + if fields['itemPrice']: + itemPrice, itemCurrency = \ + getPriceFromString(fields['itemPrice']) if fields['itemCurrency']: itemCurrency = fields['itemCurrency'] print('Adding shared item') diff --git a/tests.py b/tests.py index 6b4a5ab18..ce56fa060 100644 --- a/tests.py +++ b/tests.py @@ -104,6 +104,7 @@ from inbox import jsonPostAllowsComments from inbox import validInbox from inbox import validInboxFilenames from categories import guessHashtagCategory +from content import getPriceFromString from content import limitRepeatedWords from content import switchWords from content import extractTextFieldsInPOST @@ -5164,9 +5165,23 @@ def _testValidPassword(): assert validPassword('A!bc:defg1/234?56') +def _testGetPriceFromString() -> None: + print('testGetPriceFromString') + price, curr = getPriceFromString("5.23") + assert price == "5.23" + assert curr == "EUR" + price, curr = getPriceFromString("£7.36") + assert price == "7.36" + assert curr == "GBP" + price, curr = getPriceFromString("$10.63") + assert price == "10.63" + assert curr == "USD" + + def runAllTests(): print('Running tests...') updateDefaultThemesList(os.getcwd()) + _testGetPriceFromString() _testFunctions() _testDateConversions() _testAuthorizeSharedItems()