SHARE
TWEET

Untitled

a guest Jun 19th, 2017 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from models import Base, User
  2. from flask import Flask, jsonify, request, url_for, abort, json
  3. from sqlalchemy.ext.declarative import declarative_base
  4. from sqlalchemy.orm import relationship, sessionmaker
  5. from sqlalchemy import create_engine
  6. from passlib.hash import sha256_crypt
  7.  
  8. from flask import Flask
  9.  
  10. engine = create_engine('sqlite:///users.db')
  11.  
  12. Base.metadata.bind = engine
  13. DBSession = sessionmaker(bind=engine)
  14. session = DBSession()
  15. app = Flask(__name__)
  16.  
  17. @app.route('/api/users', methods = ['POST'])
  18. def new_user():
  19.     content = request.get_json(silent=True)
  20.     username =  content['username']
  21.     password = content['password']
  22.     # username = request.get_json('username')
  23.     # password = request.get_json('password')
  24.     password = json.dumps(password)
  25.     password_hash = sha256_crypt.encrypt(password)
  26.     # print("Hello %s", username)
  27.     if (not username) or (not password):
  28.         abort(400) # missing arguments
  29.     user = User(id = 123456789, username = username, password_hash = password_hash)
  30.  
  31.     # # if session.query(User).filter_by(username = username).first() is not None:
  32.     # #     abort(400) # existing user
  33.     if user:
  34.         session.add(user)
  35.         session.commit()
  36.         return jsonify({ 'username': user.username }), 201, {'Location': url_for('get_user', id = user.id, _external = True)}
  37.  
  38. @app.route('/api/users/<int:id>')
  39. def get_user(id):
  40.     user = session.query(User).filter_by(id=id).one()
  41.     if not user:
  42.         abort(400)
  43.     return jsonify({'username': user.username})
  44.  
  45.  
  46. if __name__ == '__main__':
  47.     app.debug = True
  48.     app.run(host='0.0.0.0', port=5009)
RAW Paste Data
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top