Code review comment for lp:~daker/loco-team-portal/ld-languages-menu

Revision history for this message
Michael Hall (mhall119) wrote :

Put the 'languages' dict in utils.py outside of any function def, set it to None by default, then a method to return it, populating it if it's not already there, then call that method from the context processor:

loco_directory/common/utils.py

languages = None
def get_languages():
    """
    Return a language list.
    """
    if languages is None:
        languages = {}
        Languages = Language.objects.order_by('name')
        for language in Languages:
           if check_for_language(language.code):
                languages[language.code] = language.name
        languages = sorted([(value,key) for (key,value) in languages.items()])
    return languages

loco_directory/common/context_processors.py

import common.utils
def language_menu(request):
    return {'languages': common.utils.get_languages()}

« Back to merge proposal