Esempio n. 1
0
def request_password():
    """Create a ticket and send a email with link to reset_password page."""

    if current_user.is_authenticated:
        return redirect(url_for('user.view_single', user_id=current_user.id))

    def create_hash(bits=96):
        assert bits % 8 == 0
        required_length = bits / 8 * 2
        s = hex(random.getrandbits(bits)).lstrip('0x').rstrip('L')
        if len(s) < required_length:
            return create_hash(bits)
        else:
            return s

    form = RequestPassword(request.form)

    if form.validate_on_submit():
        user = User.query.filter(
            User.email == form.email.data).first()

        if not user:
            flash(_('%(email)s is unknown to our system.',
                    email=form.email.data), 'danger')
        else:
            _hash = create_hash(256)

            ticket = PasswordTicket(user.id, _hash)
            db.session.add(ticket)
            db.session.commit()

            reset_link = url_for('user.reset_password',
                                 hash=_hash, _external=True)

            send_email(to=user.email,
                       subject='Password reset https://svia.nl',
                       email_template='email/forgot_password.html',
                       sender='via',
                       user=user,
                       reset_link=reset_link)

            flash(_('An email has been sent to %(email)s with further '
                    'instructions.', email=form.email.data), 'success')
            return redirect(url_for('home.home'))
    else:
        flash_form_errors(form)

    return render_template('user/request_password.htm', form=form)
Esempio n. 2
0
 def notify_followers(self):
     form_url = url_for('custom_form.view_single', form_id=self.form_id,
                        _external=True)
     followers = CustomFormFollower.query\
         .filter(CustomFormFollower.form_id == self.form_id)
     owner = User.query.get(self.owner_id)
     form = CustomForm.query.get(self.form_id)
     for follower in followers:
         send_email(to=follower.owner.email,
                    subject='Formulier ingevuld',
                    email_template='email/form.html',
                    email_template_kwargs=dict(
                        sender='via',
                        user=follower.owner,
                        form_url=form_url,
                        owner=owner.first_name + " " + owner.last_name,
                        form=form.name)
                    )