def remind_team_member(membership_id, previous_todos=None, previous_blockers=None): """ Sends an individual reminder to a user. Includes TODOs and blockers if provided. """ try: membership = Membership.objects.get(id=membership_id, is_active=True) except Membership.DoesNotExist: logger.error( "Active Membership with %s ID does not exist." % membership_id) return subject = 'What did you get done today?' from_email = 'Digestus Reminder <{email}>'.format(email=membership.team.email) recipient = [ '{name} <{email}>'.format(name=membership.user.get_full_name(), email=membership.user.email) ] context = { 'team_email': membership.team.email, 'team_name': membership.team.name, 'previous_todos': previous_todos, 'previous_blockers': previous_blockers, 'domain': get_domain_name(), } text_body = render_to_string('updates/emails/reminder.txt', context) email_msg = EmailMultiAlternatives( subject=subject, body=text_body, from_email=from_email, to=recipient, ) email_msg.subaccount = membership.team.subaccount_id try: email_msg.send() except Exception as e: logger.exception('Failed to send team member reminder. Retrying in 5 minutes.') remind_team_member.retry( args=[membership_id, previous_todos, previous_blockers], exc=e, countdown=300, max_retries=5, )
def send_digest(team_id, for_date, for_project_managers=False): """ Sends digest for the given date to all active members and silent recipients of the team. Arguments: `team`: `Team` object `for_date`: A `datetime.datetime` instance in UTC `for_project_managers`: Boolean; whether to send only to Project Manager members """ # TODO: create decorator for this repeating pattern: try...except try: team = Team.objects.get(id=team_id, is_active=True) except Team.DoesNotExist: logger.exception( "Active team with %s ID does not exist." % team_id) return team_updates = team.get_updates(for_date) if team_updates: ph_tz = pytz.timezone('Asia/Manila') update_for_date = for_date.astimezone(ph_tz).strftime('%a, %b %d %Y') context = { 'members_and_updates': team.get_updates(for_date), 'team': team, 'date': update_for_date, 'domain': get_domain_name(), } text_body = render_to_string('updates/emails/digest.txt', context) html_body = render_to_string('updates/emails/digest.html', context) # Prepare email from_email = 'Digestus Digest <{email}>'.format(email=team.email) subject = 'Digest for {team} for {date}'.format(team=team.name, date=update_for_date) msg = EmailMultiAlternatives( subject=subject, body=text_body, from_email=from_email, to=team.get_recipients(for_project_managers), ) msg.auto_text = True msg.preserve_recipients = True msg.auto_text = False msg.auto_html = False msg.attach_alternative(html_body, 'text/html') msg.content_subtype = 'html' msg.subaccount = team.subaccount_id try: msg.send() except Exception as e: logger.exception( 'Digest sending failed for team with ID: %s. Retrying in 5 minutes.' % team_id) send_digest.retry( args=[team_id, for_date, for_project_managers], exc=e, countdown=300, max_retries=5, ) else: error_msg = 'Team %s has no active members. Sending of digest aborted.' % team.name logger.error(error_msg)