Sanitize a value according to the current decimal and thousand separator setting. Used with form field input.
(value)
| 274 | |
| 275 | |
| 276 | def sanitize_separators(value): |
| 277 | """ |
| 278 | Sanitize a value according to the current decimal and |
| 279 | thousand separator setting. Used with form field input. |
| 280 | """ |
| 281 | if isinstance(value, str): |
| 282 | parts = [] |
| 283 | decimal_separator = get_format("DECIMAL_SEPARATOR") |
| 284 | if decimal_separator in value: |
| 285 | value, decimals = value.split(decimal_separator, 1) |
| 286 | parts.append(decimals) |
| 287 | if settings.USE_THOUSAND_SEPARATOR: |
| 288 | thousand_sep = get_format("THOUSAND_SEPARATOR") |
| 289 | if ( |
| 290 | thousand_sep == "." |
| 291 | and value.count(".") == 1 |
| 292 | and len(value.split(".")[-1]) != 3 |
| 293 | ): |
| 294 | # Special case where we suspect a dot meant decimal separator |
| 295 | # (see #22171). |
| 296 | pass |
| 297 | else: |
| 298 | for replacement in { |
| 299 | thousand_sep, |
| 300 | unicodedata.normalize("NFKD", thousand_sep), |
| 301 | }: |
| 302 | value = value.replace(replacement, "") |
| 303 | parts.append(value) |
| 304 | value = ".".join(reversed(parts)) |
| 305 | return value |