#26745: Add the possibilty to extend the createsuper user command
------------------------------+--------------------
     Reporter:  Lyra2108      |      Owner:  nobody
         Type:  New feature   |     Status:  new
    Component:  contrib.auth  |    Version:  master
     Severity:  Normal        |   Keywords:
 Triage Stage:  Unreviewed    |  Has patch:  0
Easy pickings:  1             |      UI/UX:  0
------------------------------+--------------------
 We want to perform actions after the creation of the superuser. But we do
 not want to rewrite the createsuper command entirely.

 This should be done in a transaction, because if the post actions fail the
 user shall not be created.
 Additionally it might be useful to also have a pre create action which can
 modify the user data passed to the ``create_superuser()`` method on the
 user manager and add non required parameters to the user data before the
 creation of the super user.

 We thought about something along these lines:

 {{{#!diff
 --- ./django/contrib/auth/management/commands/createsuperuser.py
 2016-06-11 01:02:02.307032020 +0200
 +++ /home/markus/.venvs/inyoka/lib/python2.7/site-
 packages/django/contrib/auth/management/commands/createsuperuser.py
 2016-06-11 00:58:07.887693597 +0200
 @@ -10,7 +10,7 @@
  from django.contrib.auth.management import get_default_username
  from django.core import exceptions
  from django.core.management.base import BaseCommand, CommandError
 -from django.db import DEFAULT_DB_ALIAS
 +from django.db import DEFAULT_DB_ALIAS, transaction
  from django.utils.encoding import force_str
  from django.utils.six.moves import input
  from django.utils.text import capfirst
 @@ -146,10 +146,19 @@
          if username:
              user_data[self.UserModel.USERNAME_FIELD] = username
              user_data['password'] = password
 -
 
self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)
 +            with transaction.atomic():
 +                user_data = self.pre_create(user_data)
 +                user =
 
self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)
 +                self.post_create(user)
              if options['verbosity'] >= 1:
                  self.stdout.write("Superuser created successfully.")

 +    def pre_create(self, user_data):
 +        return user_data
 +
 +    def post_create(self, user):
 +        pass
 +
      def get_input_data(self, field, message, default=None):
          """
          Override this method if you want to customize data inputs or
 }}}
 Diff is against v1.8.13 but this shouldn't make a difference.

--
Ticket URL: <https://code.djangoproject.com/ticket/26745>
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/051.1d0a9405caddc733d12d79db067fb2ed%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to