-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[back] feat: (wip) add command notify_nocontrib_users
- Loading branch information
1 parent
f659203
commit f075216
Showing
4 changed files
with
91 additions
and
1 deletion.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
backend/core/management/commands/notify_nocontrib_users.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{% load i18n %} | ||
<!DOCTYPE html> | ||
<html> | ||
<body> | ||
<p> | ||
{% blocktranslate %}coucou{% endblocktranslate %} | ||
</p> | ||
</body> | ||
</html> |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters