|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
1 | 3 | """Signal handling for core app."""
|
2 | 4 |
|
3 | 5 | from __future__ import absolute_import
|
|
12 | 14 | from django.dispatch import receiver
|
13 | 15 | from future.backports.urllib.parse import urlparse
|
14 | 16 |
|
| 17 | +from readthedocs.oauth.models import RemoteOrganization |
15 | 18 | from readthedocs.projects.models import Project, Domain
|
16 | 19 |
|
17 | 20 | log = logging.getLogger(__name__)
|
|
23 | 26 | '/api/v2/sustainability',
|
24 | 27 | ]
|
25 | 28 |
|
26 |
| - |
27 | 29 | webhook_github = Signal(providing_args=['project', 'data', 'event'])
|
28 | 30 | webhook_gitlab = Signal(providing_args=['project', 'data', 'event'])
|
29 | 31 | webhook_bitbucket = Signal(providing_args=['project', 'data', 'event'])
|
@@ -79,12 +81,20 @@ def decide_if_cors(sender, request, **kwargs): # pylint: disable=unused-argumen
|
79 | 81 | def delete_projects_and_organizations(sender, instance, *args, **kwargs):
|
80 | 82 | # Here we count the owner list from the projects that the user own
|
81 | 83 | # 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) |
| 84 | + # Add annotate before filter |
| 85 | + # https://github.com/rtfd/readthedocs.org/pull/4577 |
| 86 | + # https://docs.djangoproject.com/en/2.1/topics/db/aggregation/#order-of-annotate-and-filter-clauses # noqa |
| 87 | + projects = ( |
| 88 | + Project.objects.annotate(num_users=Count('users')) |
| 89 | + .filter(users=instance.id).exclude(num_users__gt=1) |
| 90 | + ) |
83 | 91 |
|
84 | 92 | # Here we count the users list from the organization that the user belong
|
85 | 93 | # 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)) |
| 94 | + oauth_organizations = ( |
| 95 | + RemoteOrganization.objects.annotate(num_users=Count('users')) |
| 96 | + .filter(users=instance.id).exclude(num_users__gt=1) |
| 97 | + ) |
88 | 98 |
|
89 | 99 | projects.delete()
|
90 | 100 | oauth_organizations.delete()
|
|
0 commit comments