Development of an internal social media platform with personalised dashboards for students
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.

validators.py 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. from django.core.exceptions import ValidationError
  2. from django.core.validators import validate_email
  3. from django.template import Template, TemplateSyntaxError, TemplateDoesNotExist
  4. from django.utils.encoding import force_text
  5. from .compat import text_type
  6. def validate_email_with_name(value):
  7. """
  8. Validate email address.
  9. Both "Recipient Name <email@example.com>" and "email@example.com" are valid.
  10. """
  11. value = force_text(value)
  12. if '<' and '>' in value:
  13. start = value.find('<') + 1
  14. end = value.find('>')
  15. if start < end:
  16. recipient = value[start:end]
  17. else:
  18. recipient = value
  19. validate_email(recipient)
  20. def validate_comma_separated_emails(value):
  21. """
  22. Validate every email address in a comma separated list of emails.
  23. """
  24. if not isinstance(value, (tuple, list)):
  25. raise ValidationError('Email list must be a list/tuple.')
  26. for email in value:
  27. try:
  28. validate_email_with_name(email)
  29. except ValidationError:
  30. raise ValidationError('Invalid email: %s' % email, code='invalid')
  31. def validate_template_syntax(source):
  32. """
  33. Basic Django Template syntax validation. This allows for robuster template
  34. authoring.
  35. """
  36. try:
  37. Template(source)
  38. except (TemplateSyntaxError, TemplateDoesNotExist) as err:
  39. raise ValidationError(text_type(err))