[Django] #20541: Differenciate user from superuser creation in django.contrib.auth at a signal level

18 views
Skip to first unread message

Django

unread,
May 31, 2013, 4:39:14 PM5/31/13
to django-...@googlegroups.com
#20541: Differenciate user from superuser creation in django.contrib.auth at a
signal level
--------------------------------------+--------------------
Reporter: antonio@… | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: contrib.auth | Version: 1.5
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 1
Easy pickings: 1 | UI/UX: 0
--------------------------------------+--------------------
Hi all,

Currently the steps to create a superuser
(auth.models.User.create_superuser) are:

1) Create user
2) Save User
3) Assign staff, active, superuser
4) Save again

When the first save happens the post_save signal is raised, but the user
is not a superuser yet. The signal will get raised again after a few
lines, which is wasteful. My proposal is to implement superuser creation
like this:

{{{
class UserManager(BaseUserManager):

def create_user(self, username, email=None, password=None, save=True,
**extra_fields):
"""
Creates and saves a User with the given username, email and
password.
"""
now = timezone.now()
if not username:
raise ValueError('The given username must be set')
email = self.normalize_email(email)
user = self.model(username=username, email=email,
is_staff=False, is_active=True,
is_superuser=False,
last_login=now, date_joined=now, **extra_fields)

user.set_password(password)
if save:
user.save(using=self._db)
return user

def create_superuser(self, username, email, password, **extra_fields):
u = self.create_user(username, email, password, save=False,
**extra_fields)
u.is_staff = True
u.is_active = True
u.is_superuser = True
u.save(using=self._db)
return u
}}}

Now when post_save is fired, one can check is_superuser, is_staff or
is_active and take action based on that.

--
Ticket URL: <https://code.djangoproject.com/ticket/20541>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
May 31, 2013, 9:09:28 PM5/31/13
to django-...@googlegroups.com
#20541: Differenciate user from superuser creation in django.contrib.auth at a
signal level
--------------------------------------+------------------------------------

Reporter: antonio@… | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: contrib.auth | Version: 1.5
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by russellm):

* needs_better_patch: => 0
* needs_docs: => 0
* needs_tests: => 0
* stage: Unreviewed => Accepted


Comment:

I fully accept that the underlying problem -- that saving a superuser
involves 2 saves -- should be solved. I'm just not completely convinced
about the solution. The extra save=True argument seems messy to me.

Personally, I'd rather see the 'guts' of create_user factored out into in
internal method that is called by both create_user and create_superuser -
that way, the public call to create_user would end up looking a lot like
create_superuser, but with reversed boolean values.

--
Ticket URL: <https://code.djangoproject.com/ticket/20541#comment:1>

Django

unread,
Jun 25, 2013, 5:13:46 PM6/25/13
to django-...@googlegroups.com
#20541: Differenciate user from superuser creation in django.contrib.auth at a
signal level
--------------------------------------+------------------------------------
Reporter: antonio@… | Owner: bak1an
Type: Cleanup/optimization | Status: assigned
Component: contrib.auth | Version: 1.5

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by bak1an):

* status: new => assigned
* cc: antonbaklanov@… (added)
* owner: nobody => bak1an


Comment:

here is my pull request for this
https://github.com/django/django/pull/1305

--
Ticket URL: <https://code.djangoproject.com/ticket/20541#comment:2>

Django

unread,
Jun 27, 2013, 6:59:23 AM6/27/13
to django-...@googlegroups.com
#20541: Differenciate user from superuser creation in django.contrib.auth at a
signal level
--------------------------------------+------------------------------------
Reporter: antonio@… | Owner: bak1an
Type: Cleanup/optimization | Status: closed
Component: contrib.auth | Version: 1.5
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"cab333cb16656cbb7d2bcfb06b2f7aeab9bac7af"]:
{{{
#!CommitTicketReference repository=""
revision="cab333cb16656cbb7d2bcfb06b2f7aeab9bac7af"
Fixed #20541 -- don't raise db signals twice when creating superuser
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/20541#comment:3>

Reply all
Reply to author
Forward
0 new messages