Skip to content

Commit

Permalink
add soft deletions attributes in profile class + override deletion me…
Browse files Browse the repository at this point in the history
…thod + update email and log in mechanism
  • Loading branch information
OhMaley committed Dec 4, 2024
1 parent 6cbb09f commit 5d7ef98
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/apps/competitions/emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@


def get_organizer_emails(competition):
return [user.email for user in competition.all_organizers]
return [user.email for user in competition.all_organizers if not user.is_deleted]


def send_participation_requested_emails(participant):
if participant.user.is_deleted: return

context = {
'participant': participant
}
Expand All @@ -29,6 +31,8 @@ def send_participation_requested_emails(participant):


def send_participation_accepted_emails(participant):
if participant.user.is_deleted: return

context = {
'participant': participant
}
Expand All @@ -50,6 +54,8 @@ def send_participation_accepted_emails(participant):


def send_participation_denied_emails(participant):
if participant.user.is_deleted: return

context = {
'participant': participant
}
Expand All @@ -72,6 +78,8 @@ def send_participation_denied_emails(participant):


def send_direct_participant_email(participant, content):
if participant.user.is_deleted: return

codalab_send_markdown_email(
subject=f'A message from the admins of {participant.competition.title}',
markdown_content=content,
Expand Down
23 changes: 23 additions & 0 deletions src/apps/profiles/migrations/0014_auto_20241120_1607.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 2.2.28 on 2024-11-20 16:07

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('profiles', '0013_auto_20240304_0616'),
]

operations = [
migrations.AddField(
model_name='user',
name='deleted_at',
field=models.DateTimeField(blank=True, null=True),
),
migrations.AddField(
model_name='user',
name='is_deleted',
field=models.BooleanField(default=False),
),
]
25 changes: 25 additions & 0 deletions src/apps/profiles/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ class User(ChaHubSaveMixin, AbstractBaseUser, PermissionsMixin):
# Required for social auth and such to create users
objects = ChaHubUserManager()

# Soft deletion
is_deleted = models.BooleanField(default=False)
deleted_at = models.DateTimeField(null=True, blank=True)

def save(self, *args, **kwargs):
self.slug = slugify(self.username, allow_unicode=True)
super().save(*args, **kwargs)
Expand Down Expand Up @@ -194,6 +198,27 @@ def get_used_storage_space(self):
return storage_used


def delete(self, *args, **kwargs):
"""Soft delete the user and anonymize personal data."""
# Mark the user as deleted
self.is_deleted = True
self.deleted_at = now()

# Anonymize personal data
# TODO add all personal data that needs to be anonymized
self.email = f"deleted_{uuid.uuid4()}@domain.com"
self.username = f"deleted_user_{self.id}"

# Save the changes
self.save()

def restore(self, *args, **kwargs):
"""Restore a soft-deleted user. Note that personal data remains anonymized."""
self.is_deleted = False
self.deleted_at = None
self.save()


class GithubUserInfo(models.Model):
# Required Info
uid = models.CharField(max_length=30, unique=True)
Expand Down
2 changes: 1 addition & 1 deletion src/apps/profiles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def log_in(request):

# Check if the user exists
try:
user = User.objects.get(Q(username=username) | Q(email=username))
user = User.objects.get((Q(username=username) | Q(email=username)) & Q(is_deleted=False))
except User.DoesNotExist:
messages.error(request, "User does not exist!")
else:
Expand Down

0 comments on commit 5d7ef98

Please sign in to comment.