Get currency symbol from price

main
Bob Mottram 2021-08-07 18:03:41 +01:00
parent e042b47721
commit 8cb31a10a8
3 changed files with 95 additions and 5 deletions

View File

@ -21,6 +21,7 @@ from utils import dangerousMarkup
from utils import isPGPEncrypted from utils import isPGPEncrypted
from utils import containsPGPPublicKey from utils import containsPGPPublicKey
from utils import acctDir from utils import acctDir
from utils import isfloat
from petnames import getPetName from petnames import getPetName
@ -1086,3 +1087,76 @@ def limitRepeatedWords(text: str, maxRepeats: int) -> str:
for word, item in replacements.items(): for word, item in replacements.items():
text = text.replace(item[0], item[1]) text = text.replace(item[0], item[1])
return text 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",
"": "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",
"": "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"

View File

@ -277,6 +277,7 @@ from utils import hasGroupType
from manualapprove import manualDenyFollowRequest from manualapprove import manualDenyFollowRequest
from manualapprove import manualApproveFollowRequest from manualapprove import manualApproveFollowRequest
from announce import createAnnounce from announce import createAnnounce
from content import getPriceFromString
from content import replaceEmojiFromTags from content import replaceEmojiFromTags
from content import addHtmlTags from content import addHtmlTags
from content import extractMediaInFormPOST from content import extractMediaInFormPOST
@ -13920,10 +13921,10 @@ class PubServer(BaseHTTPRequestHandler):
if not fields.get('itemType'): if not fields.get('itemType'):
print(postType + ' no itemType') print(postType + ' no itemType')
return -1 return -1
if not fields.get('itemPrice'): if 'itemPrice' not in fields:
print(postType + ' no itemPrice') print(postType + ' no itemPrice')
return -1 return -1
if not fields.get('itemCurrency'): if 'itemCurrency' not in fields:
print(postType + ' no itemCurrency') print(postType + ' no itemCurrency')
return -1 return -1
if not fields.get('category'): if not fields.get('category'):
@ -13949,10 +13950,10 @@ class PubServer(BaseHTTPRequestHandler):
if isfloat(fields['itemQty']): if isfloat(fields['itemQty']):
itemQty = float(fields['itemQty']) itemQty = float(fields['itemQty'])
itemPrice = "0.00" itemPrice = "0.00"
if fields['itemPrice']:
if isfloat(fields['itemPrice']):
itemPrice = fields['itemPrice']
itemCurrency = "EUR" itemCurrency = "EUR"
if fields['itemPrice']:
itemPrice, itemCurrency = \
getPriceFromString(fields['itemPrice'])
if fields['itemCurrency']: if fields['itemCurrency']:
itemCurrency = fields['itemCurrency'] itemCurrency = fields['itemCurrency']
print('Adding shared item') print('Adding shared item')

View File

@ -104,6 +104,7 @@ from inbox import jsonPostAllowsComments
from inbox import validInbox from inbox import validInbox
from inbox import validInboxFilenames from inbox import validInboxFilenames
from categories import guessHashtagCategory from categories import guessHashtagCategory
from content import getPriceFromString
from content import limitRepeatedWords from content import limitRepeatedWords
from content import switchWords from content import switchWords
from content import extractTextFieldsInPOST from content import extractTextFieldsInPOST
@ -5164,9 +5165,23 @@ def _testValidPassword():
assert validPassword('A!bc:defg1/234?56') 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(): def runAllTests():
print('Running tests...') print('Running tests...')
updateDefaultThemesList(os.getcwd()) updateDefaultThemesList(os.getcwd())
_testGetPriceFromString()
_testFunctions() _testFunctions()
_testDateConversions() _testDateConversions()
_testAuthorizeSharedItems() _testAuthorizeSharedItems()