Skip to content

Commit

Permalink
Organizations: move signals
Browse files Browse the repository at this point in the history
  • Loading branch information
stsewd committed Sep 3, 2020
1 parent 26f7b3c commit 01a4cc3
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions readthedocs/organizations/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
default_app_config = 'readthedocs.organizations.apps.OrganizationsConfig'
3 changes: 3 additions & 0 deletions readthedocs/organizations/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@
class OrganizationsConfig(AppConfig):

name = 'readthedocs.organizations'

def ready(self):
import readthedocs.organizations.signals # noqa
76 changes: 76 additions & 0 deletions readthedocs/organizations/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
"""Organization signals."""

import logging

from allauth.account.signals import user_signed_up
from django.db.models import Count
from django.db.models.signals import pre_delete
from django.dispatch import receiver

from readthedocs.builds.models import Version
from readthedocs.organizations.models import (
Organization,
Team,
TeamInvite,
TeamMember,
)
from readthedocs.projects.models import Project

log = logging.getLogger(__name__)


# pylint: disable=unused-argument
@receiver(user_signed_up)
def attach_org(sender, request, user, **kwargs):
"""Attach invited user to organization."""
team_slug = request.session.get('team')
if team_slug:
team = Team.objects.get(slug=team_slug)
TeamMember.objects.create(team=team, member=user)


# pylint: disable=unused-argument
@receiver(pre_delete, sender=Organization)
def remove_organization_completely(sender, instance, using, **kwargs):
"""
Remove Organization leaf-overs.
This includes:
- Projects
- Versions
- Builds (deleted on cascade)
- Teams
- Team Invitations
- Team Memberships
- Artifacts (HTML, PDF, etc)
"""
organization = instance
log.info('Removing Organization %s completely', organization.slug)

# ``Project`` has a ManyToMany relationship with ``Organization``. We need
# to be sure that the projects we are deleting here belongs only to the
# organization deleted
projects = Project.objects.annotate(
Count('organizations'),
).filter(
organizations__in=[organization],
organizations__count=1,
)

versions = Version.objects.filter(project__in=projects)
teams = Team.objects.filter(organization=organization)
team_invites = TeamInvite.objects.filter(organization=organization)
team_memberships = TeamMember.objects.filter(team__in=teams)

# Bulk delete
team_memberships.delete()
team_invites.delete()
teams.delete()

# Granular delete that trigger other complex tasks
for version in versions:
# Triggers a task to remove artifacts from storage
version.delete()

projects.delete()

0 comments on commit 01a4cc3

Please sign in to comment.