Global method? Method without a home... where to put it?

I have made an additon to my settings which looks like this:

# multitext configuration
MULTITEXT_LANGUAGES = {
    'ENGLISH': 'en-us',
    'GERMAN': 'de-de', }
MULTITEXT_CHOICES = [
    # (*Language*, _('*Display Name*')),
    (MULTITEXT_LANGUAGES['ENGLISH'], 'English'),
    (MULTITEXT_LANGUAGES['GERMAN'], 'German'),
]

There are several forms that build on this information that live in different apps. I have a method that builds form fields based on this setting. I want to be as DRY as possible. Here is the method:

def get_fields_from_language_settings():
    base_fields = {}
    fields = {}

    for i in range(len(settings.MULTITEXT_LANGUAGES)):
        field_name = 'language%s' % (i,)
        base_fields[field_name] = f.CharField(disabled=True, initial=settings.MULTITEXT_CHOICES[i][0])
        fields[field_name] = f.CharField(disabled=True, initial=settings.MULTITEXT_CHOICES[i][0])
        field_name += '_text'
        base_fields[field_name] = f.CharField()
        fields[field_name] = f.CharField()

    return [base_fields, fields]

I usually have an app called infrastructure. That’s where I put all the things that I need that don’t really belong anywhere else. So for instance, I will put my base template into this app. I also have some view helper functions that I put here. So I put my get_fields_from_language_settings()-method into a file called utility.py. Do you also have methods like this in your projects? Where do you put them?

I normally have an app called core. Since this is a form field related function, I’d put it in core.forms. Using names like ‘helpers’ or ‘utils’ isn’t great because the names convey zero information.

1 Like