|
5 | 5 | import logging
|
6 | 6 |
|
7 | 7 | from corsheaders import signals
|
| 8 | +from django.conf import settings |
| 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 |
|
12 | 15 | from readthedocs.projects.models import Project, Domain
|
13 | 16 |
|
14 |
| - |
15 | 17 | log = logging.getLogger(__name__)
|
16 | 18 |
|
17 | 19 | WHITELIST_URLS = ['/api/v2/footer_html', '/api/v2/search', '/api/v2/docsearch']
|
@@ -62,4 +64,20 @@ def decide_if_cors(sender, request, **kwargs): # pylint: disable=unused-argumen
|
62 | 64 |
|
63 | 65 | return False
|
64 | 66 |
|
| 67 | + |
| 68 | +@receiver(pre_delete, sender=settings.AUTH_USER_MODEL) |
| 69 | +def delete_projects_and_organizations(sender, instance, *args, **kwargs): |
| 70 | + # Here we count the owner list from the projects that the user own |
| 71 | + # Then exclude the projects where there are more than one owner |
| 72 | + projects = instance.projects.all().annotate(num_users=Count('users')).exclude(num_users__gt=1) |
| 73 | + |
| 74 | + # Here we count the users list from the organization that the user belong |
| 75 | + # Then exclude the organizations where there are more than one user |
| 76 | + oauth_organizations = (instance.oauth_organizations.annotate(num_users=Count('users')) |
| 77 | + .exclude(num_users__gt=1)) |
| 78 | + |
| 79 | + projects.delete() |
| 80 | + oauth_organizations.delete() |
| 81 | + |
| 82 | + |
65 | 83 | signals.check_request_enabled.connect(decide_if_cors)
|
0 commit comments