def createUser(login_session): newUser= User(name = login_session['username'], email = login_session['email'], picture = login_session['picture']) session.add(newUser) session.commit() user = session.query(User.filter(email = login_session['email']).one()) return user.id
def new_user(): username = request.json.get('username') password = request.json.get('password') if username is None or password is None: print "missing arguments" abort(400) if session.query(User).filter_by(username = username).first() is not None: print "existing user" user = session.query(User).filter_by(username=username).first() return jsonify({'message':'user already exists'}), 200#, {'Location': url_for('get_user', id = user.id, _external = True)} user = User(username = username) user.hash_password(password) session.add(user) session.commit() return jsonify({ 'username': user.username }), 201#, {'Location': url_for('get_user', id = user.id, _external = True)}
def verify_password(username_or_token, password): #Try to see if it's a token first user_id = User.verify_auth_token(username_or_token) if user_id: user = session.query(User).filter_by(id = user_id).one() else: user = session.query(User).filter_by(username = username_or_token).first() if not user or not user.verify_password(password): return False g.user = user return True
# Bind the engine to the metadata of the Base class so that the # declaratives can be accessed through a DBSession instance Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) # A DBSession() instance establishes all conversations with the database # and represents a "staging zone" for all the objects loaded into the # database session object. Any change made against the objects in the # session won't be persisted into the database until you call # session.commit(). If you're not happy about the changes, you can # revert all of them back to the last commit by calling # session.rollback() session = DBSession() # add a dummy user in the table user1 = User(name="Ahmad", email="*****@*****.**", picture="mypic.jpg") session.add(user1) session.commit() # Category for Soccer with Items in it category1 = Category(name="Soccer") session.add(category1) session.commit() catItem2 = Category_item( name="Jersey", description="Board approved Jersey with Classic Logo in the middle", category=category1, user_id=user1.id)
def login(provider): #STEP 1 - Parse the auth code auth_code = request.json.get('auth_code') print "Step 1 - Complete, received auth code %s" % auth_code if provider == 'google': #STEP 2 - Exchange for a token try: # Upgrade the authorization code into a credentials object oauth_flow = flow_from_clientsecrets('client_secrets.json', scope='') oauth_flow.redirect_uri = 'postmessage' credentials = oauth_flow.step2_exchange(auth_code) except FlowExchangeError: response = make_response(json.dumps('Failed to upgrade the authorization code.'), 401) response.headers['Content-Type'] = 'application/json' return response # Check that the access token is valid. access_token = credentials.access_token url = ('https://www.googleapis.com/login2/v1/tokeninfo?access_token=%s' % access_token) h = httplib2.Http() result = json.loads(h.request(url, 'GET')[1]) if result.get('error') is not None: response = make_response(json.dumps(result.get('error')), 500) response.headers['Content-Type'] = 'application/json' print "Step 2 Complete! Access Token : %s " % credentials.access_token #STEP 3 - Find User or make a new one #Get user info h = httplib2.Http() userinfo_url = "https://www.googleapis.com/login2/v1/userinfo" params = {'access_token': credentials.access_token, 'alt':'json'} answer = requests.get(userinfo_url, params=params) data = answer.json() name = data['name'] picture = data['picture'] email = data['email'] #see if user exists, if it doesn't make a new one user = session.query(User).filter_by(email=email).first() if not user: user = User(username = name, picture = picture, email = email) session.add(user) session.commit() #STEP 4 - Make token token = user.generate_auth_token(600) #STEP 5 - Send back token to the client return jsonify({'token': token.decode('ascii')}) #return jsonify({'token': token.decode('ascii'), 'duration': 600}) else: return 'Unrecoginized Provider'
# Bind the engine to the metadata of the Base class so that the # declaratives can be accessed through a DBSession instance Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) # A DBSession() instance establishes all conversations with the database # and represents a "staging zone" for all the objects loaded into the # database session object. Any change made against the objects in the # session won't be persisted into the database until you call # session.commit(). If you're not happy about the changes, you can # revert all of them back to the last commit by calling # session.rollback() session = DBSession() # create a user User1 = User(name="Steve J", email="*****@*****.**") #Menu for UrbanBurger restaurant1 = Restaurant(name = "Urban Burger", street = "150 Sutton Place", city = "Closter", zipcode = "07624", rtype = "Tavern") session.add(restaurant1) session.commit() menuItem1 = MenuItem(name = "French Fries", description = "with garlic and parmesan", price = "$2.99", course = "Appetizer", restaurant = restaurant1) session.add(menuItem1) session.commit() menuItem2 = MenuItem(name = "Chicken Burger", description = "Juicy grilled chicken patty with tomato mayo and lettuce", price = "$5.50", course = "Entree", restaurant = restaurant1) session.add(menuItem2)
from database_setup2 import User, Base, Category, Book engine = create_engine( 'postgresql://*****:*****@localhost/catalog' ) # Bind the engine to the metadata of the Base class so that the # declaratives can be accessed through a DBSession instance Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) session = DBSession() user1 = User( name="Josue Acosta", email="*****@*****.**", picture= "https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg" ) user2 = User( name="", email="*****@*****.**", picture= "https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg" ) user3 = User( name="Josue Acosta", email="*****@*****.**", picture= "https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg" ) user4 = User(