Add management command for re-sending confirmation emails to people who haven't responded
This commit is contained in:
parent
e3eeb66c1b
commit
5c30446c5a
3 changed files with 57 additions and 0 deletions
0
get_together/management/__init__.py
Normal file
0
get_together/management/__init__.py
Normal file
0
get_together/management/commands/__init__.py
Normal file
0
get_together/management/commands/__init__.py
Normal file
|
@ -0,0 +1,57 @@
|
|||
from django.core.management.base import BaseCommand, CommandError
|
||||
from django.urls import reverse
|
||||
from django.core.mail import send_mail
|
||||
from django.template.loader import get_template, render_to_string
|
||||
from django.conf import settings
|
||||
from django.contrib.sites.models import Site
|
||||
|
||||
from accounts.models import Account, EmailConfirmation
|
||||
|
||||
import time
|
||||
import urllib
|
||||
import datetime
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "Sends confirmation emails to accounts that haven't been confirmed yet"
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('-d','--days-since-last', dest='days', type=int, default=None)
|
||||
|
||||
def handle(self, *args, **options):
|
||||
site = Site.objects.get(id=1)
|
||||
accounts = Account.objects.filter(is_email_confirmed=False)
|
||||
for account in accounts:
|
||||
confirmation_request = EmailConfirmation.objects.filter(user=account.user)
|
||||
|
||||
if 'days' in options and options.get('days', None):
|
||||
discard_before = datetime.datetime.now(tz=datetime.timezone.utc) - datetime.timedelta(days=options.get('days'))
|
||||
for request in confirmation_request.filter(expires__lte=discard_before):
|
||||
request.delete()
|
||||
|
||||
if confirmation_request.count() > 0:
|
||||
print("Confirmation request pending for %s <%s>" % (account.user.username, account.user.email))
|
||||
break
|
||||
|
||||
print("Sending confirmation email to %s <%s>" % (account.user.username, account.user.email))
|
||||
confirmation_request = account.new_confirmation_request()
|
||||
confirmation_url = "https://%s%s" % (site.domain, reverse('confirm-email', kwargs={'confirmation_key':confirmation_request.key}))
|
||||
|
||||
context = {
|
||||
'confirmation': confirmation_request,
|
||||
'confirmation_url': confirmation_url,
|
||||
}
|
||||
email_subject = '[GetTogether] Email confirmation reminder'
|
||||
email_body_text = render_to_string('get_together/emails/confirm_email.txt', context)
|
||||
email_body_html = render_to_string('get_together/emails/confirm_email.html', context)
|
||||
email_recipients = [account.user.email]
|
||||
email_from = getattr(settings, 'DEFAULT_FROM_EMAIL', 'noreply@gettogether.community')
|
||||
send_mail(
|
||||
subject=email_subject,
|
||||
message=email_body_text,
|
||||
from_email=email_from,
|
||||
recipient_list=email_recipients,
|
||||
html_message=email_body_html
|
||||
)
|
||||
time.sleep(0.1)
|
||||
|
||||
|
Loading…
Reference in a new issue