def test_raises_exception_on_redefined_context(self): user = UserFactory() with self.assertRaisesMessage( Exception, 'email context should not redefine defaults: user'): prepare_email( template='accountdelete_success', user=user, context={ 'user': user, }, )
def send_new_verification_code(self): self._unverify_mail() prepare_email( 'send_new_verification_code', self, { 'url': '{hostname}/#/verify-mail?key={code}'.format( hostname=settings.HOSTNAME, code=VerificationCode.objects.get( user=self, type=VerificationCode.EMAIL_VERIFICATION).code) }, to=self.unverified_email).send()
def prepare_application_accepted_email(application): return prepare_email(template='application_accepted', user=application.user, context={ 'group': application.group, 'group_url': group_wall_url(application.group) })
def prepare_signup_email(user, verification_code): return prepare_email( template='signup', user=user, context={'url': user_emailverification_url(verification_code.code)}, to=user.unverified_email, )
def prepare_private_user_conversation_message_notification(user, messages): first_message = messages[0] conversation = first_message.conversation author = first_message.author reply_to_name = author.display_name local_part = make_local_part(conversation, user) reply_to = formataddr( (reply_to_name, '{}@{}'.format(local_part, settings.SPARKPOST_RELAY_DOMAIN))) from_email = formataddr((author.display_name, settings.DEFAULT_FROM_EMAIL)) return prepare_email(template='conversation_message_notification', from_email=from_email, user=user, reply_to=[reply_to], context={ 'messages': messages, 'conversation_name': author.display_name, 'conversation_url': user_detail_url(author), 'mute_url': user_conversation_mute_url(author, conversation), })
def prepare_group_thread_message_notification(user, messages): first_message = messages[0] conversation = first_message.conversation thread = first_message.thread thread_text_beginning = Truncator(thread.content).chars(num=60) from_text = author_names(messages) reply_to_name = thread.author.display_name conversation_name = thread_text_beginning local_part = make_local_part(conversation, user, thread) reply_to = formataddr( (reply_to_name, '{}@{}'.format(local_part, settings.SPARKPOST_RELAY_DOMAIN))) from_email = formataddr((from_text, settings.DEFAULT_FROM_EMAIL)) return prepare_email(template='thread_message_notification', from_email=from_email, user=user, reply_to=[reply_to], context={ 'messages': messages, 'conversation_name': conversation_name, 'thread_author': thread.author, 'thread_message_content': thread.content_rendered(truncate_words=40), 'thread_url': thread_url(thread), 'mute_url': thread_mute_url(thread), })
def prepare_group_conversation_message_notification(user, messages): first_message = messages[0] conversation = first_message.conversation group = conversation.target from_text = author_names(messages) reply_to_name = group.name conversation_name = group.name local_part = make_local_part(conversation, user) reply_to = formataddr( (reply_to_name, '{}@{}'.format(local_part, settings.SPARKPOST_RELAY_DOMAIN))) from_email = formataddr((from_text, settings.DEFAULT_FROM_EMAIL)) return prepare_email(template='conversation_message_notification', from_email=from_email, user=user, reply_to=[reply_to], context={ 'messages': messages, 'conversation_name': conversation_name, 'conversation_url': group_wall_url(group), 'mute_url': group_conversation_mute_url(group, conversation), })
def prepare_changemail_request_email(user, verification_code): return prepare_email( 'changemail_request', user, { 'url': user_emailverification_url(verification_code.code), }, to=user.unverified_email)
def prepare_group_conversation_message_notification(user, message): group = message.conversation.target reply_to_name = group.name conversation_name = group.name local_part = make_local_part(message.conversation, user) reply_to = formataddr( (reply_to_name, '{}@{}'.format(local_part, settings.SPARKPOST_RELAY_DOMAIN))) from_email = formataddr( (message.author.display_name, settings.DEFAULT_FROM_EMAIL)) return prepare_email('conversation_message_notification', from_email=from_email, user=user, reply_to=[reply_to], context={ 'conversation_name': conversation_name, 'author': message.author, 'message_content': message.content_rendered(), 'conversation_url': group_wall_url(group), 'mute_url': group_conversation_mute_url( group, message.conversation), })
def prepare_emailinvitation_email(invitation): return prepare_email('emailinvitation', None, { 'group_name': invitation.group.name, 'invite_url': invite_url(invitation), 'email': invitation.email, 'invited_by_name': invitation.invited_by.display_name, }, to=invitation.email)
def prepare_application_declined_email(application): return prepare_email( template='application_declined', user=application.user, context={ 'group': application.group, }, )
def prepare_pickup_conversation_message_notification(user, messages): first_message = messages[0] conversation = first_message.conversation pickup = conversation.target group_tz = pickup.store.group.timezone language = user.language if not translation.check_for_language(language): language = 'en' with translation.override(language): with timezone.override(group_tz): weekday = format_date( pickup.date.astimezone(timezone.get_current_timezone()), 'EEEE', locale=translation.to_locale(language), ) time = format_time( pickup.date, format='short', locale=translation.to_locale(language), tzinfo=timezone.get_current_timezone(), ) date = format_date( pickup.date.astimezone(timezone.get_current_timezone()), format='long', locale=translation.to_locale(language), ) long_date = '{} {}, {}'.format(weekday, time, date) short_date = '{} {}'.format(weekday, time) reply_to_name = _('Pickup %(date)s') % { 'date': short_date, } conversation_name = _('Pickup %(date)s') % { 'date': long_date, } from_text = author_names(messages) local_part = make_local_part(conversation, user) reply_to = formataddr((reply_to_name, '{}@{}'.format(local_part, settings.SPARKPOST_RELAY_DOMAIN))) from_email = formataddr((from_text, settings.DEFAULT_FROM_EMAIL)) return prepare_email( template='conversation_message_notification', from_email=from_email, user=user, reply_to=[reply_to], context={ 'messages': messages, 'conversation_name': conversation_name, 'conversation_url': pickup_detail_url(pickup), 'mute_url': pickup_conversation_mute_url(pickup, conversation), } )
def prepare_user_became_editor_email(user, group): return prepare_email( template='user_became_editor', user=user, context={ 'group_name': group.name, 'group_url': group_wall_url(group), }, )
def prepare_changemail_request_email(user, verification_code): return prepare_email( template='changemail_request', user=user, context={ 'url': user_emailverification_url(verification_code.code), }, to=user.unverified_email, )
def prepare_user_inactive_in_group_email(user, group): return prepare_email( template='user_inactive_in_group', user=user, context={ 'group_name': group.name, 'group_url': group_wall_url(group), 'num_days_inactive': settings.NUMBER_OF_DAYS_UNTIL_INACTIVE_IN_GROUP, }, )
def prepare_emailinvitation_email(invitation): return prepare_email( template='emailinvitation', user=None, context={ 'group_name': invitation.group.name, 'invite_url': invite_url(invitation), 'email': invitation.email, 'invited_by_name': invitation.invited_by.display_name, }, to=invitation.email, )
def prepare_user_inactive_in_group_email(user, group): group_url = '{hostname}/#/group/{group_id}/'.format( hostname=settings.HOSTNAME, group_id=group.id) return prepare_email('user_inactive_in_group', user=user, context={ 'group_name': group.name, 'group_url': group_url, 'num_days_inactive': settings.NUMBER_OF_DAYS_UNTIL_INACTIVE_IN_GROUP, })
def prepare_pickup_notification_email( user, group, tonight_date, tomorrow_date, tonight_user=None, tonight_empty=None, tonight_not_full=None, tomorrow_user=None, tomorrow_empty=None, tomorrow_not_full=None, ): has_pickups_tonight = any([ items is not None and len(items) > 0 for items in [ tonight_user, tonight_empty, tonight_not_full, ] ]) has_pickups_tomorrow = any([ items is not None and len(items) > 0 for items in [ tomorrow_user, tomorrow_empty, tomorrow_not_full, ] ]) settings_url = '{hostname}/#/group/{group_id}/settings'.format( hostname=settings.HOSTNAME, group_id=group.id, ) return prepare_email(template='pickup_notification', user=user, context={ 'settings_url': settings_url, 'group': group, 'tonight_date': tonight_date, 'tomorrow_date': tomorrow_date, 'has_pickups_tonight': has_pickups_tonight, 'has_pickups_tomorrow': has_pickups_tomorrow, 'tonight_user': tonight_user, 'tonight_empty': tonight_empty, 'tonight_not_full': tonight_not_full, 'tomorrow_user': tomorrow_user, 'tomorrow_empty': tomorrow_empty, 'tomorrow_not_full': tomorrow_not_full, })
def prepare_group_application_message_notification(user, messages): first_message = messages[0] conversation = first_message.conversation application = conversation.target language = user.language if not translation.check_for_language(language): language = 'en' with translation.override(language): reply_to_name = application.user.display_name conversation_name = _( 'New message in application of %(user_name)s to %(group_name)s' ) % { 'user_name': application.user.display_name, 'group_name': application.group.name, } if application.user == user: conversation_name = _( 'New message in your application to %(group_name)s') % { 'group_name': application.group.name } from_text = author_names(messages) local_part = make_local_part(conversation, user) reply_to = formataddr( (reply_to_name, '{}@{}'.format(local_part, settings.SPARKPOST_RELAY_DOMAIN))) from_email = formataddr((from_text, settings.DEFAULT_FROM_EMAIL)) return prepare_email(template='conversation_message_notification', from_email=from_email, user=user, reply_to=[reply_to], context={ 'messages': messages, 'conversation_name': conversation_name, 'conversation_url': group_application_url(application), 'mute_url': group_application_mute_url( application, conversation), })
def prepare_group_summary_emails(group, context): """Prepares one email per language""" members = group.members.filter( groupmembership__in=GroupMembership.objects.active() .with_notification_type(GroupNotificationType.WEEKLY_SUMMARY) ).exclude(groupmembership__user__in=get_user_model().objects.unverified_or_ignored()) grouped_members = itertools.groupby(members.order_by('language'), key=lambda member: member.language) return [ prepare_email( template='group_summary', context=context, to=[member.email for member in members], language=language, ) for (language, members) in grouped_members ]
def prepare_pickup_notification_email( user, group, tonight_date, tomorrow_date, tonight_user=None, tonight_empty=None, tonight_not_full=None, tomorrow_user=None, tomorrow_empty=None, tomorrow_not_full=None, ): has_pickups_tonight = any([ items is not None and len(items) > 0 for items in [ tonight_user, tonight_empty, tonight_not_full, ] ]) has_pickups_tomorrow = any([ items is not None and len(items) > 0 for items in [ tomorrow_user, tomorrow_empty, tomorrow_not_full, ] ]) return prepare_email( template='pickup_notification', user=user, context={ 'settings_url': group_settings_url(group), 'group': group, 'tonight_date': tonight_date, 'tomorrow_date': tomorrow_date, 'has_pickups_tonight': has_pickups_tonight, 'has_pickups_tomorrow': has_pickups_tomorrow, 'tonight_user': tonight_user, 'tonight_empty': tonight_empty, 'tonight_not_full': tonight_not_full, 'tomorrow_user': tomorrow_user, 'tomorrow_empty': tomorrow_empty, 'tomorrow_not_full': tomorrow_not_full, }, )
def prepare_group_application_message_notification(user, message): application = message.conversation.target language = user.language if not translation.check_for_language(language): language = 'en' with translation.override(language): reply_to_name = application.user.display_name conversation_name = _( 'New message in %(user_name)s\'s application') % { 'user_name': application.user.display_name, } local_part = make_local_part(message.conversation, user) reply_to = formataddr( (reply_to_name, '{}@{}'.format(local_part, settings.SPARKPOST_RELAY_DOMAIN))) from_email = formataddr( (message.author.display_name, settings.DEFAULT_FROM_EMAIL)) return prepare_email('conversation_message_notification', from_email=from_email, user=user, reply_to=[reply_to], context={ 'conversation_name': conversation_name, 'author': message.author, 'message_content': message.content_rendered(), 'conversation_url': group_application_url(application), 'mute_url': group_application_mute_url( application, message.conversation), })
def prepare_new_application_notification_email(user, application): applicant = application.user conversation = Conversation.objects.get_for_target(application) reply_to_name = applicant.display_name local_part = make_local_part(conversation, user) reply_to = formataddr( (reply_to_name, '{}@{}'.format(local_part, settings.SPARKPOST_RELAY_DOMAIN))) from_email = formataddr( (applicant.display_name, settings.DEFAULT_FROM_EMAIL)) return prepare_email(template='new_application', from_email=from_email, user=user, reply_to=[reply_to], context={ 'applicant': applicant, 'group': application.group, 'questions': application.questions_rendered(), 'answers': application.answers_rendered(), 'conversation_url': group_application_url(application), 'mute_url': group_application_mute_url( application, conversation), 'settings_url': group_settings_url(application.group), 'group_applications_url': group_applications_url(application.group), 'group_edit_url': group_edit_url(application.group), })
def prepare_conversation_message_notification(user, message): if not isinstance(message.conversation.target, Group): raise Exception( 'Cannot send message notification if conversation does not belong to a group' ) group = message.conversation.target reply_to_name = group.name conversation_name = group.name local_part = make_local_part(message.conversation, user) reply_to = formataddr( (reply_to_name, '{}@{}'.format(local_part, settings.SPARKPOST_RELAY_DOMAIN))) from_email = formataddr( (message.author.display_name, settings.DEFAULT_FROM_EMAIL)) with translation.override(user.language): return prepare_email('conversation_message_notification', from_email=from_email, user=user, reply_to=[reply_to], context={ 'conversation_name': conversation_name, 'author': message.author, 'message_content': message.content_rendered(), 'conversation_url': group_wall_url(group), 'mute_url': conversation_mute_url(group, message.conversation) })
def prepare_changemail_success_email(user): return prepare_email( template='changemail_success', user=user, )
def prepare_passwordreset_request_email(user, verification_code): return prepare_email( template='passwordreset_request', user=user, context={'url': user_passwordreset_url(verification_code.code)}, )
def prepare_accountdelete_request_email(user, verification_code): return prepare_email( template='accountdelete_request', user=user, context={'url': user_delete_url(verification_code.code)}, )
def prepare_passwordreset_success_email(user): return prepare_email( template='passwordreset_success', user=user, )
def reset_password(self): new_password = User.objects.make_random_password(length=20) self.set_password(new_password) self.save() prepare_email('newpassword', self, {'password': new_password}).send()
def _send_mail_change_notification(self): prepare_email('changemail_notice', self, {}).send()