|
5 | 5 | import logging
|
6 | 6 |
|
7 | 7 | from corsheaders import signals
|
| 8 | +from django.contrib.auth import get_user_model |
| 9 | +from django.db.models.signals import pre_delete |
8 | 10 | from django.dispatch import Signal
|
9 |
| -from django.db.models import Q |
| 11 | +from django.db.models import Q, Count |
| 12 | +from django.dispatch import receiver |
10 | 13 | from future.backports.urllib.parse import urlparse
|
11 | 14 |
|
| 15 | +from readthedocs.oauth.tasks import bulk_delete_oauth_remote_organizations |
12 | 16 | from readthedocs.projects.models import Project, Domain
|
13 |
| - |
| 17 | +from readthedocs.projects.tasks import bulk_delete_projects |
14 | 18 |
|
15 | 19 | log = logging.getLogger(__name__)
|
| 20 | +User = get_user_model() |
16 | 21 |
|
17 | 22 | WHITELIST_URLS = ['/api/v2/footer_html', '/api/v2/search', '/api/v2/docsearch']
|
18 | 23 |
|
@@ -62,4 +67,23 @@ def decide_if_cors(sender, request, **kwargs): # pylint: disable=unused-argumen
|
62 | 67 |
|
63 | 68 | return False
|
64 | 69 |
|
| 70 | + |
| 71 | +@receiver(pre_delete, sender=User) |
| 72 | +def delete_projects_and_organizations(instance, *args, **kwargs): |
| 73 | + # Here we count the owner list from the projects that the user own |
| 74 | + # Then exclude the projects where there are more than one owner |
| 75 | + projects_id = (instance.projects.all().annotate(num_users=Count('users')) |
| 76 | + .exclude(num_users__gt=1) |
| 77 | + .values_list('id', flat=True)) |
| 78 | + |
| 79 | + # Here we count the users list from the organization that the user belong |
| 80 | + # Then exclude the organizations where there are more than one user |
| 81 | + oauth_organizations_id = (instance.oauth_organizations.annotate(num_users=Count('users')) |
| 82 | + .exclude(num_users__gt=1) |
| 83 | + .values_list('id', flat=True)) |
| 84 | + |
| 85 | + bulk_delete_projects.delay(projects_id) |
| 86 | + bulk_delete_oauth_remote_organizations.delay(oauth_organizations_id) |
| 87 | + |
| 88 | + |
65 | 89 | signals.check_request_enabled.connect(decide_if_cors)
|
0 commit comments