Advertisement
Guest User

Untitled

a guest
Jul 29th, 2022
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 3.28 KB | None | 0 0
  1. Index: tests/test_email.py
  2. IDEA additional info:
  3. Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
  4. <+>UTF-8
  5. ===================================================================
  6. diff --git a/tests/test_email.py b/tests/test_email.py
  7. --- a/tests/test_email.py   (revision 66726ef806ea2940870251b587a1ae07be9bc1af)
  8. +++ b/tests/test_email.py   (revision afc778de29e4480f8f95b0517b385ed3728b370c)
  9. @@ -20,6 +20,14 @@
  10.      assert email(value, whitelist=whitelist)
  11.  
  12.  
  13. [email protected](('value', 'local_characters_in_user'), [
  14. +    ('emł[email protected]', "ł"),
  15. +    ('łemł[email protected]', "ł"),
  16. +])
  17. +def test_returns_true_on_valid_email_with_local_characters_in_user(value, local_characters_in_user):
  18. +    assert email(value, local_characters_in_user=local_characters_in_user)
  19. +
  20. +
  21.  @pytest.mark.parametrize(('value',), [
  22.      (None,),
  23.      ('',),
  24. Index: validators/email.py
  25. IDEA additional info:
  26. Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
  27. <+>UTF-8
  28. ===================================================================
  29. diff --git a/validators/email.py b/validators/email.py
  30. --- a/validators/email.py   (revision 66726ef806ea2940870251b587a1ae07be9bc1af)
  31. +++ b/validators/email.py   (revision afc778de29e4480f8f95b0517b385ed3728b370c)
  32. @@ -1,29 +1,34 @@
  33.  import re
  34.  
  35.  from .utils import validator
  36. +from functools import cache
  37.  
  38. -user_regex = re.compile(
  39. -    # dot-atom
  40. -    r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+"
  41. -    r"(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*$"
  42. -    # quoted-string
  43. -    r'|^"([\001-\010\013\014\016-\037!#-\[\]-77]|'
  44. -    r"""\\[\001-\011\013\014\016-77])*"$)""",
  45. -    re.IGNORECASE
  46. -)
  47. -domain_regex = re.compile(
  48. -    # domain
  49. -    r'(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+'
  50. -    r'(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?$)'
  51. -    # literal form, ipv4 address (SMTP 4.1.3)
  52. -    r'|^\[(25[0-5]|2[0-4]\d|[0-1]?\d?\d)'
  53. -    r'(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}\]$',
  54. -    re.IGNORECASE)
  55. +
  56. +@cache
  57. +def regex(local_characters_in_user=""):
  58. +    return re.compile(
  59. +        # dot-atom
  60. +        r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z" + local_characters_in_user + "]+"
  61. +        r"(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z" + local_characters_in_user + "]+)*$"
  62. +        # quoted-string
  63. +        r'|^"([\001-\010\013\014\016-\037!#-\[\]-77]|'
  64. +        r"""\\[\001-\011\013\014\016-77])*"$)""",
  65. +        re.IGNORECASE
  66. +    ), re.compile(
  67. +        # domain
  68. +        r'(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+'
  69. +        r'(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?$)'
  70. +        # literal form, ipv4 address (SMTP 4.1.3)
  71. +        r'|^\[(25[0-5]|2[0-4]\d|[0-1]?\d?\d)'
  72. +        r'(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}\]$',
  73. +        re.IGNORECASE)
  74. +
  75. +
  76.  domain_whitelist = ['localhost']
  77.  
  78.  
  79.  @validator
  80. -def email(value, whitelist=None):
  81. +def email(value, whitelist=None, local_characters_in_user=""):
  82.      """
  83.      Validate an email address.
  84.  
  85. @@ -46,10 +51,12 @@
  86.  
  87.      :param value: value to validate
  88.      :param whitelist: domain names to whitelist
  89. +    :param local_characters_in_user: string with extra accepted characters in user part
  90.  
  91.      :copyright: (c) Django Software Foundation and individual contributors.
  92.      :license: BSD
  93.      """
  94. +    user_regex, domain_regex = regex(local_characters_in_user)
  95.  
  96.      if whitelist is None:
  97.          whitelist = domain_whitelist
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement