From e62a78775cceceaea591bac467c9db84cff1bf16 Mon Sep 17 00:00:00 2001 From: Andreas Divaris Date: Thu, 30 Nov 2023 16:06:42 -0800 Subject: [PATCH 1/5] feat: OPTIC-184: Remove is_deleted field from User model and manager --- label_studio/users/admin.py | 4 ---- .../0001_squashed_0009_auto_20210219_1237.py | 5 +---- label_studio/users/models.py | 20 ++----------------- 3 files changed, 3 insertions(+), 26 deletions(-) diff --git a/label_studio/users/admin.py b/label_studio/users/admin.py index 241ae6338cca..309f48455024 100644 --- a/label_studio/users/admin.py +++ b/label_studio/users/admin.py @@ -15,10 +15,6 @@ class UserAdminShort(UserAdmin): add_fieldsets = ((None, {'fields': ('email', 'password1', 'password2')}),) - def get_queryset(self, request): - # Use the with_deleted method to include soft-deleted users in the queryset - return User.with_deleted.all() - def __init__(self, *args, **kwargs): super(UserAdminShort, self).__init__(*args, **kwargs) diff --git a/label_studio/users/migrations/0001_squashed_0009_auto_20210219_1237.py b/label_studio/users/migrations/0001_squashed_0009_auto_20210219_1237.py index 7281d075f036..51618d695a59 100644 --- a/label_studio/users/migrations/0001_squashed_0009_auto_20210219_1237.py +++ b/label_studio/users/migrations/0001_squashed_0009_auto_20210219_1237.py @@ -12,7 +12,7 @@ def add_tokens(apps, schema_editor): User = apps.get_model('users', 'User') - all_users = User.with_deleted.all() + all_users = User.objects.all() for user_one in all_users: if not hasattr(user_one, 'auth_token'): @@ -67,9 +67,6 @@ class Migration(migrations.Migration): 'verbose_name_plural': 'users', 'db_table': 'htx_user', }, - managers=[ - ('with_deleted', users.models.UserManagerWithDeleted()), - ], ), migrations.RunPython(add_tokens), # migrations.RunPython(add_users), # TODO: flag:ent diff --git a/label_studio/users/models.py b/label_studio/users/models.py index ad13a1e194af..a8b9b9e2857b 100644 --- a/label_studio/users/models.py +++ b/label_studio/users/models.py @@ -26,8 +26,8 @@ year = models.IntegerField(_('year'), choices=YEAR_CHOICES, default=datetime.datetime.now().year) -class UserManagerWithDeleted(BaseUserManager): - use_in_migrations: bool = True +class UserManager(BaseUserManager): + use_in_migrations = True def _create_user(self, email, password, **extra_fields): """ @@ -61,15 +61,6 @@ def create_superuser(self, email, password, **extra_fields): return self._create_user(email, password, **extra_fields) -class UserManager(UserManagerWithDeleted): - use_in_migrations: bool = False - - def get_queryset(self): - qs = super().get_queryset() - qs = qs.filter(is_deleted=False) - return qs - - class UserLastActivityMixin(models.Model): last_activity = models.DateTimeField(_('last activity'), default=timezone.now, editable=False) @@ -109,12 +100,6 @@ class User(UserMixin, AbstractBaseUser, PermissionsMixin, UserLastActivityMixin) default=True, help_text=_('Designates whether to treat this user as active. Unselect this instead of deleting accounts.'), ) - is_deleted = models.BooleanField( - _('deleted'), - default=False, - db_index=True, - help_text=_('Designates whether to treat this user as deleted. Select this instead of deleting accounts.'), - ) date_joined = models.DateTimeField(_('date joined'), default=timezone.now) @@ -129,7 +114,6 @@ class User(UserMixin, AbstractBaseUser, PermissionsMixin, UserLastActivityMixin) ) objects = UserManager() - with_deleted = UserManagerWithDeleted() EMAIL_FIELD = 'email' USERNAME_FIELD = 'email' From 05d748fdfa55e4f7d94b3a420856ee9a1f218fa6 Mon Sep 17 00:00:00 2001 From: Andreas Divaris Date: Thu, 30 Nov 2023 16:24:47 -0800 Subject: [PATCH 2/5] Rewrite history as manager no longer exists --- label_studio/users/migrations/0008_alter_user_managers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/label_studio/users/migrations/0008_alter_user_managers.py b/label_studio/users/migrations/0008_alter_user_managers.py index 44bf404ed6b7..0a145105b72e 100644 --- a/label_studio/users/migrations/0008_alter_user_managers.py +++ b/label_studio/users/migrations/0008_alter_user_managers.py @@ -16,7 +16,7 @@ class Migration(migrations.Migration): name='user', managers=[ ('objects', django.db.models.manager.Manager()), - ('with_deleted', users.models.UserManagerWithDeleted()), + # ('with_deleted', users.models.UserManagerWithDeleted()), ], ), ] From 0aa1f3649a7f87fcedd77c3394aa7dd34675c06b Mon Sep 17 00:00:00 2001 From: Andreas Divaris Date: Thu, 30 Nov 2023 17:02:36 -0800 Subject: [PATCH 3/5] Add migration to remove no longer needed manager from users --- .../migrations/0009_auto_20231201_0001.py | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 label_studio/users/migrations/0009_auto_20231201_0001.py diff --git a/label_studio/users/migrations/0009_auto_20231201_0001.py b/label_studio/users/migrations/0009_auto_20231201_0001.py new file mode 100644 index 000000000000..aef96bc56aae --- /dev/null +++ b/label_studio/users/migrations/0009_auto_20231201_0001.py @@ -0,0 +1,24 @@ +# Generated by Django 3.2.23 on 2023-12-01 00:01 + +from django.db import migrations +import users.models + + +class Migration(migrations.Migration): + + dependencies = [ + ('users', '0008_alter_user_managers'), + ] + + operations = [ + migrations.AlterModelManagers( + name='user', + managers=[ + ('objects', users.models.UserManager()), + ], + ), + migrations.RemoveField( + model_name='user', + name='is_deleted', + ), + ] From 7987c73e82fd055141841cb9a8473f23fea91279 Mon Sep 17 00:00:00 2001 From: Andreas Divaris Date: Fri, 1 Dec 2023 12:07:58 -0800 Subject: [PATCH 4/5] Remove reference to deleted manager from migration --- label_studio/users/migrations/0008_alter_user_managers.py | 1 - 1 file changed, 1 deletion(-) diff --git a/label_studio/users/migrations/0008_alter_user_managers.py b/label_studio/users/migrations/0008_alter_user_managers.py index 0a145105b72e..04233da1ca81 100644 --- a/label_studio/users/migrations/0008_alter_user_managers.py +++ b/label_studio/users/migrations/0008_alter_user_managers.py @@ -16,7 +16,6 @@ class Migration(migrations.Migration): name='user', managers=[ ('objects', django.db.models.manager.Manager()), - # ('with_deleted', users.models.UserManagerWithDeleted()), ], ), ] From 3a84764b2cb7d887395b22940282212adb1a6ec7 Mon Sep 17 00:00:00 2001 From: Andreas Divaris Date: Fri, 1 Dec 2023 12:10:49 -0800 Subject: [PATCH 5/5] Add documentation to note removal of manager --- label_studio/users/migrations/0008_alter_user_managers.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/label_studio/users/migrations/0008_alter_user_managers.py b/label_studio/users/migrations/0008_alter_user_managers.py index 04233da1ca81..5a9dd03f1644 100644 --- a/label_studio/users/migrations/0008_alter_user_managers.py +++ b/label_studio/users/migrations/0008_alter_user_managers.py @@ -16,6 +16,8 @@ class Migration(migrations.Migration): name='user', managers=[ ('objects', django.db.models.manager.Manager()), + # Previously, this migration contained an addition of UserManagerWithDeleted, which has since been + # removed in order to avoid referencing a non-existent manager. ], ), ]