def send_change_password_email( request, profile, email=None, subject=None, text_body=None, html_body=None, discussion=None, sender_name=None, welcome=False): mailer = get_mailer(request) localizer = request.localizer data = dict( assembl="Assembl", name=profile.name, confirm_url=maybe_contextual_route( request, 'welcome' if welcome else 'do_password_change', ticket=password_change_token(profile))) sender_email = config.get('assembl.admin_email') if discussion: data.update(dict( discussion_topic=discussion.topic, discussion_url=discussion.get_url())) sender_name = sender_name or discussion.topic if sender_name: sender_name = UnicodeDammit(sender_name).unicode_markup # sanitize sender_name = re.sub( ur"[^-\w\s'\u2019\u2032\u00b4\.\(\)]", '', sender_name, 0, re.UNICODE) sender = '"%s" <%s>' % (sender_name, sender_email) sender_name = Header(sender_name, 'utf-8').encode() if len(sender) > 255: sender = sender_email else: sender = sender_email subject = (subject or localizer.translate( _("Request for password change"))).format(**data) #subject = Header(subject, 'utf-8').encode() # Fails in some cases??? if text_body is None or html_body is not None: # if text_body and no html_body, html_body remains None. html_body = html_body or localizer.translate(_(u"""<p>Hello, {name}!</p> <p>We have received a request to change the password on your {assembl} account. Please <a href="{confirm_url}">click here to confirm your password change</a>.</p> <p>If you did not ask to reset your password please disregard this email.</p> <p>Best regards,<br />The {assembl} Team</p> """)) text_body = text_body or localizer.translate(_(u"""Hello, {name}! We have received a request to change the password on your {assembl} account. To confirm your password change please click on the link below. <{confirm_url}> If you did not ask to reset your password please disregard this email. Best regards, The {assembl} Team """)) message = Message( subject=subject, sender=sender, recipients=["%s <%s>" % ( profile.name, email or profile.get_preferred_email())], body=text_body.format(**data), html=html_body.format(**data)) mailer.send(message)
def send_change_password_email( request, profile, email=None, subject=None, text_body=None, html_body=None, discussion=None, sender_name=None, welcome=False, immediate=False): mailer = get_mailer(request) localizer = request.localizer route_maker = create_get_route(request, discussion) data = dict( assembl="Assembl", name=profile.name, confirm_url=get_global_base_url() + route_maker( 'welcome' if welcome else 'do_password_change', token=password_change_token(profile))) sender_email = config.get('assembl.admin_email') if discussion: data.update(dict( discussion_topic=discussion.topic, discussion_url=discussion.get_url())) sender_name = sender_name or discussion.topic if sender_name: sender_name = normalize_email_name(sender_name) sender = '"%s" <%s>' % (sender_name, sender_email) sender_name = Header(sender_name, 'utf-8').encode() if len(sender) > 255: sender = sender_email else: sender = sender_email subject = (subject or localizer.translate( _("Request for password change"))).format(**data) #subject = Header(subject, 'utf-8').encode() # Fails in some cases??? if text_body is None or html_body is not None: # if text_body and no html_body, html_body remains None. html_body = html_body or localizer.translate(_(u"""<p>Hello, {name}!</p> <p>We have received a request to change the password on your {assembl} account. Please <a href="{confirm_url}">click here to confirm your password change</a>.</p> <p>If you did not ask to reset your password please disregard this email.</p> <p>Best regards,<br />The {assembl} Team</p> """)) text_body = text_body or localizer.translate(_(u"""Hello, {name}! We have received a request to change the password on your {assembl} account. To confirm your password change please click on the link below. <{confirm_url}> If you did not ask to reset your password please disregard this email. Best regards, The {assembl} Team """)) message = Message( subject=subject, sender=sender, recipients=["%s <%s>" % ( profile.name, email or profile.get_preferred_email())], body=text_body.format(**data), html=html_body.format(**data)) if immediate: mailer.send_immediately(message) else: mailer.send(message)
def setup_change_password(user, password): old_password = user.password token = password_change_token(user) password_change_payload = {"token": token, "password1": password, "password2": password} user, validity = verify_password_change_token(token) assert validity == Validity.VALID return old_password, password_change_payload
def discussionCreated(self, discussion): from assembl.models import Notification from ..lib.frontend_urls import FrontendUrls from premailer import Premailer profile = discussion.creator assert profile jinja_env = Notification.make_jinja_env() (assembl_css, ink_css) = Notification.get_css_paths(discussion) request = get_current_request() confirm_url = request.route_url( 'contextual_welcome', discussion_slug=discussion.slug, ticket=password_change_token(profile)) template_data = { 'discussion': discussion, 'frontendUrls': FrontendUrls(discussion), 'ink_css': ink_css.read(), 'assembl_notification_css': assembl_css.read().decode('utf_8'), 'jinja_env': jinja_env, 'connection_url': confirm_url, 'documentation_url': config.get('documentation_url'), 'admin_email': config.get('admin_email'), 'admin_name': config.get('admin_name'), 'admin_org_name': config.get('admin_org_name'), 'admin_org_url': config.get('admin_org_url'), } html_template = jinja_env.get_template( 'notifications/html_new_discussion.jinja2') html_body = html_template.render(**template_data) html_body = Premailer(html_body, disable_leftover_css=True).transform() text_template = jinja_env.get_template( 'notifications/txt_new_discussion.jinja2') text_body = text_template.render(**template_data) sender_email = config.get('assembl.admin_email') mailer = get_mailer(request) localizer = request.localizer sender_name = discussion.topic sender_name = normalize_email_name(sender_name) sender = '"%s" <%s>' % (sender_name, sender_email) sender_name = Header(sender_name, 'utf-8').encode() if len(sender) > 255: sender = sender_email subject = localizer.translate(_("Your consultation was created")) message = Message( subject=subject, sender=sender, recipients=["%s <%s>" % ( profile.name, profile.get_preferred_email())], cc=aslist(config.get("discussion_creation_cc", "")), body=text_body, html=html_body) mailer.send(message)
def discussionCreated(self, discussion): from assembl.models import Notification from ..lib.frontend_urls import FrontendUrls from premailer import Premailer profile = discussion.creator assert profile jinja_env = Notification.make_jinja_env() (assembl_css, ink_css) = Notification.get_css_paths(discussion) request = get_current_request() confirm_url = request.route_url('contextual_welcome', discussion_slug=discussion.slug, ticket=password_change_token(profile)) template_data = { 'discussion': discussion, 'frontendUrls': FrontendUrls(discussion), 'ink_css': ink_css.read(), 'assembl_notification_css': assembl_css.read().decode('utf_8'), 'jinja_env': jinja_env, 'connection_url': confirm_url, 'documentation_url': config.get('documentation_url'), 'admin_email': config.get('admin_email'), 'admin_name': config.get('admin_name'), 'admin_org_name': config.get('admin_org_name'), 'admin_org_url': config.get('admin_org_url'), } html_template = jinja_env.get_template( 'notifications/html_new_discussion.jinja2') html_body = html_template.render(**template_data) html_body = Premailer(html_body, disable_leftover_css=True).transform() text_template = jinja_env.get_template( 'notifications/txt_new_discussion.jinja2') text_body = text_template.render(**template_data) sender_email = config.get('assembl.admin_email') mailer = get_mailer(request) localizer = request.localizer sender_name = discussion.topic sender_name = normalize_email_name(sender_name) sender = '"%s" <%s>' % (sender_name, sender_email) sender_name = Header(sender_name, 'utf-8').encode() if len(sender) > 255: sender = sender_email subject = localizer.translate(_("Your consultation was created")) message = Message(subject=subject, sender=sender, recipients=[ "%s <%s>" % (profile.name, profile.get_preferred_email()) ], cc=aslist(config.get("discussion_creation_cc", "")), body=text_body, html=html_body) mailer.send(message)
def test_change_password_token(test_app, participant1_user): # Set up old_password = participant1_user.password token = password_change_token(participant1_user) my_json = {"token": token, "password1": "lolo", "password2": "lolo"} # Test token user, validity = verify_password_change_token(token) assert validity == Validity.VALID # Test API response = test_app.post_json('/data/AgentProfile/do_password_change', my_json) assert response.status_code == 200 assert old_password != participant1_user.password assert participant1_user.check_password("lolo") == True