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