diff --git a/awx/main/management/commands/cleanup_deleted.py b/awx/main/management/commands/cleanup_deleted.py index d898a42f7c..db48558694 100644 --- a/awx/main/management/commands/cleanup_deleted.py +++ b/awx/main/management/commands/cleanup_deleted.py @@ -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) diff --git a/awx/main/models/__init__.py b/awx/main/models/__init__.py index 866daa5a48..8ea35858c4 100644 --- a/awx/main/models/__init__.py +++ b/awx/main/models/__init__.py @@ -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() diff --git a/awx/main/tests/commands.py b/awx/main/tests/commands.py index eae6c351fc..f731fc9fba 100644 --- a/awx/main/tests/commands.py +++ b/awx/main/tests/commands.py @@ -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])