Django

Code

Ticket #6092 (assigned)

Opened 9 months ago

Last modified 3 months ago

URL and email fields need to allow custom validators

Reported by: jacob Assigned to: floguy (accepted)
Milestone: Component: Core framework
Version: SVN Keywords:
Cc: joel@joelwatts.com Triage Stage: Accepted
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 1

Description

See #3989 and #6019 -- in both cases, people want to define "email" or "URL" a bit differently than Django does by default. In both cases, changing the default isn't the answer since that'll just make new people unhappy with the new defaults.

So, I should be able to do something like this:

    rfc2822pattern = re.compile(...)

    # ...

    email = models.EmailField(pattern=rfc2822pattern)

That's just a strawman proposal, of course -- I'm not sure I like that syntax.

Attachments

6092.diff (6.2 kB) - added by floguy on 12/01/07 20:32:19.
Initial patch to add pattern keyword argument to several model and form fields.
6092-docs.diff (3.0 kB) - added by floguy on 12/01/07 21:13:55.
Adds documentation to my previous patch.
6092.2.diff (4.8 kB) - added by floguy on 03/17/08 10:33:20.
Updated patch to be more idiomatic Python and to require a compiled regular expression object.
6092-docs.2.diff (2.9 kB) - added by floguy on 03/17/08 10:41:58.
Updated documentation to match the new patch.

Change History

12/01/07 19:36:39 changed by Pierre THIERRY <nowhere.man@levallois.eu.org>

  • needs_better_patch changed.
  • needs_tests changed.
  • needs_docs changed.

Note that parsing an email address with a regex may be considered an abuse of the regex system, and people previous complained that being able to parse all valid email addresses would need an unbearbly complex and hard to debug regex. Email addresses are a full language, and there's a reason they are defined in ABNF. They should be parsed with a real parser, IMHO.

12/01/07 20:31:50 changed by floguy

  • owner changed from nobody to floguy.
  • needs_docs set to 1.
  • has_patch set to 1.
  • status changed from new to assigned.

I have created an initial patch for this, which makes models.EmailField?, models.URLField, (new)forms.EmailField?, (new)forms.URLField all take a pattern keyword argument.

This pattern kwarg can be either a string or a compiled regular expression.

There are also regression tests for the newforms modifications.

12/01/07 20:32:19 changed by floguy

  • attachment 6092.diff added.

Initial patch to add pattern keyword argument to several model and form fields.

12/01/07 21:13:55 changed by floguy

  • attachment 6092-docs.diff added.

Adds documentation to my previous patch.

12/01/07 21:14:05 changed by floguy

  • needs_docs deleted.

01/13/08 07:17:11 changed by Simon Greenhill <dev@simon.net.nz>

  • stage changed from Unreviewed to Ready for checkin.

02/03/08 04:19:54 changed by mtredinnick

  • needs_better_patch set to 1.
  • stage changed from Ready for checkin to Accepted.

There are a few problems with the code patch that need work here.

  1. The get() method on a dictionary returns None by default; no need to specify it.
  2. Don't write == None or != None. Instead, use is and is not to compare with None, since there's only one None object in Python. However, this is moot here anyway, since you might as well just check for "truthiness" -- if pattern: ... -- rather than all the None checks. It's slightly more idiomatic which means people won't be caught wondering why we aren't using the obvious construct.
  3. Let's simplify the internals a bit by saying that people should pass in a reg-exp object here (so the parameter should probably be called something other than "pattern"). Then you can just use duck-typing assumptions and you don't need to check the type or anything. Simply call .match() on the passed in object when we need it. That will remove a bunch of if-blocks. Bear in mind that this is for a bit of a corner case, so it's not going to be a huge burden to ask them to call re.compile once first.

I don't see a huge advantage in allowing both reg-exp objects *and* strings and by choosing the reg-exp approach, we allow the user to pass in anything that has a match() method, since we only use that, which could be something more complex than a reg-exp if they really wanted it.

03/17/08 10:33:20 changed by floguy

  • attachment 6092.2.diff added.

Updated patch to be more idiomatic Python and to require a compiled regular expression object.

03/17/08 10:41:58 changed by floguy

  • attachment 6092-docs.2.diff added.

Updated documentation to match the new patch.

06/01/08 14:19:35 changed by jpwatts

  • cc set to joel@joelwatts.com.

Add/Change #6092 (URL and email fields need to allow custom validators)




Change Properties
Action