#17320 closed Cleanup/optimization (fixed)
Sites.domain should not contain whitespace
Reported by: | Jason Novinger | Owned by: | Horst Gutmann |
---|---|---|---|
Component: | contrib.sites | Version: | 1.3 |
Severity: | Normal | Keywords: | sprint2013 |
Cc: | Flavio Curella, krzysiumed@… | Triage Stage: | Ready for checkin |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Having space characters in the domain field of the Site model causes problems when using an instance to generate links (e.g. Share on Facebook).
Attachments (2)
Change History (15)
by , 13 years ago
Attachment: | remove_whitespace_from_domain.diff added |
---|
comment:1 by , 13 years ago
Cc: | added |
---|
comment:2 by , 13 years ago
Patch needs improvement: | set |
---|---|
Triage Stage: | Unreviewed → Accepted |
Type: | Bug → Cleanup/optimization |
by , 13 years ago
Attachment: | 17320.diff added |
---|
comment:3 by , 13 years ago
Owner: | changed from | to
---|---|
Patch needs improvement: | unset |
Status: | new → assigned |
I'd implemented clean
method instead of clean_fields
- it's much more simpler (you've not to call parent).
Another issue is that my implementation check if domain doesn't contain spaces, but it doesn't check other whitespaces (for example '\r
'). Is it necessary?
If it is, we can do it in a few ways:
if self.domain != (self.domain.translate(None, string.whitespace)):
or
if all(map(lambda whitespace: whitespace not in self.domain, string.whitespace)):
or
if all(map(lambda whitespace: self.domain.find(whitespace) == -1, string.whitespaces)):
or
if re.search('\s', self.domain):
The first three are ugly. The last one is the best, but it's slow (the pattern '\s' must be compiled each time - the solution is to cache compiled pattern).
comment:4 by , 13 years ago
Cc: | added |
---|
comment:5 by , 12 years ago
Keywords: | sprint2013 added |
---|---|
Owner: | changed from | to
Patch needs improvement: | set |
Personally, I believe that validating regarding spaces alone should be sufficient (unless we want to go for an all-out domain-name validation) but the current patch marks the whole model as invalid and not just the domain field. Also, since this is a user-facing validation message, it should be localized.
So the question raised above remains: Do we only need space-validation or should we go for an all-out domain name validation for this field?
comment:6 by , 12 years ago
Doing domain-name validation 'the right way' leads to a lot of added complexity in the code and, what's worse: the risk of false negatives. People not being able to add a reference to their own site is *not* what we want.
As for the validation: the error can and should happen at the field level by adding the validator to validators
for the database field. For this an instance of Django's RegexValidator can be used.
comment:7 by , 12 years ago
Patch needs improvement: | unset |
---|
Added a pull request: https://github.com/django/django/pull/828
comment:8 by , 12 years ago
I wonder if the error message should be geared more towards non-technical users and (to me) 'whitespace' feels like a technical term. What do you think about this for an error message?: "The domain name cannot contain any spaces or tabs."
comment:10 by , 12 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
comment:11 by , 12 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
I agree that spaces in domain names are invalid.
However, Django avoids "magic auto-fix", because it quickly becomes auto-break for some expectations.
If would be better to implement a
clean_fields
method in theSite
model.