Ticket #9764: idn_support.diff
File idn_support.diff, 3.6 KB (added by , 16 years ago) |
---|
-
django/forms/fields.py
432 432 RegexField.__init__(self, email_re, max_length, min_length, *args, 433 433 **kwargs) 434 434 435 def clean(self, value): 436 try: 437 value = super(EmailField, self).clean(value) 438 except ValidationError: 439 # Trivial case failed. Try for possible IDN domain-part 440 if value and u'@' in value: 441 parts = value.split(u'@') 442 domain_part = parts[-1] 443 try: 444 parts[-1] = parts[-1].encode('idna') 445 except UnicodeError: 446 pass # invalid domain part 447 # After validation revert ACE encoded domain-part to 448 # original (IDN) value as suggested by RFC 3490 449 value = super(EmailField, self).clean(u'@'.join(parts) 450 ).replace(parts[-1], domain_part) 451 else: 452 raise 453 return value 454 435 455 try: 436 456 from django.conf import settings 437 457 URL_VALIDATOR_USER_AGENT = settings.URL_VALIDATOR_USER_AGENT … … 547 567 self.user_agent = validator_user_agent 548 568 549 569 def clean(self, value): 570 if value: 571 value = smart_unicode(value) 550 572 # If no URL scheme given, assume http:// 551 573 if value and '://' not in value: 552 574 value = u'http://%s' % value 553 # If no URL path given, assume / 554 if value and not urlparse.urlsplit(value)[2]: 555 value += '/' 575 if value: 576 splitted = urlparse.urlsplit(value) 577 # IDN -> ACE 578 try: 579 netloc_ace = splitted[1].encode('idna') 580 except UnicodeError: # invalid domain part 581 netloc_ace = splitted[1] 582 value = value.replace(splitted[1], netloc_ace) 583 # If no URL path given, assume / 584 if not splitted[2]: 585 value += u'/' 556 586 value = super(URLField, self).clean(value) 557 587 if value == u'': 558 588 return value 589 # ACE -> IDN 590 value = value.replace(netloc_ace, splitted[1]) 559 591 if self.verify_exists: 560 592 import urllib2 561 593 headers = { -
tests/regressiontests/forms/fields.py
733 733 ValidationError: [u'This field is required.'] 734 734 >>> f.clean('person@example.com') 735 735 u'person@example.com' 736 737 Address with IDN domain-part: 738 >>> f.clean('local@domain.with.idn.xyzäöüßabc.part.com') 739 u'local@domain.with.idn.xyz\xe4\xf6\xfc\xdfabc.part.com' 740 736 741 >>> f.clean('foo') 737 742 Traceback (most recent call last): 738 743 ... … … 765 770 Traceback (most recent call last): 766 771 ... 767 772 ValidationError: [u'Enter a valid e-mail address.'] 773 >>> f.clean('foo@.com') 774 Traceback (most recent call last): 775 ... 776 ValidationError: [u'Enter a valid e-mail address.'] 768 777 769 778 EmailField also access min_length and max_length parameters, for convenience. 770 779 >>> f = EmailField(min_length=10, max_length=15) … … 868 877 u'http://200.8.9.10/' 869 878 >>> f.clean('http://200.8.9.10:8000/test') 870 879 u'http://200.8.9.10:8000/test' 880 881 IDN domain 882 >>> f.clean('http://some.idn.xyzäöüßabc.domain.com:123/blah') 883 u'http://some.idn.xyz\xe4\xf6\xfc\xdfabc.domain.com:123/blah' 884 871 885 >>> f.clean('foo') 872 886 Traceback (most recent call last): 873 887 ...