返回顶部

收藏

验证用户注册信息

更多

signup.py

import webapp2
import re

form = """
<form method="post">
    <b>Signup</b> <br><br>
    <label> Username
      <input name="username" value=%(username)s><div style="color:red">%(error_username)s</div>
    </label>
    <label> Password
      <input type="password" name="password" value=%(password)s><div style="color:red">%(error_password)s</div>
    </label>
    <label> Verify password
      <input type="password" name="verify" value=%(verify)s><div style="color:red">%(error_verify)s</div>
    </label>
    <label> Email(optional)
      <input name="email" value=%(email)s><div style="color:red">%(error_email)s</div>
    </label> <br>
    <input type="submit">
</form>
"""

class SignUp(webapp2.RequestHandler):
    def valid_username(self, username):
        username_pattern = re.compile('^[a-zA-Z0-9-_]{3,20}$')
        if re.match(username_pattern, username):
            return ''
        else:
            return 'Wrong username! No space in it and between 3 to 20 characters.'

    def valid_password(self, password, verify):
        password_pattern = re.compile('^.{3,20}$')
        if re.match(password_pattern, password):
            error_password = ''
        else:
            error_password = 'Password should be between 3 to 20 characters long.'
        if cmp(password, verify) == 0:
            error_verify = ''
        else:
            error_verify = 'Password not match!'
        return (error_password, error_verify)

    def valid_email(self, email):
        email_pattern = re.compile('^[\S]+@[\S]+\.[\S]+$|^$')
        # Email is optional.
        if re.match(email_pattern, email):
            return ''
        else:
            return 'Wrong email address!'

    def write_form(self, username='', error_username='', password='', error_password='', verify='', error_verify='', email='', error_email=''):
        self.response.out.write(form % {'username': username,
                                        'error_username': error_username,
                                        'password': password,
                                        'error_password': error_password,
                                        'verify': verify,
                                        'error_verify': error_verify,
                                        'email': email,
                                        'error_email': error_email})

    def get(self):
        self.write_form()

    def post(self):
        user_username = self.request.get('username')
        user_password = self.request.get('password')
        user_verify = self.request.get('verify')
        user_email = self.request.get('email')

        error_username = self.valid_username(user_username)
        error_password, error_verify = self.valid_password(user_password, user_verify)
        error_email = self.valid_email(user_email)

        if not (error_username or error_password or error_verify or error_email):
            self.redirect("/Welcome?username=" + user_username)
        else:
            self.write_form(user_username, error_username, user_password, error_password, user_verify, error_verify, user_email, error_email)

class WelcomeHandler(webapp2.RequestHandler):
    def get(self):
        username = self.request.get('username')
        self.response.out.write("Welcome, %s!" % username)

app = webapp2.WSGIApplication([('/signup', SignUp),
                               ('/Welcome', WelcomeHandler)],
                              debug=True)

app.yaml

application: udacity-cs253-zfz
version: 1
runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /.* 
  script: signup.app

标签:Google,App,Engine

收藏

0人收藏

支持

0

反对

0

发表评论