1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Closes #13149: Wrap form field labels with gettext_lazy()

Co-authored-by: Jeremy Stretch <jstretch@netboxlabs.com>
This commit is contained in:
Arthur Hanson
2023-07-31 23:52:38 +07:00
committed by GitHub
parent 83bebc1bd2
commit b7a9649269
51 changed files with 1381 additions and 601 deletions

View File

@@ -1,5 +1,6 @@
from django import forms
from django.contrib.postgres.forms import SimpleArrayField
from django.utils.translation import gettext_lazy as _
from ..utils import parse_numeric_range
@@ -12,8 +13,9 @@ class NumericArrayField(SimpleArrayField):
def clean(self, value):
if value and not self.to_python(value):
raise forms.ValidationError(f'Invalid list ({value}). '
f'Must be numeric and ranges must be in ascending order')
raise forms.ValidationError(
_("Invalid list ({value}). Must be numeric and ranges must be in ascending order.").format(value=value)
)
return super().clean(value)
def to_python(self, value):

View File

@@ -1,4 +1,5 @@
from django import forms
from django.utils.translation import gettext_lazy as _
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist
from django.db.models import Q
@@ -40,7 +41,7 @@ class CSVMultipleChoiceField(CSVChoicesMixin, forms.MultipleChoiceField):
if not value:
return []
if not isinstance(value, str):
raise forms.ValidationError(f"Invalid value for a multiple choice field: {value}")
raise forms.ValidationError(_("Invalid value for a multiple choice field: {value}").format(value=value))
return value.split(',')
@@ -53,7 +54,7 @@ class CSVModelChoiceField(forms.ModelChoiceField):
Extends Django's `ModelChoiceField` to provide additional validation for CSV values.
"""
default_error_messages = {
'invalid_choice': 'Object not found: %(value)s',
'invalid_choice': _('Object not found: %(value)s'),
}
def to_python(self, value):
@@ -61,7 +62,7 @@ class CSVModelChoiceField(forms.ModelChoiceField):
return super().to_python(value)
except MultipleObjectsReturned:
raise forms.ValidationError(
f'"{value}" is not a unique value for this field; multiple objects were found'
_('"{value}" is not a unique value for this field; multiple objects were found').format(value=value)
)
@@ -70,7 +71,7 @@ class CSVModelMultipleChoiceField(forms.ModelMultipleChoiceField):
Extends Django's `ModelMultipleChoiceField` to support comma-separated values.
"""
default_error_messages = {
'invalid_choice': 'Object not found: %(value)s',
'invalid_choice': _('Object not found: %(value)s'),
}
def clean(self, value):
@@ -93,11 +94,11 @@ class CSVContentTypeField(CSVModelChoiceField):
try:
app_label, model = value.split('.')
except ValueError:
raise forms.ValidationError(f'Object type must be specified as "<app>.<model>"')
raise forms.ValidationError(_('Object type must be specified as "<app>.<model>"'))
try:
return self.queryset.get(app_label=app_label, model=model)
except ObjectDoesNotExist:
raise forms.ValidationError(f'Invalid object type')
raise forms.ValidationError(_('Invalid object type'))
class CSVMultipleContentTypeField(forms.ModelMultipleChoiceField):

View File

@@ -1,7 +1,7 @@
import re
from django import forms
from django.utils.translation import gettext as _
from django.utils.translation import gettext_lazy as _
from utilities.forms.constants import *
from utilities.forms.utils import expand_alphanumeric_pattern, expand_ipaddress_pattern
@@ -21,10 +21,10 @@ class ExpandableNameField(forms.CharField):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if not self.help_text:
self.help_text = """
Alphanumeric ranges are supported for bulk creation. Mixed cases and types within a single range
are not supported (example: <code>[ge,xe]-0/0/[0-9]</code>).
"""
self.help_text = _(
"Alphanumeric ranges are supported for bulk creation. Mixed cases and types within a single range are "
"not supported (example: <code>[ge,xe]-0/0/[0-9]</code>)."
)
def to_python(self, value):
if not value:

View File

@@ -4,7 +4,7 @@ from django import forms
from django.db.models import Count
from django.forms.fields import JSONField as _JSONField, InvalidJSONInput
from django.templatetags.static import static
from django.utils.translation import gettext as _
from django.utils.translation import gettext_lazy as _
from netaddr import AddrFormatError, EUI
from utilities.forms import widgets
@@ -26,14 +26,14 @@ class CommentField(forms.CharField):
A textarea with support for Markdown rendering. Exists mostly just to add a standard `help_text`.
"""
widget = widgets.MarkdownWidget
help_text = f"""
<i class="mdi mdi-information-outline"></i>
<a href="{static('docs/reference/markdown/')}" target="_blank" tabindex="-1">
Markdown</a> syntax is supported
"""
label = _('Comments')
help_text = _(
'<i class="mdi mdi-information-outline"></i> '
'<a href="{url}" target="_blank" tabindex="-1">Markdown</a> syntax is supported'
).format(url=static('docs/reference/markdown/'))
def __init__(self, *, help_text=help_text, required=False, **kwargs):
super().__init__(help_text=help_text, required=required, **kwargs)
def __init__(self, *, label=label, help_text=help_text, required=False, **kwargs):
super().__init__(label=label, help_text=help_text, required=required, **kwargs)
class SlugField(forms.SlugField):
@@ -44,10 +44,11 @@ class SlugField(forms.SlugField):
slug_source: Name of the form field from which the slug value will be derived
"""
widget = widgets.SlugWidget
label = _('Slug')
help_text = _("URL-friendly unique shorthand")
def __init__(self, *, slug_source='name', help_text=help_text, **kwargs):
super().__init__(help_text=help_text, **kwargs)
def __init__(self, *, slug_source='name', label=label, help_text=help_text, **kwargs):
super().__init__(label=label, help_text=help_text, **kwargs)
self.widget.attrs['slug-source'] = slug_source
@@ -77,7 +78,7 @@ class TagFilterField(forms.MultipleChoiceField):
]
# Choices are fetched each time the form is initialized
super().__init__(label='Tags', choices=get_choices, required=False, *args, **kwargs)
super().__init__(label=_('Tags'), choices=get_choices, required=False, *args, **kwargs)
class LaxURLField(forms.URLField):
@@ -113,7 +114,7 @@ class MACAddressField(forms.Field):
"""
widget = forms.CharField
default_error_messages = {
'invalid': 'MAC address must be in EUI-48 format',
'invalid': _('MAC address must be in EUI-48 format'),
}
def to_python(self, value):