Exemplo n.º 1
0
def register():
    form = RegistrationForm(request.form)

    if not app.config['RECAPTCHA_ENABLED']:
        del form.recaptcha

    if request.method == 'POST' and form.validate():
        #Exract data from the form
        full_name = form.full_name.data
        email     = form.email.data
        password  = form.password.data
        
        #create user
        user = User(email, hashlib.sha1(password).hexdigest())
        user.full_name = full_name
        user.active = False
        
        #add to database
        token = Token(str(uuid.uuid4()), datetime.datetime.now())
        user.token = token
        db.session.add(user)
        db.session.commit()
        
        #create email
        from_email = '*****@*****.**'

        link = url_for('confirm', email=email, token=token.token, _external = True)
        body_html = """ <html>
                            <head></head>
                            <body>
                              <p>Welcome!<p>
                              <p>This is the wcloud system, which creates new WebLab-Deusto instances.
                              Your account is ready, and you can activate it:</p>
                              <ul>
                                <li><a href="%(link)s">%(link)s</a>.</li>
                              </ul>
                              <p>If you didn't register, feel free to ignore this e-mail.</p>
                              <p>Best regards,</p>
                              <p>WebLab-Deusto team</p>
                            </body>
                          </html>""" % dict(link=link)
        print(body_html)
        body = """Welcome to wcloud. Click %s to confirm your registration.""" % link
        subject = 'wCloud registration'
        
        # Send email
        try:
            utils.send_email(app, body, subject, from_email, user.email, body_html)
            flash("Mail sent to %s from %s with subject '%s'. Check your SPAM folder if you don't receive it." % (user.email, from_email, subject), 'success') 
        except:
            db.session.delete(token)
            db.session.delete(user)
            db.session.commit()
            flash("There was an error sending the e-mail. This might be because of a invalid e-mail address. Please re-check it.", "error")
            return render_template('register.html', form=form)

        flash("""Thanks for registering. You have an
              email with the steps to confirm your account""", 'success')
        return redirect(url_for('login'))
    
    return render_template('register.html', form=form)