74 lines
2.8 KiB
Python
74 lines
2.8 KiB
Python
# Society Self-Service
|
|
# Copyright © 2018-2020 Yingtong Li (RunasSudo)
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU Affero General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
from ssmain.email import Emailer
|
|
|
|
from django.core.management.base import BaseCommand, CommandError
|
|
|
|
from django.conf import settings
|
|
from django.template import loader
|
|
from django.urls import reverse
|
|
from django.utils import timezone
|
|
|
|
from ssmembership import models
|
|
|
|
import hmac
|
|
import logging
|
|
import premailer
|
|
import time
|
|
import urllib.parse
|
|
|
|
class Command(BaseCommand):
|
|
help = 'Send emails for membership renewal'
|
|
|
|
def add_arguments(self, parser):
|
|
parser.add_argument('ids', nargs='*', type=int, help='Members with ID numbers equal to these values will be emailed (default all)')
|
|
parser.add_argument('--render', action='store_true', help='Render to stdout instead of sending emails')
|
|
|
|
def handle(self, *args, **options):
|
|
today = timezone.localtime(timezone.now()).date()
|
|
|
|
template_html = loader.get_template('ssmembership/email/renew.html')
|
|
template_txt = loader.get_template('ssmembership/email/renew.txt')
|
|
|
|
if len(options['ids']) > 0:
|
|
members = models.Member.objects.filter(id__in=options['ids'])
|
|
else:
|
|
raise Exception('Must provide IDs')
|
|
|
|
emailer = Emailer()
|
|
for member in members:
|
|
#import pdb; pdb.set_trace()
|
|
if member.member_type != 1 or member.expires < today or member.expires > today.replace(month=12, day=31):
|
|
self.stdout.write('Skipping {} at {}'.format(member.id, member.email))
|
|
continue
|
|
|
|
self.stdout.write('Emailing {} at {}'.format(member.id, member.email))
|
|
|
|
sig = hmac.new(settings.SECRET_KEY_MEMBERSIG.encode('utf-8'), member.email.encode('utf-8'), 'sha256').hexdigest()
|
|
renew_url = reverse('renew_signed') + '?' + urllib.parse.urlencode({'email': member.email, 'sig': sig})
|
|
|
|
template_args = {
|
|
'name': member.first_name.strip() + ' ' + member.last_name.strip(),
|
|
'renew_url': renew_url,
|
|
'baseurl': 'https://' + settings.ALLOWED_HOSTS[0]
|
|
}
|
|
|
|
content_html = premailer.Premailer(template_html.render(template_args), cssutils_logging_level=logging.ERROR).transform()
|
|
content_txt = template_txt.render(template_args)
|
|
|
|
emailer.send_raw_mail([member.email], '{} membership renewal'.format(settings.ORG_NAME), content_txt, content_html)
|