def handle(self, *args, **options): cfg = get_config() # Expire members (and let them know it happened) expired = Member.objects.filter(paiduntil__lt=datetime.now()) for m in expired: MemberLog(member=m, timestamp=datetime.now(), message='Membership expired').save() # Generate an email to the user send_template_mail( cfg.sender_email, m.user.email, "Your {0} membership has expired".format(settings.ORG_NAME), 'membership/mail/expired.txt', { 'member': m, }, ) self.stdout.write("Expired member {0} (paid until {1})".format( m, m.paiduntil)) # An expired member has no membersince and no paiduntil. m.membersince = None m.paiduntil = None m.save() # Send warnings to members about to expire. We explicitly avoid sending # a warning in the last 24 hours before expire, so we don't end up sending # both a warning and an expiry within minutes in case the cronjob runs on # slightly different times. warning = Member.objects.filter( Q(paiduntil__gt=datetime.now() - timedelta(days=1)) & Q(paiduntil__lt=datetime.now() + timedelta(days=30)) & (Q(expiry_warning_sent__lt=datetime.now() - timedelta(days=10)) | Q(expiry_warning_sent__isnull=True))) for m in warning: MemberLog(member=m, timestamp=datetime.now(), message='Membership expiry warning sent to %s' % m.user.email).save() # Generate an email to the user send_template_mail( cfg.sender_email, m.user.email, "Your {0} membership will expire soon".format( settings.ORG_NAME), 'membership/mail/warning.txt', { 'member': m, }, ) self.stdout.write( "Sent warning to member {0} (paid until {1}, last warned {2})". format(m, m.paiduntil, m.expiry_warning_sent)) m.expiry_warning_sent = datetime.now() m.save()
def _email_something(self, template_name, mail_subject, pdfname=None, pdfcontents=None, bcc=False, extracontext=None): # Send off the receipt/invoice by email if possible if not self.invoice.recipient_email: return # Build a text email, and attach the PDF if there is one if self.invoice.recipient_secret: # If we have the secret, include it in the email even if we have # a user. This is because users often forward that email, and # then the recipient can access it. As long as the secret is # included, both the logged in and the not logged in user # can see it. invoiceurl = '%s/invoices/%s/%s/' % (settings.SITEBASE, self.invoice.pk, self.invoice.recipient_secret) elif self.invoice.recipient_user: # General URL that shows a normal invoice invoiceurl = '%s/invoices/%s/' % (settings.SITEBASE, self.invoice.pk) else: invoiceurl = None param = { 'invoice': self.invoice, 'invoiceurl': invoiceurl, 'currency_abbrev': settings.CURRENCY_ABBREV, 'currency_symbol': settings.CURRENCY_SYMBOL, } if extracontext: param.update(extracontext) pdfdata = [] if pdfname: pdfdata = [(pdfname, 'application/pdf', base64.b64decode(pdfcontents)), ] if bcc: bcclist = [settings.INVOICE_NOTIFICATION_RECEIVER, ] else: bcclist = [] if self.invoice.extra_bcc_list: bcclist.extend([e.strip() for e in self.invoice.extra_bcc_list.split(',')]) # Queue up in the database for email sending soon send_template_mail(settings.INVOICE_SENDER_EMAIL, self.invoice.recipient_email, mail_subject, 'invoices/mail/%s' % template_name, param, pdfdata, bcclist, )
def handle(self, *args, **options): cfg = get_config() for r in MeetingReminder.objects.select_related('meeting').filter( sentat__isnull=True, sendat__lte=timezone.now()): for a in r.meeting.get_all_attendees().select_related('user'): send_template_mail( cfg.sender_email, a.user.email, "Upcoming meeting: {}".format(r.meeting.name), 'membership/mail/meeting_reminder.txt', { 'meeting': r.meeting, 'member': a, }, sendername=cfg.sender_name, receivername=a.fullname, ) r.sentat = timezone.now() r.save()