mathesar icon indicating copy to clipboard operation
mathesar copied to clipboard

Convert the currency_info dataset to align with Money type Display options

Open silentninja opened this issue 2 years ago • 10 comments

Problem

#1205 Introduces display options for the Mathesar_Money type. Money type has display options like currency symbol, number_format etc. We are able to infer the currency symbol from the imported data. Often currencies tend to follow the number formats of their country or have a predefined format which is followed when representing them, so we should set the default values for other display options based on the standard representation format of the inferred currency.

Proposed solution

We have a dataset (currency_info.json) extracted from locale files, it contains currency symbols and the standards for that currency symbol, this information can be converted into values that align with our display options which could then be used to set predefined display option values.

Additional context

  • Blocked by #1205
  • Money Display options API spec and related discussion #1243
  • Helpful links to get human readable locale name
    • https://gist.github.com/jacobbubu/1836273
    • https://gist.github.com/SergLam/a4ed0463cce5c5b32339bb351f8b7815
  • Rough Code to get currency codes mapped to currency symbol with the same data

import json

with open("currency_info.json", "r") as f:
    currency_info = json.loads(f.read())
duplicates_map = {}
for currency_code, currency_details in currency_info.items():
    if currency_details['currency_symbol'] in duplicates_map:
        not_exist = True
        for code, details in duplicates_map[currency_details['currency_symbol']].items():
            if details['data'] == currency_details:
                not_exist = False
                duplicates_map[currency_details['currency_symbol']][code]['codes'].append(currency_code)

        if not_exist:
            duplicates_map[currency_details['currency_symbol']][currency_code] = {
                'codes': [currency_code], 'data': currency_details
            }
    else:
        duplicates_map[currency_details['currency_symbol']] = {
            currency_code: {'codes': [currency_code], 'data': currency_details}
        }
print(duplicates_map)

silentninja avatar Mar 23 '22 12:03 silentninja

@silentninja can I work on this?

ratika-12 avatar Mar 24 '22 15:03 ratika-12

Sure @ratika-12. I have assigned the issue to you. Since the issue depends on #1205, please use mathesar-1105-money-display-option as your base branch. Thanks for the help!

silentninja avatar Mar 24 '22 16:03 silentninja

@ratika-12 are you still working on this?

@silentninja is this even still relevant?

kgodey avatar Apr 29 '22 14:04 kgodey

@kgodey If this issue is still relevant, you may assign this to me.

shivams avatar May 11 '22 18:05 shivams

Go ahead @shivams

kgodey avatar May 11 '22 18:05 kgodey

The issue is still relevant, @silentninja made some updates to the description and the latest issue description should be accurate.

kgodey avatar May 11 '22 18:05 kgodey

I've looked into this issue. The following rough code extracts the information from currency_info.json and returns the display options as defined in https://github.com/centerofci/mathesar/issues/1105:

import json

currency = getCurrency() #some arbitrary function that infers the currency's ISO code, e.g. 'en_US.ISO8859-1'

with open("currency_info.json", "r") as info_file:
    currency_info = json.load(info_file)

display = dict()
display['symbol'] = currency_info[currency]['currency_symbol']
display['symbol_location'] = 1 if currency_info[currency]['p_cs_precedes'] else -1
display['decimal_symbol'] = currency_info[currency]['mon_decimal_point']
display['digit_symbol'] = currency_info[currency]['mon_thousands_sep']
display['digit_grouping'] = currency_info[currency]['mon_grouping']
return display

I see that the issue is easy. However, I still have a few doubts:

  1. Where should I integrate the logic? Should it go into mathesar/api/display_options.py? Or, should I create a new json which contains the information extracted from currency_info.json in our desired display option format?
  2. What do we expect as the input? Do we expect the currency symbol (e.g. $) or do we expect the currency ISO code as the input (e.g. en_US.ISO8859-1)? I see that we're inferring the currency symbol. However, a single currency symbol has multiple ISO codes associated with it. Which ISO code should I choose then?
  3. The digit_grouping part needs some more clarifications. I see that we're abiding by the .NET standard for this. In the currency_info.json file, however, which standard is being followed for interpreting this grouping array? It seems to me, it is this standard, however its interpretation isn't compatible with the arrays given in the json file. I am a little confused regarding this.

shivams avatar May 12 '22 21:05 shivams

@shivams https://github.com/centerofci/mathesar/discussions/1243#discussioncomment-2519195 has the display options spec for MathesarMoney type. We need to be able to pre-fill those display options(currency_symbol_location and number_format) with the correct value.

We infer the currency data in this line https://github.com/centerofci/mathesar/blob/b2467dcf63de786c0e8063821ff317ef608ec3a6/mathesar/utils/display_options_inference.py#L35

As you can see, we are able to infer the currency symbol but symbol_location and number_format values are hardcoded, rather they should be replaced with the correct values based on the currency symbol.

Current currency_info.json does not contain any of the possible values rather it has information useful to derive the values we need, so we need to rework the currency_info.json file. For example

  "en_US.ISO8859-1": {
    "int_curr_symbol": "USD ",
    "currency_symbol": "$",
    "mon_decimal_point": ".",
    "mon_thousands_sep": ",",
    "mon_grouping": [
      3,
      3,
      0
    ],
    "positive_sign": "",
    "negative_sign": "-",
    "int_frac_digits": 2,
    "frac_digits": 2,
    "p_cs_precedes": 1,
    "p_sep_by_space": 0,
    "n_cs_precedes": 1,
    "n_sep_by_space": 0,
    "p_sign_posn": 1,
    "n_sign_posn": 1,
    "decimal_point": ".",
    "thousands_sep": ",",
    "grouping": [
      3,
      3,
      0
    ]
  },

should be replaced with

 {"currency_symbol": "$", 
 "number_format": 'english',
 "currency_symbol_location": 'after-minus'
}

Other information can be discarded.

silentninja avatar May 13 '22 18:05 silentninja

I would like to take up this issue if possible.

abhishak3 avatar Jul 24 '22 08:07 abhishak3

Go ahead @abhishak3. :)

kgodey avatar Jul 25 '22 14:07 kgodey

@abhishak3 are you still working on this?

kgodey avatar Sep 01 '22 15:09 kgodey

I am unable to wrap my head around this issue. You can unassign me for the time being.

abhishak3 avatar Sep 02 '22 07:09 abhishak3

Thanks @abhishak3. Let me know if you'd like an issue that's better specced out to work on.

kgodey avatar Sep 06 '22 17:09 kgodey

This issue has not been updated in 90 days and is being marked as stale.

github-actions[bot] avatar Apr 16 '23 21:04 github-actions[bot]

Closing this, too old, requirements are likely out of date. We can create a new issue if we need this functionality in the future.

kgodey avatar Feb 02 '24 17:02 kgodey