Code

Opened 2 years ago

Closed 2 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 2 years ago.
models_domainnamefield_2.txt (1.6 KB) - added by michele 2 years ago.
ensure python string is always UTF

Download all attachments as: .zip

Change History (5)

Changed 2 years ago by michele

comment:1 Changed 2 years ago by michele

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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)

Changed 2 years ago by michele

ensure python string is always UTF

comment:2 Changed 2 years ago by michele

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

comment:3 Changed 2 years ago by claudep

  • Resolution set to wontfix
  • Status changed from new to closed

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 2 years ago by claudep (previous) (diff)

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.