Advertisement
Guest User

jsldjf

a guest
Apr 4th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. from flask import Flask, request, render_template
  2. import string
  3.  
  4. app = Flask(__name__)
  5.  
  6. app.config['DEBUG'] = True
  7.  
  8. @app.route('/')
  9. def display_form():
  10. return render_template('index.html')
  11.  
  12. @app.route('/', methods=['POST'])
  13. def validate_form():
  14. username = request.form['username']
  15. password = request.form['password']
  16. verified_password = request.form['verify']
  17. email = request.form['email']
  18. invalidChars = set(' ')
  19. validChars = set('@, .')
  20. username_content = ""
  21. email_content = ""
  22. errors_present = False
  23.  
  24.  
  25. # Here we will check the validity of the username. It cannot contain spaces, be empty, be less than 3 character or longer than 20 characters.
  26. if any(char in invalidChars for char in username) or len(username)<3 or len(username)>20:
  27. username_error_msg = "That is not a valid username"
  28. username_content = ""
  29. errors_present = True
  30. else:
  31. username_error_msg = ""
  32. username_content = username
  33.  
  34.  
  35. # Here we validate the password. It follows the same rules as the username.
  36. if any(char in invalidChars for char in password) or len(password)<3 or len(password)>20:
  37. password_error_msg = "That is not a valid password"
  38. errors_present = True
  39. else:
  40. password_error_msg = ""
  41.  
  42. # Here we validate the 'verify password' field. If it does not match the passowrd it throws an error
  43. ver_pass_err_stat = verified_password != password
  44. if ver_pass_err_stat == True:
  45. verify_error_msg = "Passwords do not match"
  46. errors_present = True
  47. else:
  48. verify_error_msg = ""
  49.  
  50. # Here we validate the email. It can be left blank but if it does contain characters we check to see if it is a valid email by looking for the @ symbol and '.' character
  51. if email != "":
  52. if any(char not in validChar for char in email):
  53. email_error_msg = "That is not a valid email address"
  54. email_content = ""
  55. errors_present = True
  56. else:
  57. email_error_msg = ""
  58. email_content = email
  59. else:
  60. email_error_msg = ""
  61. email_content = email
  62.  
  63. # If there are any errors present it will rerender the form with a blanket display of any present errors. If there are no errors present then it will render the 'Welcome' template instead.
  64. if errors_present == True:
  65. return render_template('index.html',
  66. username = username_content,
  67. username_error = username_error_msg,
  68. password_error = password_error_msg,
  69. verify_error = verify_error_msg,
  70. email = email_content,
  71. email_error = email_error_msg
  72. )
  73. else:
  74. render_template('welcome.html', new_user = username)
  75.  
  76.  
  77.  
  78. app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement