13149 add gettext_lazy to forms

This commit is contained in:
Arthur 2023-07-13 15:05:20 +07:00 committed by Jeremy Stretch
parent 887dfa0234
commit 230d80a31d
3 changed files with 13 additions and 11 deletions

View File

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

View File

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

View File

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