Update po files
[mailman-postorious.git] / src / postorius / forms / validators.py
bloba06d8703e903501656bb21c11653b7bbebd8c707
1 import uuid
3 from django.core.exceptions import ValidationError
4 from django.core.validators import validate_email
5 from django.utils.translation import gettext_lazy as _
8 def validate_uuid_or_email(input_value):
9 """Check if the value is a valid email or UUID.
11 This is useful to determine if a given input is a user identifier in
12 Mailman 3 API 3.1+, where, user identifiers are either UUID or email.
14 :param input_value: The value to validate.
15 :type input_value: str
16 :returns: input_value
17 :rtype: str
18 :raises ValidationError: If the value is either a UUID, not an email.
19 """
21 try:
22 validate_email(input_value)
23 except ValidationError:
24 is_email = False
25 else:
26 is_email = True
28 is_uuid = False
29 if not is_email:
30 try:
31 uuid.UUID(input_value)
32 except ValueError:
33 is_uuid = False
34 else:
35 is_uuid = True
37 if not any([is_email, is_uuid]):
38 raise ValidationError(
39 _('Invalid: "{0}" should be either email or UUID').format(
40 input_value),
41 params={'value': input_value},
42 code='invalid')
43 return input_value