Skip to content

Commit

Permalink
fix: delete projects for user
Browse files Browse the repository at this point in the history
  • Loading branch information
paulschreiber committed Oct 2, 2024
1 parent 2c811a1 commit 80700f6
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion terraso_backend/apps/core/management/commands/delete_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@

from django.core.exceptions import ValidationError
from django.core.management.base import BaseCommand, CommandError
from django.db.models import Count, ProtectedError

from apps.core.models import User
from apps.project_management.models import Project


class Command(BaseCommand):
Expand All @@ -40,4 +42,16 @@ def handle(self, *args, **options):
except (User.DoesNotExist, ValidationError):
raise CommandError(f"Please specify a valid user ID [input: {user_id}]")

user.delete()
# projects where the user is the only member
projects = Project.objects.annotate(members_count=Count("group__members__id")).filter(
members_count=1, group__members__id=user.id
)

for project in projects:
project.delete()

# NOTE: user deletion currently fails due to audit logs
try:
user.delete()
except ProtectedError:
raise CommandError(f"Unable to delete user {user}")

0 comments on commit 80700f6

Please sign in to comment.