Opened 12 years ago

Closed 12 years ago

#18120 closed Uncategorized (wontfix)

add DomainNameField model Field type to validate and store Internet Domain Names

Reported by: michele Owned by: nobody
Component: Database layer (models, ORM) Version: 1.4
Severity: Normal Keywords: domain
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

In django models you can store an URL, but not a domain name. This patch adds a DomainNameField class to address that.

DomainNameField accepts:

  • traditional domain names in ASCII form
  • Internationalized Domain Names in IDNA form (ASCII encoded)
  • Internationalized Domain Names in UTF form

DomainNameField always stores values in ASCII format by encoding and decoding UTF IDNs as needed.

Implementation notes:

  • based on CharField and DomainNameValidator (see #18119)
  • an implicit max_length=255 is added to CharField to propagate the Domain constraint down to the db schema
  • naming: accept_idna might be renamed to "accept_idn" or simply "idn" for Internationalized Domain Name

This passes successfully a buch of tests:

class Zone(models.Model):
    name = models.DomainNameField()

plain_domain = 'foo.com'
idn_domain_good = 'xn--1lq90ic7fzpc.xn--fiqs8s'.decode('idna')   # a IDN domain in UTF format
idn_domain_bad = idn_domain + '#'

Zone(name=plain_domain).full_clean()
# ok!
Zone(name=idn_domain_good).full_clean()
# ok!
Zone(name=idn_domain_bad).full_clean()
# raises ValidationError ok!
Zone(name=idn_domain).save()
# selecting from db yields:
xn--1lq90ic7fzpc.xn--fiqs8s
# ok!

class Zone(models.Model):
    name = models.DomainNameField(accept_idna=False)

Zone(name=idn_domain_good).full_clean()
# raises ValidationError ok!

I'll implement these in a suitable test module if django.db will start one.

Work for this and DomainNameValidator was sponsored by BuddyNS.

Attachments (2)

models_domainnamefield.txt (1.6 KB ) - added by michele 12 years ago.
models_domainnamefield_2.txt (1.6 KB ) - added by michele 12 years ago.
ensure python string is always UTF

Download all attachments as: .zip

Change History (5)

by michele, 12 years ago

Attachment: models_domainnamefield.txt added

comment:1 by michele, 12 years ago

Two notes:

  1. the test mentioned above has idn_domain_good = 'xn--1lq90ic7fzpc.xn--fiqs8s'.encode('idna') instead of decode
  2. please review and comment about to_python() being called on both raw and python data (see comment in patch)

by michele, 12 years ago

ensure python string is always UTF

comment:2 by michele, 12 years ago

Please refer to attachment:models_domainnamefield_2.txt , obsoletes the previous patch.

comment:3 by Claude Paroz, 12 years ago

Resolution: wontfix
Status: newclosed

I think the DomainNameValidator (#18119) is a useful addition to Django. However, a specific DomainName field seems far too specialized to be maintained into Django. It is probably much more adequate for an external package.

Last edited 12 years ago by Claude Paroz (previous) (diff)
Note: See TracTickets for help on using tickets.
Back to Top