From f075216d64005e4d57b5fb2a04d68a4610503ed4 Mon Sep 17 00:00:00 2001 From: Gresille&Siffle <39056254+GresilleSiffle@users.noreply.github.com> Date: Thu, 18 Jan 2024 11:30:48 +0100 Subject: [PATCH] [back] feat: (wip) add command notify_nocontrib_users --- .../commands/notify_nocontrib_users.py | 78 +++++++++++++++++++ .../templates/core/no_contrib_email/body.html | 9 +++ .../core/no_contrib_email/subject.txt | 0 backend/settings/settings.py | 5 +- 4 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 backend/core/management/commands/notify_nocontrib_users.py create mode 100644 backend/core/templates/core/no_contrib_email/body.html create mode 100644 backend/core/templates/core/no_contrib_email/subject.txt diff --git a/backend/core/management/commands/notify_nocontrib_users.py b/backend/core/management/commands/notify_nocontrib_users.py new file mode 100644 index 0000000000..250e680415 --- /dev/null +++ b/backend/core/management/commands/notify_nocontrib_users.py @@ -0,0 +1,78 @@ +""" +Notify active users who haven't contributed since their account was created. +""" +from datetime import datetime, timedelta +from smtplib import SMTPException + +from django.core.mail import send_mail +from django.core.management.base import BaseCommand, CommandError +from django.db.models import Q, Count +from django.template.loader import render_to_string +from django.utils import timezone + +from core.models.user import User +from tournesol.models import Comparison +from settings import settings + + +class Command(BaseCommand): + help = "Notify active users who haven't contributed since their account was created." + + def handle(self, *args, **options): + self.stdout.write(f"start command: {__name__}") + + from_email = settings.REST_REGISTRATION["VERIFICATION_FROM_EMAIL"] + no_contrib_period = settings.APP_CORE["MGMT_NO_CONTRIBUTION_REMINDER_PERIOD"] + + creation_date = timezone.now() - no_contrib_period + # creation_date = datetime(2022, 8, 22) + + # display the configuration if more verbosity is asked + if options.get("verbosity", 1) > 1: + self.stdout.write(f"MGMT_NO_CONTRIBUTION_REMINDER_PERIOD: {no_contrib_period.days}") + + users = ( + User.objects.filter( + is_active=True, + date_joined__date=creation_date.date(), + ) + .annotate( + n_comparisons=Count( + "comparisons", + filter=Q(comparisons__datetime_add__date__gt=creation_date.date()), + ) + ) + .filter(n_comparisons=0) + ) + + html_message = render_to_string("core/no_contrib_email/body.html") + + fails = [] + successes = 0 + for user in users: + try: + send_mail( + subject="Subject here", + message="Here is the message.", + html_message=html_message, + from_email=from_email, + recipient_list=[user.email], + fail_silently=False, + ) + except SMTPException as ex: + fails.append((user.email, type(ex).__name__, ex)) + else: + successes += 1 + + self.stdout.write(self.style.SUCCESS(f"users notified: {successes}")) + + if len(fails): + self.stdout.write("some users have not been notified...") + + for fail in fails: + self.stdout.write(f"email: {fail[0]} // {fail[1]}: {fail[2]}") + + raise CommandError(f"Failed to notify {len(fails)} users.") + + self.stdout.write(self.style.SUCCESS("success")) + self.stdout.write("end") diff --git a/backend/core/templates/core/no_contrib_email/body.html b/backend/core/templates/core/no_contrib_email/body.html new file mode 100644 index 0000000000..9bb713f36f --- /dev/null +++ b/backend/core/templates/core/no_contrib_email/body.html @@ -0,0 +1,9 @@ +{% load i18n %} + + + +

+ {% blocktranslate %}coucou{% endblocktranslate %} +

+ + diff --git a/backend/core/templates/core/no_contrib_email/subject.txt b/backend/core/templates/core/no_contrib_email/subject.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/backend/settings/settings.py b/backend/settings/settings.py index ab9ed1b452..a84cf661df 100644 --- a/backend/settings/settings.py +++ b/backend/settings/settings.py @@ -429,7 +429,10 @@ REST_REGISTRATION.get( "REGISTER_VERIFICATION_PERIOD", datetime.timedelta(days=7) - ) + ), + # Active users who haven't contributed since their account was created will be + # notified after this number of days. + "MGMT_NO_CONTRIBUTION_REMINDER_PERIOD": datetime.timedelta(days=10) } # Configuration of the app `tournesol`