#24774: Django sites framework, domain field unique
-------------------------------------+-------------------------------------
Reporter: arieve | Owner: nobody
Type: Bug | Status: new
Component: contrib.sites | Version: 1.8
Severity: Normal | Resolution:
Keywords: sites framework, | Triage Stage:
domain field, unique | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by arieve):
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
Old description:
> In the django.contrib.sites.models the domain field of the Site model is
> defined like this:
> {{{#!python
> domain = models.CharField(_('domain name'), max_length=100,
> validators=[_simple_domain_name_validator])
> }}}
>
> I'd like to stress that the field doesn't have the requirement to be
> unique. Neither by having unique = True or by the
> _simple_domain_name_validator, which only checks for whitespaces and
> typos.
>
> In the case that a user (or two different users) would add the same
> domain (ie "example.com") then the following example from the
> documentation would go wrong or at least give a completely wrong result:
>
> {{{#!python
> from django.contrib.sites.shortcuts import get_current_site
>
> def my_view(request):
> current_site = get_current_site(request)
> if current_site.domain == 'foo.com':
> # Do something
> pass
> else:
> # Do something else.
> pass
> }}}
>
> I don't see any reason why the domain field shouldn't be unique and hence
> I consider this to be a bug and I believe that there should be a
> validation that the value domain of the domain field is unique.
>
> Quick work around:
>
> {{{#!python
> from django.contrib.sites.models import Site
>
> class CmsSite(Site):
>
> def clean(self, *args, **kwargs):
> if self.__class__._default_manager.filter(domain =
> self.domain).exists():
> raise ValidationError('Domain already exits')
> super(CmsSite, self).validate_unique(*args, **kwargs)
>
> def save(self, *args, **kwargs):
> self.full_clean()
> super(CmsSite, self).save(*args, **kwargs)
> }}}
New description:
In the django.contrib.sites.models the domain field of the Site model is
defined like this:
{{{#!python
domain = models.CharField(_('domain name'), max_length=100,
validators=[_simple_domain_name_validator])
}}}
I'd like to stress that the field doesn't have the requirement to be
unique. Neither by having unique = True or by the
_simple_domain_name_validator, which only checks for whitespaces and
typos.
In the case that a user (or two different users) would add the same domain
(ie "example.com") then the following example from the documentation would
go wrong or at least give a completely wrong result:
{{{#!python
from django.contrib.sites.shortcuts import get_current_site
def my_view(request):
current_site = get_current_site(request)
if current_site.domain == 'foo.com':
# Do something
pass
else:
# Do something else.
pass
}}}
I don't see any reason why the domain field shouldn't be unique and hence
I consider this to be a bug and I believe that there should be a
validation that the value domain of the domain field is unique.
Quick work around:
{{{#!python
from django.contrib.sites.models import Site
class CmsSite(Site):
def clean(self, *args, **kwargs):
if self.__class__._default_manager.filter(domain =
self.domain).exists():
raise ValidationError('Domain already exits')
super(CmsSite, self).clean(*args, **kwargs)
def save(self, *args, **kwargs):
self.full_clean()
super(CmsSite, self).save(*args, **kwargs)
}}}
--
--
Ticket URL: <https://code.djangoproject.com/ticket/24774#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/064.de59a9ddc8d47ec7f150232682787ef8%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.