From 7d841e155ef13ee32348cb051636147e8095ea2e Mon Sep 17 00:00:00 2001 From: vic012 Date: Sun, 14 Jul 2024 17:14:34 -0300 Subject: [PATCH 1/4] Added utils get_states_of_brazil --- localflavor/br/utils.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 localflavor/br/utils.py diff --git a/localflavor/br/utils.py b/localflavor/br/utils.py new file mode 100644 index 00000000..a074ba42 --- /dev/null +++ b/localflavor/br/utils.py @@ -0,0 +1,31 @@ +from .br_states import STATE_CHOICES + + +def get_states_of_brazil(federative_unit=None, capital_letter=False): + """ + Return a state of Brazil or available options + + Parametes: + federative_unit (Any, optional): The Federative Unit. If not provided, defaults to None. + capital_letter (bool, optional): A boolean flag to return the state with capital letter. Defaults to False + + returns: + Union[str, dict]: + - If federative_unit not is None and his value is valid, returns a string + - If federative_unit is None, returns a dictionary + - If capital_letter is True, returns all values with capital letters + """ + + state_choices_available = { + acronym: state.upper() if capital_letter else state for acronym, state in STATE_CHOICES + } + + if federative_unit is None: + return state_choices_available + + federative_unit = federative_unit.upper() + + if federative_unit in state_choices_available: + return state_choices_available[federative_unit] + + return state_choices_available From 73ece294b7db75e6a8014301012f15319b8d335f Mon Sep 17 00:00:00 2001 From: vic012 Date: Fri, 19 Jul 2024 10:24:31 -0300 Subject: [PATCH 2/4] improvements: improvement utils file and added test file --- localflavor/br/utils.py | 2 +- tests/test_br/test_br.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/localflavor/br/utils.py b/localflavor/br/utils.py index a074ba42..b82a4d10 100644 --- a/localflavor/br/utils.py +++ b/localflavor/br/utils.py @@ -23,7 +23,7 @@ def get_states_of_brazil(federative_unit=None, capital_letter=False): if federative_unit is None: return state_choices_available - federative_unit = federative_unit.upper() + federative_unit = federative_unit.upper() if isinstance(federative_unit, str) else "" if federative_unit in state_choices_available: return state_choices_available[federative_unit] diff --git a/tests/test_br/test_br.py b/tests/test_br/test_br.py index a1a3726a..a4da13a6 100644 --- a/tests/test_br/test_br.py +++ b/tests/test_br/test_br.py @@ -3,6 +3,7 @@ from localflavor.br import models from localflavor.br.forms import (BRCNPJField, BRCPFField, BRProcessoField, BRStateChoiceField, BRStateSelect, BRZipCodeField) +from localflavor.br.utils import get_states_of_brazil from tests.test_br.forms import BRPersonProfileForm @@ -250,3 +251,35 @@ def test_BRPostalCodeField(self): self.assertEqual(instance.max_length, new_instance.max_length) self.assertEqual(instance.description, new_instance.description) self.assertEqual(instance.validators, new_instance.validators) + + +class GetStatesOfBrazil(SimpleTestCase): + + ALL_EXPECTED_STATES = dict + + def test_get_valid_state(self): + federative_unit = "pb" + state_of_brazil = get_states_of_brazil(federative_unit=federative_unit) + expected_value = "ParaĆ­ba" + + self.assertEqual(state_of_brazil, expected_value) + + def test_get_return_all_states_dict(self): + states_of_brazil_capital_letter = get_states_of_brazil(capital_letter=True) + states_of_brazil_without_capital_letter = get_states_of_brazil() + + self.assertEqual( + type(states_of_brazil_capital_letter), + self.ALL_EXPECTED_STATES + ) + self.assertEqual( + type(states_of_brazil_without_capital_letter), + self.ALL_EXPECTED_STATES + ) + + def test_federative_unit_invalid(self): + invalid_inputs = [1, 1.0, "None", [None]] + + for invalid_input in invalid_inputs: + result = get_states_of_brazil(invalid_input) + self.assertIsInstance(result, self.ALL_EXPECTED_STATES) From 88c39304708a582c726ad8ff1634a99be036f368 Mon Sep 17 00:00:00 2001 From: vic012 Date: Mon, 29 Jul 2024 09:21:58 -0300 Subject: [PATCH 3/4] Change files: docs/changelog.rst and docs/authors.rst --- docs/authors.rst | 1 + docs/changelog.rst | 2 ++ 2 files changed, 3 insertions(+) diff --git a/docs/authors.rst b/docs/authors.rst index 4e41ef55..9feb54ab 100644 --- a/docs/authors.rst +++ b/docs/authors.rst @@ -104,6 +104,7 @@ Authors * Paul Cunnane * Paul Donohue * Paulo Poiati +* Pedro Henrique Vicente de Sousa * Peter J. Farrell * Rael Max * Ramiro Morales diff --git a/docs/changelog.rst b/docs/changelog.rst index 642acdd8..a6eb8a0e 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -11,6 +11,8 @@ New flavors: New fields for existing flavors: - Added CIN Number field in Morocco flavor (`gh-705 `_). +- Added get_states_of_brazil to br.utils to return a state or all available options with some settings + () Modifications to existing flavors: From 9aab886843be0017ac0b290eb10c1e6a767f83fd Mon Sep 17 00:00:00 2001 From: Pedro Henrique Date: Mon, 29 Jul 2024 09:30:24 -0300 Subject: [PATCH 4/4] Update changelog.rst --- docs/changelog.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index a6eb8a0e..91d88b2e 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -12,7 +12,7 @@ New fields for existing flavors: - Added CIN Number field in Morocco flavor (`gh-705 `_). - Added get_states_of_brazil to br.utils to return a state or all available options with some settings - () + (`gh-510 `_). Modifications to existing flavors: