def handle(self, *args, **options): if len(args) > 1: raise CommandError( "need exactly one or zero arguments for username") if args: username, = args else: username = getpass.getuser() User = get_user_model() try: u = User.objects.get(username=username) except User.DoesNotExist: raise CommandError("user %s does not exist" % username) print("Changing password for user: %s" % u.username) p1 = p2 = "" while "" in (p1, p2) or p1 != p2: p1 = getpass.getpass() p2 = getpass.getpass("Password (again): ") if p1 != p2: print("Passwords do not match, try again") elif "" in (p1, p2): raise CommandError("aborted") u.set_password(p1) u.save() return "Password changed successfully for user %s\n" % u.username
def handle(self, *args, **options): if len(args) > 1: raise CommandError("need exactly one or zero arguments for username") if args: username, = args else: username = getpass.getuser() User = get_user_model() try: u = User.objects.get(username=username) except User.DoesNotExist: raise CommandError("user %s does not exist" % username) print("Changing password for user: %s" % u.username) p1 = p2 = "" while "" in (p1, p2) or p1 != p2: p1 = getpass.getpass() p2 = getpass.getpass("Password (again): ") if p1 != p2: print("Passwords do not match, try again") elif "" in (p1, p2): raise CommandError("aborted") u.set_password(p1) u.save() return "Password changed successfully for user %s\n" % u.username
def handle(self, *args, **options): if len(args) > 1: raise CommandError("extra arguments supplied") if len(args) < 1: raise CommandError("session_key argument missing") key = args[0].lower() if not set(key).issubset(set(VALID_KEY_CHARS)): raise CommandError("malformed session key") engine = importlib.import_module(settings.SESSION_ENGINE) if not engine.SessionStore().exists(key): print("Session Key does not exist. Expired?") return session = engine.SessionStore(key) data = session.load() print('Session to Expire: %s' % session.get_expiry_date()) print('Raw Data: %s' % data) uid = data.get('_auth_user_id', None) if uid is None: print('No user associated with session') return print("User id: %s" % uid) User = get_user_model() try: user = User.objects.get(pk=uid) except User.DoesNotExist: print("No user associated with that id.") return username_field = 'username' if hasattr(User, 'USERNAME_FIELD') and User.USERNAME_FIELD is not None: username_field = User.USERNAME_FIELD for key in [username_field, 'email', 'first_name', 'last_name']: print("%s: %s" % (key, getattr(user, key)))
def handle(self, *args, **options): if not settings.DEBUG: raise CommandError('Only available in debug mode') from django_extensions.compat import get_user_model from django.contrib.auth.models import Group email = options.get('default_email', DEFAULT_FAKE_EMAIL) include_regexp = options.get('include_regexp', None) exclude_regexp = options.get('exclude_regexp', None) include_groups = options.get('include_groups', None) exclude_groups = options.get('exclude_groups', None) no_admin = options.get('no_admin', False) no_staff = options.get('no_staff', False) User = get_user_model() users = User.objects.all() if no_admin: users = users.exclude(is_superuser=True) if no_staff: users = users.exclude(is_staff=True) if exclude_groups: groups = Group.objects.filter(name__in=exclude_groups.split(",")) if groups: users = users.exclude(groups__in=groups) else: raise CommandError("No group matches filter: %s" % exclude_groups) if include_groups: groups = Group.objects.filter(name__in=include_groups.split(",")) if groups: users = users.filter(groups__in=groups) else: raise CommandError("No groups matches filter: %s" % include_groups) if exclude_regexp: users = users.exclude(username__regex=exclude_regexp) if include_regexp: users = users.filter(username__regex=include_regexp) for user in users: user.email = email % { 'username': user.username, 'first_name': user.first_name, 'last_name': user.last_name } user.save() print('Changed %d emails' % users.count())
def handle_noargs(self, **options): if not settings.DEBUG: raise CommandError('Only available in debug mode') from django_extensions.compat import get_user_model from django.contrib.auth.models import Group email = options.get('default_email', DEFAULT_FAKE_EMAIL) include_regexp = options.get('include_regexp', None) exclude_regexp = options.get('exclude_regexp', None) include_groups = options.get('include_groups', None) exclude_groups = options.get('exclude_groups', None) no_admin = options.get('no_admin', False) no_staff = options.get('no_staff', False) User = get_user_model() users = User.objects.all() if no_admin: users = users.exclude(is_superuser=True) if no_staff: users = users.exclude(is_staff=True) if exclude_groups: groups = Group.objects.filter(name__in=exclude_groups.split(",")) if groups: users = users.exclude(groups__in=groups) else: raise CommandError("No group matches filter: %s" % exclude_groups) if include_groups: groups = Group.objects.filter(name__in=include_groups.split(",")) if groups: users = users.filter(groups__in=groups) else: raise CommandError("No groups matches filter: %s" % include_groups) if exclude_regexp: users = users.exclude(username__regex=exclude_regexp) if include_regexp: users = users.filter(username__regex=include_regexp) for user in users: user.email = email % {'username': user.username, 'first_name': user.first_name, 'last_name': user.last_name} user.save() print('Changed %d emails' % users.count())
def handle(self, *args, **options): if not settings.DEBUG: raise CommandError('Only available in debug mode') from django_extensions.compat import get_user_model if options.get('prompt_passwd', False): from getpass import getpass passwd = getpass('Password: '******'You must enter a valid password') else: passwd = options.get('default_passwd', DEFAULT_FAKE_PASSWORD) User = get_user_model() user = User() user.set_password(passwd) count = User.objects.all().update(password=user.password) print('Reset %d passwords' % count)
def handle(self, *args, **options): if len(args) > 1: raise CommandError("extra arguments supplied") group = options['group'] if group and not Group.objects.filter(name=group).count() == 1: names = six.u("', '").join(g['name'] for g in Group.objects.values('name')).encode('utf-8') if names: names = "'" + names + "'." raise CommandError("Unknown group '" + group + "'. Valid group names are: " + names) if len(args) and args[0] != '-': outfile = open(args[0], 'w') else: outfile = stdout User = get_user_model() qs = User.objects.all().order_by('last_name', 'first_name', 'username', 'email') if group: qs = qs.filter(groups__name=group).distinct() qs = qs.values('last_name', 'first_name', 'username', 'email') getattr(self, options['format'])(qs, outfile)