adduser.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. from django.core.exceptions import ValidationError, NON_FIELD_ERRORS
  2. from django.core.management.base import BaseCommand, CommandError
  3. from django.utils import timezone
  4. from optparse import make_option
  5. from misago.models import Role, User
  6. class Command(BaseCommand):
  7. args = 'username email password'
  8. help = 'Creates new user account'
  9. option_list = BaseCommand.option_list + (
  10. make_option('--admin',
  11. action='store_true',
  12. dest='admin',
  13. default=False,
  14. help='Make a new user an administrator'),
  15. )
  16. def handle(self, *args, **options):
  17. if len(args) < 3:
  18. raise CommandError('adduser requires exactly three arguments: user name, e-mail address and password')
  19. # Set user
  20. try:
  21. new_user = User.objects.create_user(args[0], args[1], args[2])
  22. except ValidationError as e:
  23. raise CommandError("New user cannot be created because of following errors:\n\n%s" % '\n'.join(e.messages))
  24. # Set admin role
  25. if options['admin']:
  26. new_user.roles.add(Role.objects.get(_special='admin'))
  27. new_user.make_acl_key(True)
  28. new_user.save(force_update=True)
  29. if options['admin']:
  30. self.stdout.write('Successfully created new administrator "%s"' % args[0])
  31. else:
  32. self.stdout.write('Successfully created new user "%s"' % args[0])
  33. self.stdout.write('\n\nNew user should use "%s" e-mail address and "%s" password to sign in.\n' % (args[1], args[2]))