mirror of
https://github.com/ansible/awx.git
synced 2026-03-13 15:09:32 -02:30
Revert previous fix for deleted name change, update how we change username when a user is marked inactive to keep it under 30 chars.
This commit is contained in:
@@ -11,7 +11,7 @@ from django.core.management.base import BaseCommand, CommandError
|
||||
from django.db import transaction
|
||||
from django.contrib.auth.models import User
|
||||
from django.utils.dateparse import parse_datetime
|
||||
from django.utils.timezone import now
|
||||
from django.utils.timezone import now, is_aware, make_aware
|
||||
|
||||
# AWX
|
||||
from awx.main.models import *
|
||||
@@ -53,13 +53,19 @@ class Command(BaseCommand):
|
||||
if not active_field:
|
||||
self.logger.warning('skipping model %s, no active field', model)
|
||||
return
|
||||
if name_field == 'username' and active_field == 'is_active':
|
||||
name_prefix = '_d_'
|
||||
else:
|
||||
name_prefix = '_deleted_'
|
||||
qs = model.objects.filter(**{
|
||||
active_field: False,
|
||||
'%s__startswith' % name_field: '_d',
|
||||
'%s__startswith' % name_field: name_prefix,
|
||||
})
|
||||
self.logger.debug('cleaning up model %s', model)
|
||||
for instance in qs:
|
||||
dt = parse_datetime(getattr(instance, name_field).split('_')[2])
|
||||
if not is_aware(dt):
|
||||
dt = make_aware(dt, self.cutoff.tzinfo)
|
||||
if not dt:
|
||||
self.logger.warning('unable to find deleted timestamp in %s '
|
||||
'field', name_field)
|
||||
|
||||
@@ -98,18 +98,7 @@ class PrimordialModel(models.Model):
|
||||
|
||||
if self.active:
|
||||
if 'name' in self._meta.get_all_field_names():
|
||||
# 0 1
|
||||
# 01234567890123
|
||||
has_description = False
|
||||
if 'description' in self._meta.get_all_field_names():
|
||||
has_description = True
|
||||
old_desc = ''
|
||||
if has_description:
|
||||
old_desc = self.description
|
||||
self.description = "deleted: %s" % self.name
|
||||
if old_desc:
|
||||
self.description = "%s (%s)" % (self.description, old_desc)
|
||||
self.name = "_d_%s" % (now().isoformat())
|
||||
self.name = "_deleted_%s_%s" % (now().isoformat(), self.name)
|
||||
self.active = False
|
||||
if save:
|
||||
self.save()
|
||||
@@ -1247,7 +1236,10 @@ class JobEvent(models.Model):
|
||||
def user_mark_inactive(user, save=True):
|
||||
'''Use instead of delete to rename and mark users inactive.'''
|
||||
if user.is_active:
|
||||
user.username = "_deleted_%s_%s" % (now().isoformat(), user.username)
|
||||
# Set timestamp to datetime.isoformat() but without the time zone
|
||||
# offse to stay withint the 30 character username limit.
|
||||
deleted_ts = now().strftime('%Y-%m-%dT%H:%M:%S.%f')
|
||||
user.username = '_d_%s' % deleted_ts
|
||||
user.is_active = False
|
||||
if save:
|
||||
user.save()
|
||||
|
||||
@@ -226,6 +226,8 @@ class CleanupDeletedTest(BaseCommandTest):
|
||||
# "Delete some users".
|
||||
for user in User.objects.all():
|
||||
user.mark_inactive()
|
||||
self.assertTrue(len(user.username) <= 30,
|
||||
'len(%r) == %d' % (user.username, len(user.username)))
|
||||
# With days=1, no users will be deleted.
|
||||
counts_before = self.get_user_counts()
|
||||
self.assertTrue(counts_before[1])
|
||||
|
||||
Reference in New Issue
Block a user