adduser.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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.users.models import UserManager, Group
  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 addres and password')
  19. # Get group
  20. if options['admin']:
  21. group = Group.objects.get(pk=1)
  22. else:
  23. group = Group.objects.get(pk=3)
  24. # Set user
  25. try:
  26. manager = UserManager()
  27. manager.create_user(args[0], args[1], args[2], group=group)
  28. except ValidationError as e:
  29. raise CommandError("New user cannot be created because of following errors:\n\n%s" % '\n'.join(e.messages))
  30. if options['admin']:
  31. self.stdout.write('Successfully created new administrator "%s"' % args[0])
  32. else:
  33. self.stdout.write('Successfully created new user "%s"' % args[0])
  34. self.stdout.write('\n\nNew user should use "%s" e-mail address and "%s" password to sign in.\n' % (args[1], args[2]))