You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.py 1.2KB

1234567891011121314151617181920212223242526272829
  1. from django.core.exceptions import ValidationError
  2. from django.utils.functional import SimpleLazyObject
  3. from django.utils.text import format_lazy
  4. def prefix_validation_error(error, prefix, code, params):
  5. """
  6. Prefix a validation error message while maintaining the existing
  7. validation data structure.
  8. """
  9. if error.error_list == [error]:
  10. error_params = error.params or {}
  11. return ValidationError(
  12. # We can't simply concatenate messages since they might require
  13. # their associated parameters to be expressed correctly which
  14. # is not something `format_lazy` does. For example, proxied
  15. # ngettext calls require a count parameter and are converted
  16. # to an empty string if they are missing it.
  17. message=format_lazy(
  18. '{} {}',
  19. SimpleLazyObject(lambda: prefix % params),
  20. SimpleLazyObject(lambda: error.message % error_params),
  21. ),
  22. code=code,
  23. params={**error_params, **params},
  24. )
  25. return ValidationError([
  26. prefix_validation_error(e, prefix, code, params) for e in error.error_list
  27. ])