Skip to content

Commit

Permalink
Revert "feat: OPTIC-184: Remove is_deleted field from User model and …
Browse files Browse the repository at this point in the history
…manager (#5125)"

This reverts commit 32eeea4.
  • Loading branch information
dredivaris authored Dec 8, 2023
1 parent 32eeea4 commit 9a39265
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 29 deletions.
4 changes: 4 additions & 0 deletions label_studio/users/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ 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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

def add_tokens(apps, schema_editor):
User = apps.get_model('users', 'User')
all_users = User.objects.all()
all_users = User.with_deleted.all()

for user_one in all_users:
if not hasattr(user_one, 'auth_token'):
Expand Down Expand Up @@ -67,6 +67,9 @@ 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
Expand Down
3 changes: 1 addition & 2 deletions label_studio/users/migrations/0008_alter_user_managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ 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.
('with_deleted', users.models.UserManagerWithDeleted()),
],
),
]
24 changes: 0 additions & 24 deletions label_studio/users/migrations/0009_auto_20231201_0001.py

This file was deleted.

20 changes: 18 additions & 2 deletions label_studio/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
year = models.IntegerField(_('year'), choices=YEAR_CHOICES, default=datetime.datetime.now().year)


class UserManager(BaseUserManager):
use_in_migrations = True
class UserManagerWithDeleted(BaseUserManager):
use_in_migrations: bool = True

def _create_user(self, email, password, **extra_fields):
"""
Expand Down Expand Up @@ -61,6 +61,15 @@ 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)

Expand Down Expand Up @@ -100,6 +109,12 @@ 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)

Expand All @@ -114,6 +129,7 @@ class User(UserMixin, AbstractBaseUser, PermissionsMixin, UserLastActivityMixin)
)

objects = UserManager()
with_deleted = UserManagerWithDeleted()

EMAIL_FIELD = 'email'
USERNAME_FIELD = 'email'
Expand Down

0 comments on commit 9a39265

Please sign in to comment.