Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- import os
- import time
- import urlparse
- from flask import Flask
- from flask import render_template
- from flask import request
- from flask_sqlalchemy import SQLAlchemy
- from werkzeug import generate_password_hash
- from werkzeug import check_password_hash
- app = Flask(__name__)
- app.config['SQLALCHEMY_DATABASE_URI'] = os.getenv(
- 'DATABASE_URL', 'mysql://hackweek:hackweek@hackweek-db:3306/hackweek-db')
- db = SQLAlchemy(app)
- class User(db.Model):
- __tablename__ = 'tbl_user'
- user_id = db.Column(db.Integer, primary_key=True)
- user_name = db.Column(db.String(45), unique=False, nullable=False)
- user_username = db.Column(db.String(45), unique=True, nullable=False)
- user_password = db.Column(db.String(45), unique=False, nullable=False)
- def __repr__(self):
- return '<User %r>' % self.username
- @app.cli.command('initdb')
- def initdb_command():
- """Initializes the database."""
- while True:
- try:
- db.create_all()
- print('Initialized the database.')
- except Exception as e:
- print('Exception found: {0}'.format(e))
- time.sleep(2)
- @app.route("/")
- def main():
- return render_template('index.html')
- @app.route('/showSignUp')
- def showSignUp():
- return render_template('signup.html')
- @app.route('/signUp', methods=['POST'])
- def signUp():
- # read the posted values from the UI
- _name = request.form['inputName']
- _email = request.form['inputEmail']
- _password = request.form['inputPassword']
- # validate the recieved values
- if _name and _email and _password:
- return json.dumps({'html': '<span>All fields good !!</span>'})
- else:
- return json.dumps({'html': '<span>Enter the required fields</span>'})
- # creating the user
- def createUser():
- _hashed_password = generate_password_hash(_password)
- user = User(user_name=_name, user_username=_email, user_password=_password)
- db.session.add(user)
- try:
- db.session.commit()
- return json.dumps({'message': 'User created successfully !'})
- except Exception as e:
- return json.dumps({'error': str(e)})
Add Comment
Please, Sign In to add comment