-
Notifications
You must be signed in to change notification settings - Fork 302
Added a utils.py get_state_name_from_state_abbreviation #510
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
base: master
Are you sure you want to change the base?
Conversation
leogregianin
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Useful for many applications
|
@vic012 Can you provide some information about how this fits into django-localflavor? Reading through |
|
Thanks for the feedback, @benkonrath ! You're right — Why it fits into django-localflavor:
Example use case:Let’s say a user stores from localflavor.br.utils import get_states_of_brazil
print(get_states_of_brazil("RJ")) # Rio de Janeiro
# or
print(get_states_of_brazil("rj")) # Rio de Janeiro
print(get_states_of_brazil("RJ", capital_letter=True)) # RIO DE JANEIRO
# or
print(get_states_of_brazil("rj", capital_letter=True)) # RIO DE JANEIRO
print(get_states_of_brazil("Rt", capital_letter=True)) # {'AC': 'ACRE', 'AL': 'ALAGOAS', 'AP': 'AMAPÁ', ...}
# or
print(get_states_of_brazil("Rt")) # {'AC': 'Acre', 'AL': 'Alagoas', 'AP': 'Amapá', ...}
print(get_states_of_brazil(capital_letter=True)) # {'AC': 'ACRE', 'AL': 'ALAGOAS', 'AP': 'AMAPÁ', ...}
# or
print(get_states_of_brazil()) # {'AC': 'Acre', 'AL': 'Alagoas', 'AP': 'Amapá', ...}# models.py
from django.db import models
from localflavor.br.br_states import STATE_CHOICES
class UserProfile(models.Model):
name = models.CharField(max_length=100)
state = models.CharField(max_length=2, choices=STATE_CHOICES)
# views.py
from django.shortcuts import render
from localflavor.br.utils import get_states_of_brazil
from .models import UserProfile
def profile_view(request, user_id):
profile = UserProfile.objects.get(id=user_id)
state_name = get_states_of_brazil(profile.state)
return render(request, 'profile.html', {'profile': profile, 'state_name': state_name}){# profile.html #}
<p>User: {{ profile.name }}</p>
<p>State: {{ state_name }}</p>Using raw STATE_CHOICES directly in a large codebase:In large projects with multiple developers, it's common to see repetitive logic written in different parts of the codebase to convert Brazilian state abbreviations into full names using the STATE_CHOICES variable. Here's an example of how it's typically done without a helper: from localflavor.br.br_states import STATE_CHOICES
def get_state(federative_unit=None, capital_letter=False):
state_choices_available = {
acronym.upper(): name.upper() if capital_letter else name
for acronym, name in STATE_CHOICES
}
if federative_unit is None:
return state_choices_available
federative_unit = str(federative_unit).upper()
return state_choices_available.get(federative_unit, state_choices_available)
# or
def get_state_name(federative_unit=None, capital_letter=False):
state_dict = {
acronym.upper(): name.upper() if capital_letter else name
for acronym, name in STATE_CHOICES
}
return state_dict.get(str(federative_unit).upper(), state_dict)Although this works, it tends to be copied and adapted differently across the project — sometimes with minor variations in casing, error handling, or naming conventions. This increases the risk of inconsistencies and makes the code harder to read, debug, and maintain over time. By centralizing this logic in a utility function like get_states_of_brazil, we:
In short, a shared utility improves readability, maintainability, and developer experience in growing codebases. |
|
I understand that this is a basic utility and not everyone may find it necessary. However, I've personally needed a similar function in the past, and I believe this addition could be helpful to others as well. Just as localflavor has supported many applications around the world with simple but essential tools, this small contribution might prove useful in similar scenarios. |
|
I am not sure if this function is really useful for a broader user base or something that is useful just for a few use cases. I also see some problems with this function that could be improved anyway:
|
|
Thank you for the detailed feedback @rennerocha ! I agreed with your points regarding naming, expected return types, and keeping the function focused on a single use case. Based on your suggestions, I’ve updated the PR with the following changes:
I believe this version is clearer, more predictable. |
Added new utility functions for Brazilian state names and options.
Removed duplicate entry for CIN Number field in Morocco flavor and consolidated related information.
Description:
This PR adds a new helper function to the localflavor.br module, encapsulated in a utils.py file. The function,
get_state_name_from_state_abbreviation, allows developers to convert a Brazilian state abbreviation (UF) into its full state name with a simple, consistent API.Key Changes:
Introduces
STATE_CHOICES_DICT, a constant dictionary mapping all Brazilian UF abbreviations to their full names (derived fromSTATE_CHOICES).Adds
get_state_name_from_state_abbreviation:Accepts a string abbrev (e.g., "RJ" or "sp").
Normalizes input to upper case.
Returns the full state name (e.g., "Rio de Janeiro"), or None if the abbreviation is invalid or not a string.
Includes unit tests covering:
Valid abbreviations (both upper and lower case),
Invalid abbreviations,
Non-string inputs.
Updates
docs/changelog.rstwith a description of the new functionality.Adds the contributor’s name to
docs/authors.rst.