mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
15094 Add missing gettext to error strings for internationalization (#15155)
* 15049 add missing gettext to error strings * 15049 add missing gettext to error strings * 15094 review change * 15094 review change * Formatting cleanup --------- Co-authored-by: Jeremy Stretch <jstretch@netboxlabs.com>
This commit is contained in:
@@ -2,6 +2,7 @@ import re
|
||||
|
||||
from django import forms
|
||||
from django.forms.models import fields_for_model
|
||||
from django.utils.translation import gettext as _
|
||||
|
||||
from utilities.choices import unpack_grouped_choices
|
||||
from utilities.querysets import RestrictedQuerySet
|
||||
@@ -38,7 +39,7 @@ def parse_numeric_range(string, base=10):
|
||||
try:
|
||||
begin, end = int(begin.strip(), base=base), int(end.strip(), base=base) + 1
|
||||
except ValueError:
|
||||
raise forms.ValidationError(f'Range "{dash_range}" is invalid.')
|
||||
raise forms.ValidationError(_('Range "{value}" is invalid.').format(value=dash_range))
|
||||
values.extend(range(begin, end))
|
||||
return sorted(set(values))
|
||||
|
||||
@@ -61,7 +62,7 @@ def parse_alphanumeric_range(string):
|
||||
begin, end = dash_range, dash_range
|
||||
if begin.isdigit() and end.isdigit():
|
||||
if int(begin) >= int(end):
|
||||
raise forms.ValidationError(f'Range "{dash_range}" is invalid.')
|
||||
raise forms.ValidationError(_('Range "{value}" is invalid.').format(value=dash_range))
|
||||
|
||||
for n in list(range(int(begin), int(end) + 1)):
|
||||
values.append(n)
|
||||
@@ -73,10 +74,10 @@ def parse_alphanumeric_range(string):
|
||||
else:
|
||||
# Not a valid range (more than a single character)
|
||||
if not len(begin) == len(end) == 1:
|
||||
raise forms.ValidationError(f'Range "{dash_range}" is invalid.')
|
||||
raise forms.ValidationError(_('Range "{value}" is invalid.').format(value=dash_range))
|
||||
|
||||
if ord(begin) >= ord(end):
|
||||
raise forms.ValidationError(f'Range "{dash_range}" is invalid.')
|
||||
raise forms.ValidationError(_('Range "{value}" is invalid.').format(value=dash_range))
|
||||
|
||||
for n in list(range(ord(begin), ord(end) + 1)):
|
||||
values.append(chr(n))
|
||||
@@ -221,18 +222,24 @@ def parse_csv(reader):
|
||||
if '.' in header:
|
||||
field, to_field = header.split('.', 1)
|
||||
if field in headers:
|
||||
raise forms.ValidationError(f'Duplicate or conflicting column header for "{field}"')
|
||||
raise forms.ValidationError(_('Duplicate or conflicting column header for "{field}"').format(
|
||||
field=field
|
||||
))
|
||||
headers[field] = to_field
|
||||
else:
|
||||
if header in headers:
|
||||
raise forms.ValidationError(f'Duplicate or conflicting column header for "{header}"')
|
||||
raise forms.ValidationError(_('Duplicate or conflicting column header for "{header}"').format(
|
||||
header=header
|
||||
))
|
||||
headers[header] = None
|
||||
|
||||
# Parse CSV rows into a list of dictionaries mapped from the column headers.
|
||||
for i, row in enumerate(reader, start=1):
|
||||
if len(row) != len(headers):
|
||||
raise forms.ValidationError(
|
||||
f"Row {i}: Expected {len(headers)} columns but found {len(row)}"
|
||||
_("Row {i}: Expected {count_expected} columns but found {count_found}").format(
|
||||
count_expected=len(headers), count_found=len(row)
|
||||
)
|
||||
)
|
||||
row = [col.strip() for col in row]
|
||||
record = dict(zip(headers.keys(), row))
|
||||
@@ -253,14 +260,18 @@ def validate_csv(headers, fields, required_fields):
|
||||
is_update = True
|
||||
continue
|
||||
if field not in fields:
|
||||
raise forms.ValidationError(f'Unexpected column header "{field}" found.')
|
||||
raise forms.ValidationError(_('Unexpected column header "{field}" found.').format(field=field))
|
||||
if to_field and not hasattr(fields[field], 'to_field_name'):
|
||||
raise forms.ValidationError(f'Column "{field}" is not a related object; cannot use dots')
|
||||
raise forms.ValidationError(_('Column "{field}" is not a related object; cannot use dots').format(
|
||||
field=field
|
||||
))
|
||||
if to_field and not hasattr(fields[field].queryset.model, to_field):
|
||||
raise forms.ValidationError(f'Invalid related object attribute for column "{field}": {to_field}')
|
||||
raise forms.ValidationError(_('Invalid related object attribute for column "{field}": {to_field}').format(
|
||||
field=field, to_field=to_field
|
||||
))
|
||||
|
||||
# Validate required fields (if not an update)
|
||||
if not is_update:
|
||||
for f in required_fields:
|
||||
if f not in headers:
|
||||
raise forms.ValidationError(f'Required column header "{f}" not found.')
|
||||
raise forms.ValidationError(_('Required column header "{header}" not found.').format(header=f))
|
||||
|
||||
Reference in New Issue
Block a user