utils: add a lazy_label helper (#30252)
It allows giving a default value for a label with a getter function to allow customization.
This commit is contained in:
parent
4599df562a
commit
f44735179f
|
@ -3,7 +3,6 @@ import random
|
|||
import time
|
||||
import logging
|
||||
import urllib
|
||||
import six
|
||||
import urlparse
|
||||
import uuid
|
||||
import datetime
|
||||
|
@ -24,7 +23,7 @@ from django.contrib.auth import (REDIRECT_FIELD_NAME, login as auth_login, SESSI
|
|||
from django import forms
|
||||
from django.forms.utils import ErrorList, to_current_timezone
|
||||
from django.utils import timezone
|
||||
from django.utils import html, http
|
||||
from django.utils import html, http, six, encoding
|
||||
from django.utils.translation import ugettext as _, ungettext
|
||||
from django.shortcuts import resolve_url
|
||||
from django.template.loader import render_to_string, TemplateDoesNotExist
|
||||
|
@ -33,7 +32,7 @@ from django.core import signing
|
|||
from django.core.urlresolvers import reverse, NoReverseMatch
|
||||
from django.utils.formats import localize
|
||||
from django.contrib import messages
|
||||
from django.utils.functional import empty
|
||||
from django.utils.functional import empty, allow_lazy
|
||||
from django.utils.http import urlsafe_base64_encode
|
||||
from django.utils.encoding import iri_to_uri, force_bytes, uri_to_iri
|
||||
from django.shortcuts import render
|
||||
|
@ -1085,3 +1084,12 @@ def user_can_change_password(user, request=None):
|
|||
if can is False:
|
||||
return can
|
||||
return True
|
||||
|
||||
|
||||
def lazy_label(default, func):
|
||||
'''Allow using a getter for a label, with late binding.
|
||||
|
||||
ex.: lazy_label(_('Default label'), lambda: app_settings.CUSTOM_LABEL)
|
||||
'''
|
||||
return encoding.force_text(func() or default)
|
||||
lazy_label = allow_lazy(lazy_label, six.text_type)
|
||||
|
|
Loading…
Reference in New Issue