Advertisement
Guest User

Untitled

a guest
May 12th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.25 KB | None | 0 0
  1. # Well that's what i've done, i was just reading some shit on some blog that was giving pros and cons to both
  2.  
  3. # so my actual question is this, I have a route /register that you make a POST to.
  4.  
  5. @user_blueprint.route('/register', methods=['POST'])
  6. def register():
  7.     email = request.form['email']
  8.     password = request.form['password']
  9.     username = request.form['username']
  10.  
  11.     if isValidEmail(email) and isValidPassword(password) and \
  12.        isValidUsername(username):
  13.         user = User(
  14.             email=email,
  15.             password=password,
  16.             username=username
  17.         )
  18.  
  19.         db.session.add(user)
  20.         db.session.commit()
  21.  
  22.         login_user(user)
  23.     etc. etc.
  24.  
  25.  
  26.  
  27.  
  28.  # In that I have the 3 funcs isValid...() eg:
  29.  
  30.  
  31. def isValidEmail(email):
  32.     if len(email) > 7:
  33.         # https://stackoverflow.com/questions/8022530/how-to-check-for-valid-email-address
  34.         if re.match("^.+@(\[?)[a-zA-Z0-9-.]+.([a-zA-Z]{2,3}|[0-9]{1,3})(]?)$",
  35.                     email):
  36.             if User.query.filter_by(email=email).first() is None:
  37.                 return True
  38.             else:
  39.                 # return False "Error: user already exists"
  40.     # return False "Error: not a valid email address"
  41.  
  42. def isValidPassword(password):
  43.     # https://stackoverflow.com/questions/41117733/validation-a-password-python
  44.     pattern = re.compile('^(?=\S{6,20}$)(?=.*?\d)(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[^A-Za-z\s0-9])')
  45.     if pattern.match(password):
  46.         return True
  47.     else:
  48.         # return False "Password Requirments: 6 chars, 1 digit, 1 Uppercase, 1 special"
  49.  
  50.  
  51. # This is when i get a bit lost, I want the corresponding errors to appear below the respective inputs dynamically (as the rest of the form is dynamically built, instead of hiding/showing).
  52. # So i need to return false back to register and send the error message back to the client to be rendered clientside.
  53.  
  54. # I thought about handling all the errors for the modal (username, email, password) in one function where you pass in the input element and then insert the new <p> after and then set the innerHTML to that of the error. But im not quite sure how i would do that or if that is the best way, if there is a better/nicer way that im missing, would appreciate the help.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement