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 prepare_accountdelete_request_email(user, verification_code): return prepare_email( template='accountdelete_request', user=user, transactional=True, context={'url': user_delete_url(verification_code.code)}, )
def prepare_passwordreset_request_email(user, verification_code): return prepare_email( template='passwordreset_request', user=user, context={'url': user_passwordreset_url(verification_code.code)}, transactional=True, )
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)) unsubscribe_url = conversation_unsubscribe_url(author, conversation=conversation) return prepare_email( template='conversation_message_notification', from_email=from_email, user=user, # TODO: which timezone? maybe the user needs a timezone? reply_to=[reply_to], unsubscribe_url=unsubscribe_url, context={ 'messages': messages, 'conversation_name': author.display_name, 'conversation_url': user_detail_url(author), 'mute_url': unsubscribe_url, }, stats_category='private_conversation_message', )
def prepare_group_conversation_message_notification(user, message): conversation = message.conversation group = conversation.target from_text = message.author.display_name reply_to_name = group.name conversation_name = group.name local_part = make_local_part(conversation, user, message) reply_to = formataddr( (reply_to_name, '{}@{}'.format(local_part, settings.SPARKPOST_RELAY_DOMAIN))) from_email = formataddr((from_text, settings.DEFAULT_FROM_EMAIL)) unsubscribe_url = conversation_unsubscribe_url(user, group=group, conversation=conversation) return prepare_email( template='conversation_message_notification', from_email=from_email, user=user, tz=group.timezone, reply_to=[reply_to], unsubscribe_url=unsubscribe_url, context={ 'messages': [message], 'conversation_name': conversation_name, 'conversation_url': group_wall_url(group), 'mute_url': unsubscribe_url, }, stats_category='group_conversation_message', )
def prepare_accountdelete_request_email(user, verification_code): return prepare_email( template='accountdelete_request', user=user, context={'url': user_delete_url(verification_code.code)}, stats_category='accountdelete_request', )
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.EMAIL_REPLY_DOMAIN))) from_email = formataddr( (applicant.display_name, settings.DEFAULT_FROM_EMAIL)) unsubscribe_url = new_application_unsubscribe_url(user, application) return prepare_email( template='new_application', from_email=from_email, user=user, tz=application.group.timezone, reply_to=[reply_to], unsubscribe_url=unsubscribe_url, context={ 'applicant': applicant, 'group': application.group, 'questions': application.questions_rendered(), 'answers': application.answers_rendered(), 'conversation_url': application_url(application), 'mute_url': unsubscribe_url, 'new_application_unsubscribe_url': unsubscribe_url, 'applications_url': applications_url(application.group), 'group_edit_url': group_edit_url(application.group), }, stats_category='new_application', )
def prepare_passwordreset_request_email(user, verification_code): return prepare_email( template='passwordreset_request', user=user, context={'url': user_passwordreset_url(verification_code.code)}, stats_category='passwordreset_request', )
def prepare_thread_message_notification(user, messages): first_message = messages[0] conversation = first_message.conversation group = conversation.find_group() 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)) unsubscribe_url = thread_unsubscribe_url(user, group, thread) return prepare_email( template='thread_message_notification', from_email=from_email, user=user, tz=group.timezone, reply_to=[reply_to], unsubscribe_url=unsubscribe_url, 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': unsubscribe_url, }, stats_category='thread_message', )
def prepare_incoming_email_rejected_email(user, content): return prepare_email( template='incoming_email_rejected', user=user, context={'content': content}, stats_category='incoming_email_rejected', )
def prepare_new_conflict_resolution_email_to_affected_user(issue): created_by = issue.created_by conversation = Conversation.objects.get_for_target(issue) voting = issue.latest_voting() user = issue.affected_user reply_to_name = created_by.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( (created_by.display_name, settings.DEFAULT_FROM_EMAIL)) this_issue_url = issue_url(issue) return prepare_email( template='new_conflict_resolution_affected_user', from_email=from_email, user=user, tz=issue.group.timezone, reply_to=[reply_to], context={ 'created_by': created_by, 'topic': issue.topic_rendered(), 'conversation_url': this_issue_url, 'issue_url': this_issue_url, 'expires_at': voting.expires_at, 'group': issue.group, }, stats_category='new_conflict_resolution', )
def prepare_new_offer_notification_email(user, offer): conversation = Conversation.objects.get_for_target(offer) reply_to_name = offer.user.display_name local_part = make_local_part(conversation, user) reply_to = formataddr( (reply_to_name, '{}@{}'.format(local_part, settings.EMAIL_REPLY_DOMAIN))) from_email = formataddr( (offer.user.display_name, settings.DEFAULT_FROM_EMAIL)) unsubscribe_url = new_offer_unsubscribe_url(user, offer) return prepare_email( template='new_offer', from_email=from_email, user=user, tz=offer.group.timezone, reply_to=[reply_to], unsubscribe_url=unsubscribe_url, context={ 'user_name': offer.user.display_name, 'offer_photo': offer_image_url(offer), 'offer_name': offer.name, 'offer_description': offer.description, 'group': offer.group, 'offer_url': offer_url(offer), 'mute_url': unsubscribe_url, 'new_offer_unsubscribe_url': unsubscribe_url, }, stats_category='new_offer', )
def prepare_incoming_email_rejected_email(user, content): return prepare_email( template='incoming_email_rejected', user=user, transactional=True, context={'content': content}, )
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, stats_category='signup', )
def prepare_application_accepted_email(application): return prepare_email(template='application_accepted', user=application.user, tz=application.group.timezone, 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, transactional=True, )
def prepare_application_declined_email(application): return prepare_email( template='application_declined', user=application.user, tz=application.group.timezone, context={ 'group': application.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, stats_category='changemail_request', )
def prepare_user_became_editor_email(user, group): return prepare_email( template='user_became_editor', user=user, tz=group.timezone, 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, transactional=True, )
def prepare_message_notification( user, messages, *, conversation_name, conversation_url, stats_category, group=None, reply_to_name=None, ): first_message = messages[0] conversation = first_message.conversation author = first_message.author if group: tz = group.timezone elif user.current_group: tz = user.current_group.timezone else: # default, I guess most groups are not so far from this timezone... tz = pytz.timezone('Europe/Berlin') if reply_to_name is None: reply_to_name = author.display_name with translation.override(language_for_user(user)): from_text = author_names(messages) # If the conversation supports threads, replies should go into a thread, not the main conversation thread = first_message if conversation.target and conversation.target.conversation_supports_threads else None local_part = make_local_part(conversation, user, thread) reply_to = formataddr( (reply_to_name, '{}@{}'.format(local_part, settings.EMAIL_REPLY_DOMAIN))) from_email = formataddr((from_text, settings.DEFAULT_FROM_EMAIL)) unsubscribe_url = conversation_unsubscribe_url( user, group=group, conversation=conversation) return prepare_email( template='conversation_message_notification', from_email=from_email, user=user, tz=tz, reply_to=[reply_to], unsubscribe_url=unsubscribe_url, context={ 'messages': messages, 'conversation_name': conversation_name, 'conversation_url': conversation_url, 'mute_url': unsubscribe_url, }, stats_category=stats_category, )
def prepare_user_inactive_in_group_email(user, group): return prepare_email( template='user_inactive_in_group', user=user, tz=group.timezone, context={ 'group_name': group.name, 'group_url': group_wall_url(group), 'num_days_inactive': settings.NUMBER_OF_DAYS_UNTIL_INACTIVE_IN_GROUP, }, )
def prepare_user_removal_from_group_email(user, group): return prepare_email( template='user_removal_from_group', user=user, tz=group.timezone, context={ 'group_name': group.name, 'group_url': group_wall_url(group), 'num_months_inactive': settings.NUMBER_OF_INACTIVE_MONTHS_UNTIL_REMOVAL_FROM_GROUP_NOTIFICATION, 'num_removal_days': settings.NUMBER_OF_DAYS_AFTER_REMOVAL_NOTIFICATION_WE_ACTUALLY_REMOVE_THEM, }, )
def prepare_user_lost_editor_role_email(user, group): return prepare_email( template='user_lost_editor_role', user=user, tz=group.timezone, context={ 'group_name': group.name, 'group_url': group_wall_url(group), 'group': group, }, stats_category='user_lost_editor_role', )
def prepare_emailinvitation_email(invitation): return prepare_email( template='emailinvitation', user=None, tz=invitation.group.timezone, 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_conflict_resolution_continued_email_to_affected_user(issue): voting = issue.latest_voting() user = issue.affected_user this_issue_url = issue_url(issue) return prepare_email( template='conflict_resolution_continued_affected_user', user=user, tz=issue.group.timezone, context={ 'issue_url': this_issue_url, 'expires_at': voting.expires_at, })
def prepare_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)) unsubscribe_url = conversation_unsubscribe_url( user, group=application.group, conversation=conversation) return prepare_email( template='conversation_message_notification', from_email=from_email, user=user, tz=application.group.timezone, reply_to=[reply_to], unsubscribe_url=unsubscribe_url, context={ 'messages': messages, 'conversation_name': conversation_name, 'conversation_url': application_url(application), 'mute_url': unsubscribe_url, }, stats_category='application_message', )
def prepare_activity_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_activities_tonight = any([ items is not None and len(items) > 0 for items in [ tonight_user, tonight_empty, tonight_not_full, ] ]) has_activities_tomorrow = any([ items is not None and len(items) > 0 for items in [ tomorrow_user, tomorrow_empty, tomorrow_not_full, ] ]) unsubscribe_url = activity_notification_unsubscribe_url(user, group) return prepare_email( template='activity_notification', user=user, tz=group.timezone, context={ 'unsubscribe_url': unsubscribe_url, 'group': group, 'tonight_date': tonight_date, 'tomorrow_date': tomorrow_date, 'has_activities_tonight': has_activities_tonight, 'has_activities_tomorrow': has_activities_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, }, stats_category='activity_notification', )
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, ] ]) unsubscribe_url = weekly_summary_unsubscribe_url(user, group) return prepare_email( template='pickup_notification', user=user, tz=group.timezone, context={ 'unsubscribe_url': unsubscribe_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_conflict_resolution_continued_email(user, issue): affected_user = issue.affected_user voting = issue.latest_voting() unsubscribe_url = conflict_resolution_unsubscribe_url(user, issue) this_issue_url = issue_url(issue) return prepare_email(template='conflict_resolution_continued', user=user, tz=issue.group.timezone, context={ 'affected_user': affected_user, 'unsubscribe_url': unsubscribe_url, 'issue_url': this_issue_url, 'expires_at': voting.expires_at, })