|
12 | 12 | from django.dispatch import receiver
|
13 | 13 | from future.backports.urllib.parse import urlparse
|
14 | 14 |
|
| 15 | +from readthedocs.oauth.models import RemoteOrganization |
15 | 16 | from readthedocs.projects.models import Project, Domain
|
16 | 17 |
|
17 | 18 | log = logging.getLogger(__name__)
|
@@ -79,12 +80,16 @@ def decide_if_cors(sender, request, **kwargs): # pylint: disable=unused-argumen
|
79 | 80 | def delete_projects_and_organizations(sender, instance, *args, **kwargs):
|
80 | 81 | # Here we count the owner list from the projects that the user own
|
81 | 82 | # Then exclude the projects where there are more than one owner
|
82 |
| - projects = instance.projects.all().annotate(num_users=Count('users')).exclude(num_users__gt=1) |
| 83 | + # Add annotate before filter |
| 84 | + # https://bit.ly/2Nne6ZJ |
| 85 | + projects = (Project.objects.annotate(num_users=Count('users')).filter(users=instance.id) |
| 86 | + .exclude(num_users__gt=1)) |
83 | 87 |
|
84 | 88 | # Here we count the users list from the organization that the user belong
|
85 | 89 | # Then exclude the organizations where there are more than one user
|
86 |
| - oauth_organizations = (instance.oauth_organizations.annotate(num_users=Count('users')) |
87 |
| - .exclude(num_users__gt=1)) |
| 90 | + oauth_organizations = (RemoteOrganization.objects.annotate(num_users=Count('users')) |
| 91 | + .filter(users=instance.id) |
| 92 | + .exclude(num_users__gt=1)) |
88 | 93 |
|
89 | 94 | projects.delete()
|
90 | 95 | oauth_organizations.delete()
|
|
0 commit comments