Exclude users that have already filed a renewal from user_renewal_notice
@@ -2,11 +2,13 @@ from datetime import datetime, timedelta
from django.contrib.auth.models import User
from django.core.management.base import BaseCommand
-from django.db.models import Prefetch
+from django.db.models import Prefetch, Q
from django.urls import reverse
+from TWLight.applications.models import Application
+from TWLight.resources.models import Partner
from TWLight.users.signals import Notice
-from TWLight.users.models import Authorization, get_company_name
+from TWLight.users.models import Authorization, get_company_name, Editor
class Command(BaseCommand):
@@ -15,8 +17,21 @@ class Command(BaseCommand):
def handle(self, *args, **options):
# Get all authorization objects with an expiry date in the next
# four weeks, for which we haven't yet sent a reminder email, and
- # exclude users who disabled these emails.
- user_qs = User.objects.prefetch_related("userprofile")
+ # exclude users who disabled these emails and who have already filed
+ # for a renewal.
+ editor_qs = Editor.objects.select_related("user")
+ users_with_applications_for_renewal = (
+ Application.objects.prefetch_related(Prefetch("editor", queryset=editor_qs))
+ .values_list("editor__user__pk")
+ .filter(
+ ~Q(partner__authorization_method=Partner.BUNDLE),
+ status__in=[Application.PENDING, Application.QUESTION],
+ parent__isnull=False,
+ editor__isnull=False,
+ )
+ .order_by("-date_created")
+ )
+ user_qs = User.objects.select_related("userprofile")
expiring_authorizations = (
Authorization.objects.prefetch_related(Prefetch("user", queryset=user_qs))
.filter(
@@ -26,6 +41,7 @@ class Command(BaseCommand):
partners__isnull=False,
)
.exclude(user__userprofile__send_renewal_notices=False)
+ .exclude(user__pk__in=users_with_applications_for_renewal)
)
for authorization_object in expiring_authorizations:
