Guest User

Untitled

a guest
Jul 6th, 2018
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. from flask import Flask, jsonify, request, render_template
  2. from flask_sqlalchemy import SQLAlchemy
  3. import re
  4. from werkzeug.security import generate_password_hash, check_password_hash
  5. from sqlalchemy.orm import validates
  6.  
  7. app = Flask(__name__)
  8. app.config['DEBUG'] = True
  9. app.config['SECRET_KEY'] = '5791628bb0b13ce0c676dfde280ba245'
  10. app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'
  11. app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
  12.  
  13. db = SQLAlchemy(app)
  14.  
  15. class User(db.Model):
  16. id = db.Column(db.Integer, primary_key=True)
  17. username = db.Column(db.String(64), index=True, unique=True, nullable=False)
  18. email = db.Column(db.String(120), index=True, nullable=False)
  19. password_hash = db.Column(db.String(128))
  20. role = db.Column(db.Enum('basic', 'admin', name='user_roles'), default='basic')
  21.  
  22. def set_password(self, password):
  23. self.password_hash = generate_password_hash(password)
  24.  
  25. def check_password(self, password):
  26. return check_password_hash(self.password_hash, password)
  27.  
  28. @validates('username')
  29. def validate_username(self, key, username):
  30. if not username:
  31. raise AssertionError('No username provided')
  32.  
  33. if User.query.filter(User.username == username).first():
  34. raise AssertionError('Username is already in use')
  35.  
  36. if len(username) < 5 or len(username) > 20:
  37. raise AssertionError('Username must be between 5 and 20 characters')
  38.  
  39. return username
  40.  
  41. @validates('email')
  42. def validate_email(self, key, email):
  43. if not email:
  44. raise AssertionError('No email provided')
  45.  
  46. if not re.match("[^@]+@[^@]+\.[^@]+", email):
  47. raise AssertionError('Provided email is not an email address')
  48.  
  49. return email
  50.  
  51. def set_password(self, password):
  52. if not password:
  53. raise AssertionError('Password not provided')
  54.  
  55. if not re.match('\d.*[A-Z]|[A-Z].*\d', password):
  56. raise AssertionError('Password must contain 1 capital letter and 1 number')
  57.  
  58. if len(password) < 5 or len(password) > 50:
  59. raise AssertionError('Password must be between 8 and 50 characters')
  60.  
  61. self.password_hash = generate_password_hash(password)
  62.  
  63.  
  64. #@app.before_first_request
  65. #def create_db():
  66. # db.create_all()
  67.  
  68. @app.route('/')
  69. def home():
  70. return render_template('index.html')
  71.  
  72. @app.route('/create', methods=['POST'])
  73. def create():
  74.  
  75. data = request.get_json()
  76. username = data['username']
  77. password = data['password']
  78. email = data['email']
  79.  
  80. user = User(username=username, email=email)
  81. user.set_password(password)
  82. try:
  83. db.session.add(user)
  84. db.session.commit()
  85. return jsonify(msg='User successfully created', user_id=user.id), 200
  86. except AssertionError as exception_message:
  87. return jsonify(msg='Error: {}. '.format(exception_message)), 400
  88.  
  89.  
  90. if __name__ == '__main__':
  91. app.run()
Add Comment
Please, Sign In to add comment