Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept display options for MathesarMoney type #1205

Merged
merged 53 commits into from
May 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
a6cf38e
Create MoneyDisplayOptionSerializer and add display options to serial…
silentninja Mar 14, 2022
3d7eaa9
Add test cases test money type display options get and set validation…
silentninja Mar 15, 2022
d8b4155
Add Money type display options validation API exception and respectiv…
silentninja Mar 15, 2022
c7dadc5
Use currency_code to set other display options for money type.
silentninja Mar 16, 2022
0352ae8
Add currency json file to fetch the details of the currency based on …
silentninja Mar 16, 2022
14efb5f
Add display type options inference to the `type_suggestions` path
silentninja Mar 18, 2022
07f24db
Modify `/type_suggestions/` path response to include inferred display…
silentninja Mar 18, 2022
415b019
Add test case for inferring column display options
silentninja Mar 18, 2022
0e1fdac
Fix display options inference to infer proper data for currencies wit…
silentninja Mar 19, 2022
ea4cb7f
Add thefuzz library for string matching currency symbol inferred from…
silentninja Mar 20, 2022
f0c6d13
Add fuzzy matching currency symbols for money type display options
silentninja Mar 21, 2022
d39ace8
Remove unnecessary error handler bypass statement
silentninja Mar 21, 2022
048130a
Fix failing test cases due to addition of column in `type_inference.csv`
silentninja Mar 21, 2022
3a96a6d
Merge branch 'master' into mathesar-1105-money-display-option
silentninja Mar 21, 2022
f51492f
Fix accidental typo
silentninja Mar 22, 2022
dd79b46
Add test case for display options preview
silentninja Mar 22, 2022
8fa5077
Fix `mathesar.utils.display_options_inference.infer_table_column_disp…
silentninja Mar 22, 2022
da4c05d
Fix `mathesar.utils.display_options_inference.infer_table_column_disp…
silentninja Mar 22, 2022
b42d610
Remove unnecessary label alias for a column in `db.types.money.get_mo…
silentninja Mar 22, 2022
aedf79a
Revert "Modify `/type_suggestions/` path response to include inferred…
silentninja Mar 22, 2022
8acbd9a
Revert "Add test case for inferring column display options"
silentninja Mar 22, 2022
8851a30
Fix issues occurred during revert commit 8acbd9a9
silentninja Mar 22, 2022
9989b00
Merge branch 'master' into mathesar-1105-money-display-option
silentninja Mar 22, 2022
3c26968
Add tests for updating money type display options
silentninja Mar 22, 2022
63911d4
Move currency related display options to `currency_details` object in…
silentninja Mar 22, 2022
cbbcce7
List display options for money type in '/databases/' api path
silentninja Mar 23, 2022
e0a2fa6
Fix test cases to accommodate currency symbol extraction in `mathesar…
silentninja Mar 23, 2022
5490b26
Fix DRF error mixin to accept custom exception objects
silentninja Mar 23, 2022
7b79f75
Fix typo in money type casting function name
silentninja Mar 23, 2022
9ed0c61
Fix `mathesar.tests.api.test_ui_types_api.test_database_types_install…
silentninja Mar 23, 2022
f6056ae
Merge branch 'master' into mathesar-1105-money-display-option
silentninja Mar 24, 2022
4ab819e
Merge branch 'master' into mathesar-1105-money-display-option
silentninja Mar 25, 2022
ef09bc8
Merge branch 'master' into mathesar-1105-money-display-option
silentninja Mar 29, 2022
35d4d9d
Merge branch 'master' into mathesar-1105-money-display-option
silentninja Mar 30, 2022
82468c3
Rename db.types.money.get_money_array to db.types.money.get_money_arr…
silentninja Apr 1, 2022
22622d2
Add docstring to mention inference happens on first row of the money …
silentninja Apr 1, 2022
6501f2b
Replace deprecated display options for Money column type with new set…
silentninja Apr 28, 2022
c5a4ef2
Fix lint issues
silentninja Apr 28, 2022
51cb09e
Merge branch 'number-display-options' into mathesar-1105-money-displa…
silentninja Apr 29, 2022
c5e1bab
Fix test cases that broke due to merge
silentninja Apr 29, 2022
1219552
Fix test cases that broke due to merge
silentninja Apr 29, 2022
10bfc11
Merge branch 'number-display-options' into mathesar-1105-money-displa…
silentninja Apr 29, 2022
605456a
Fix lint issues
silentninja Apr 29, 2022
1e70ef4
Merge branch 'master' into mathesar-1105-money-display-option
silentninja Apr 29, 2022
05c7df1
Fix lint issues
silentninja Apr 29, 2022
f0bc314
Merge remote-tracking branch 'origin/mathesar-1105-money-display-opti…
silentninja Apr 29, 2022
37b74b2
Fix `MoneyDisplayOptionSerializer` a subclass of `AbstractNumberDispl…
silentninja Apr 29, 2022
984b635
Merge branch 'number-display-options' into mathesar-1105-money-displa…
silentninja Apr 29, 2022
cb76887
Fix failing test cases due to merge conflivts
silentninja Apr 29, 2022
1d45817
Merge branch 'master' into mathesar-1105-money-display-option
silentninja Apr 29, 2022
2697456
Merge branch 'master' into mathesar-1105-money-display-option
kgodey Apr 30, 2022
b52e2d0
Merge branch 'master' into mathesar-1105-money-display-option
silentninja May 5, 2022
75d9766
Merge branch 'master' into mathesar-1105-money-display-option
silentninja May 5, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move currency related display options to currency_details object in…
… `mathesar.api.serializers.shared_serializers.MoneyDisplayOptionSerializer`
  • Loading branch information
silentninja committed Mar 22, 2022
commit 63911d4c2c86454fe8a330d85e51b8e7fc146c12
28 changes: 17 additions & 11 deletions mathesar/api/serializers/shared_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,26 +116,32 @@ def get_currency_details(currency_code):
return currency_dict[currency_code]


class CurrencyDisplayOptions(MathesarErrorMessageMixin, serializers.Serializer):
symbol = serializers.CharField()
symbol_location = serializers.ChoiceField(choices=[(1, 1), (-1, -1)])
decimal_symbol = serializers.ChoiceField(choices=[(".", "."), (",", ",")])
digit_grouping = serializers.ListField(child=serializers.IntegerField(), allow_empty=True, default=[])
digit_grouping_symbol = serializers.ChoiceField(choices=[(".", "."), (",", ","), (" ", " ")], allow_null=True)


class MoneyDisplayOptionSerializer(MathesarErrorMessageMixin, OverrideRootPartialMixin, serializers.Serializer):
currency_code = serializers.CharField(allow_null=True, required=False)
symbol = serializers.CharField(required=False)
symbol_location = serializers.CharField(required=False, allow_null=True)
decimal_symbol = serializers.CharField(required=False, allow_null=True)
digit_grouping = serializers.ListField(child=serializers.IntegerField(), required=False)
digit_grouping_symbol = serializers.CharField(required=False, allow_null=True)
currency_details = CurrencyDisplayOptions(required=False)

def validate(self, attrs):
currency_code = attrs.get('currency_code', None)
if currency_code is not None:
if len(attrs.keys() - ['currency_code']) != 0:
if attrs.get('currency_details', None) is not None:
raise MoneyDisplayOptionValueConflictAPIException()
else:
currency_details = get_currency_details(currency_code)
attrs['symbol'] = currency_details['currency_symbol']
attrs['symbol_location'] = 'Beginning' if currency_details['p_cs_precedes'] == 1 else "End"
attrs['decimal_symbol'] = currency_details['mon_decimal_point']
attrs['digit_grouping'] = currency_details['mon_grouping']
attrs['digit_grouping_symbol'] = currency_details['mon_thousands_sep']
attrs['currency_details'] = {
'symbol': currency_details['currency_symbol'],
'symbol_location': 1 if currency_details['p_cs_precedes'] == 1 else -1,
'decimal_symbol': currency_details['mon_decimal_point'],
'digit_grouping': currency_details['mon_grouping'],
'digit_grouping_symbol': currency_details['mon_thousands_sep']
}
return super().validate(attrs)


Expand Down
62 changes: 36 additions & 26 deletions mathesar/tests/api/test_column_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def column_test_table_with_service_layer_options(patent_schema):
column_data_list = [{},
{'display_options': {'input': "dropdown", 'use_custom_labels': False}},
{'display_options': {'show_as_percentage': True, 'locale': "en_US"}},
{'display_options': {'symbol': "HK $"}}]
{'display_options': {'currency_details': {'symbol': "HK $"}}}]
db_table = SATable(
"anewtable",
MetaData(bind=engine),
Expand Down Expand Up @@ -259,26 +259,32 @@ def test_column_create_invalid_default(column_test_table, client):
'currency_code': 'en_US',
}, {
'currency_code': 'en_US',
'decimal_symbol': '.',
'digit_grouping': [3, 3, 0],
'digit_grouping_symbol': ',',
'symbol': '$',
'symbol_location': 'Beginning'
'currency_details': {
'decimal_symbol': '.',
'digit_grouping': [3, 3, 0],
'digit_grouping_symbol': ',',
'symbol': '$',
'symbol_location': 1
}
}),
("MONEY", {
'currency_code': None,
'symbol': '$',
'symbol_location': 'End',
'decimal_symbol': '.',
'digit_grouping': [3, 0],
'digit_grouping_symbol': ','
'currency_details': {
'symbol': '$',
'symbol_location': -1,
'decimal_symbol': '.',
'digit_grouping': [3, 0],
'digit_grouping_symbol': ','
}
}, {
'currency_code': None,
'symbol': '$',
'symbol_location': 'End',
'decimal_symbol': '.',
'digit_grouping': [3, 0],
'digit_grouping_symbol': ','
'currency_details': {
'symbol': '$',
'symbol_location': -1,
'decimal_symbol': '.',
'digit_grouping': [3, 0],
'digit_grouping_symbol': ','
}
}),
("NUMERIC", {"show_as_percentage": True}, {"show_as_percentage": True}),
("NUMERIC", {"show_as_percentage": True, "locale": "en_US"}, {"show_as_percentage": True, "locale": "en_US"}),
Expand Down Expand Up @@ -315,11 +321,13 @@ def test_column_create_display_options(
("DATE", {'format': _too_long_string}),
("MONEY", {
'currency_code': 'en_US',
'symbol': '$',
'symbol_location': 'End',
'decimal_symbol': '.',
'digit_grouping': [3, 0],
'digit_grouping_symbol': ','
'currency_details': {
'symbol': '$',
'symbol_location': -1,
'decimal_symbol': '.',
'digit_grouping': [3, 0],
'digit_grouping_symbol': ','
}
}),
("NUMERIC", {"show_as_percentage": "wrong value type"}),
("TIMESTAMP WITH TIME ZONE", {'format': []}),
Expand Down Expand Up @@ -462,11 +470,13 @@ def test_column_update_mathesar_money_display_options(column_test_table_with_ser
display_options = {"currency_code": "en_US.ISO8859-1"}
expected_display_options = {
'currency_code': 'en_US.ISO8859-1',
'decimal_symbol': '.',
'digit_grouping': [3, 3, 0],
'digit_grouping_symbol': ',',
'symbol': '$',
'symbol_location': 'Beginning'
'currency_details': {
'decimal_symbol': '.',
'digit_grouping': [3, 3, 0],
'digit_grouping_symbol': ',',
'symbol': '$',
'symbol_location': 1
}
}
display_options_data = {"display_options": display_options}
response = client.patch(
Expand Down
9 changes: 8 additions & 1 deletion mathesar/utils/display_options_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ def infer_mathesar_money_display_options(table_oid, engine, column_attnum):
if selected_currency_code is not None:
return {'currency_code': currency_code}
else:
return {'decimal_symbol': money_array[2], 'digit_grouping_symbol': money_array[1], 'symbol': money_array[3], 'symbol_location': 'Beginning', 'digit_grouping': []}
# TODO Improve default values based on locale
return {
'decimal_symbol': money_array[2] if money_array[2] is not None else ".",
'digit_grouping_symbol': money_array[1] if money_array[1] is not None else ",",
'symbol': money_array[3],
'symbol_location': 1,
'digit_grouping': []
}


def infer_table_column_display_options(table, col_name_type_dict):
Expand Down