mirror of
https://github.com/ansible/awx.git
synced 2026-05-20 07:17:40 -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.db import transaction
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from django.utils.dateparse import parse_datetime
|
from django.utils.dateparse import parse_datetime
|
||||||
from django.utils.timezone import now
|
from django.utils.timezone import now, is_aware, make_aware
|
||||||
|
|
||||||
# AWX
|
# AWX
|
||||||
from awx.main.models import *
|
from awx.main.models import *
|
||||||
@@ -53,13 +53,19 @@ class Command(BaseCommand):
|
|||||||
if not active_field:
|
if not active_field:
|
||||||
self.logger.warning('skipping model %s, no active field', model)
|
self.logger.warning('skipping model %s, no active field', model)
|
||||||
return
|
return
|
||||||
|
if name_field == 'username' and active_field == 'is_active':
|
||||||
|
name_prefix = '_d_'
|
||||||
|
else:
|
||||||
|
name_prefix = '_deleted_'
|
||||||
qs = model.objects.filter(**{
|
qs = model.objects.filter(**{
|
||||||
active_field: False,
|
active_field: False,
|
||||||
'%s__startswith' % name_field: '_d',
|
'%s__startswith' % name_field: name_prefix,
|
||||||
})
|
})
|
||||||
self.logger.debug('cleaning up model %s', model)
|
self.logger.debug('cleaning up model %s', model)
|
||||||
for instance in qs:
|
for instance in qs:
|
||||||
dt = parse_datetime(getattr(instance, name_field).split('_')[2])
|
dt = parse_datetime(getattr(instance, name_field).split('_')[2])
|
||||||
|
if not is_aware(dt):
|
||||||
|
dt = make_aware(dt, self.cutoff.tzinfo)
|
||||||
if not dt:
|
if not dt:
|
||||||
self.logger.warning('unable to find deleted timestamp in %s '
|
self.logger.warning('unable to find deleted timestamp in %s '
|
||||||
'field', name_field)
|
'field', name_field)
|
||||||
|
|||||||
@@ -98,18 +98,7 @@ class PrimordialModel(models.Model):
|
|||||||
|
|
||||||
if self.active:
|
if self.active:
|
||||||
if 'name' in self._meta.get_all_field_names():
|
if 'name' in self._meta.get_all_field_names():
|
||||||
# 0 1
|
self.name = "_deleted_%s_%s" % (now().isoformat(), self.name)
|
||||||
# 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.active = False
|
self.active = False
|
||||||
if save:
|
if save:
|
||||||
self.save()
|
self.save()
|
||||||
@@ -1247,7 +1236,10 @@ class JobEvent(models.Model):
|
|||||||
def user_mark_inactive(user, save=True):
|
def user_mark_inactive(user, save=True):
|
||||||
'''Use instead of delete to rename and mark users inactive.'''
|
'''Use instead of delete to rename and mark users inactive.'''
|
||||||
if user.is_active:
|
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
|
user.is_active = False
|
||||||
if save:
|
if save:
|
||||||
user.save()
|
user.save()
|
||||||
|
|||||||
@@ -226,6 +226,8 @@ class CleanupDeletedTest(BaseCommandTest):
|
|||||||
# "Delete some users".
|
# "Delete some users".
|
||||||
for user in User.objects.all():
|
for user in User.objects.all():
|
||||||
user.mark_inactive()
|
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.
|
# With days=1, no users will be deleted.
|
||||||
counts_before = self.get_user_counts()
|
counts_before = self.get_user_counts()
|
||||||
self.assertTrue(counts_before[1])
|
self.assertTrue(counts_before[1])
|
||||||
|
|||||||
Reference in New Issue
Block a user