Exemplo n.º 1
0
    def post(self):
        have_error = False
        username = self.request.get('username')
        password = self.request.get('password')
        verify = self.request.get('verify')
        email = self.request.get('email')

        params = dict(username = username,
                      email = email)

        if not valid_username(username):
            params['error_username'] = "******"
            have_error = True

        # check if user already exists
        matched_users = User.by_name(username)
        if matched_users:
            params['error_username'] = "******"
            have_error = True

        # validate password
        if not valid_password(password):
            params['error_password'] = "******"
            have_error = True
        elif password != verify:
            params['error_verify'] = "Your passwords didn't match."
            have_error = True

        # validate email
        if not valid_email(email):
            params['error_email'] = "That's not a valid email."
            have_error = True

        # re-display signup form if having error
        if have_error:
            self.render('signup-form.html', **params)
        else:
            # add new user into database
            user = User.register(username, password, email)
            user.put()

            # new user login (update user cookie)
            self.login(user)
            
            # redirect to welcome page
            self.redirect('/blog')